English 中文(简体)
这四级结构是否良好? 例外情况处理对我很重要:
原标题:Is this 4-tier architecture good?(Exception handling is important for me :)

我应用了这个层次:

  • Entities
  • Database (with Entities reference)
  • Business (with database and Entities references)
  • User Interface (with Business and Entities references)

我的守则就是这方面的例子:

  • UserDAL class in database layer:

public class UsersDal
{
    databaseDataContext db;
    public UsersDal()
    {
        try
        {
            db = new databaseDataContext(ConnectToDatabase.GetConnectionString());
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public List<User> GetAllUsers()
    {
        try
        {
            return (from u in db.Users select u).ToList();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

......


在商层用户BL类类中,如:

public class UsersBll
{
    UsersDal user;
    public UsersBll()
    {
        try
        {
            user = new UsersDal();
        }
        catch(Exception ex)
        {
            throw new ProjectException(Errors.CannotCreateObject, ex);
        }
    }
    public List<User> GetAllUsers()
    {
        try
        {
            return user.GetAllUsers();
        }
        catch(Exception ex)
        {
            throw new ProjectException(Errors.CannotReadData, ex);
        }
    }

并且,在《国际调查》一书中写:

    private void GetUsers()
    {
        try
        {
            UsersBll u = new UsersBll();
            datagrid.DataSource = u.GetAllUsers();
        }
        catch(ProjectException ex)
        {
            MessageBox(ex.UserMessage);// and also can show ex.InnerException.Message for more info
        }
    }

Well, I use a ProjectException named class to produce an error contain a BLL created message by me and an Exception message that the OS automatically manipulate. Also i create an enum of possible errors and a dictionary here is some details about it:

namespace Entities
{
    public enum Errors
    {
        CannotCreateObject,
        CannotReadData,
        CannotAdd,
        CannotEdit,
        CannotDelete,...
    }

[global::System.Serializable]
public class ProjectException : Exception
{
    public ProjectException(Errors er, Exception ex)
        : base(errors[er], ex)
    {
        currentEx = er;//er is Errors enum
    }
    static ProjectException()
    {
        errors = new Dictionary<Errors, string>();
        errors.Add(Errors.CannotCreateObject, "the application cannot connect to database!");
        errors.Add(Errors.CannotReadData, "the application cannot read data from database"); //...
    }
    public string UserMessage
    {
        get
        {
            try
            {
                return errors[currentEx];
            }
            catch
            {
                return "Unknown error!";
            }
        }
    }

Is this good? it work for me fine. what s your idea?

问题回答

在<编码>副渔获物(ex)上填写throw ex;。 仅读到throw; or others throw new whateverException ("someMessage”, ex);。 是否使用原表格或后者的决定一般取决于您是否跨越申请层。 如果AcmeServerDatabaseWrapper从数据库数据交换器类型中获取,在投放AcmeDatabaseTable NotFoundException时,它就应当追捕数据库(如果存在这种类型的)或作为数据库WrapperOperationFailedException而重新浏览。 用户代码,其目标来自数据库数据交换器,应当能够知道哪些类型的例外会消失,而不必知道哪一类目标。 如果不进行总结,任何从数据库层外逃的例外情况都是一种例外,客户代码不可能表面上处理,但可能错误地处理(在实际发生的情况之外,还可能发生这种情况)。





相关问题
ASP.Net and Collection

I want to build an N-Tier web-application , for shopping cart. I want to follow the concepts collections. That is retrieve data from db, fill it in collections and bind collections with asp.web and ...

Server architecture question. (WCF+NServiceBus)

First of all i will describe current state: Server consists of several WCF services, hosted in one or several win services on diffirent machines. Service responsible for recieving data from ...

Where to place Business Entities, Enums, Custom exceptions?

I m trying to figure out how to share my entities between the data, business, and UI tiers. Is it best to create a separate project for these entities that will be referenced by all the tiers? What ...

LINQ 2 SQL N-Tier Application

im trying to test and then implement LINQ 2 SQL as my Data Access to my N-Tier Application, as i read some info today, i added to my database a DateTime column as this reflected in my L2S Designer i ...

n-tier architecture: best place to store business objects?

Say I have a 3-tier architecture (UI, Business, and Data). Usually, I create a 4th project called "Model" or "Common" to keep my data access objects and each of the other projects would then use this ...

DDD Concepts in N-Layer Development

After spending a couple months studying DDD methodology, I ve now began to apply these concepts into actual products at my company. In fact, I ve been tasked with creating a suitable and maintainable ...

WCF based WinForms app in standalone mode

I have a windows application that has its business logic layer implemented as a WCF service. This service is currently hosted on IIS. I wanted to know that if there s a way where I could optionally ...

热门标签