【问题标题】:Is this 4-tier architecture good?(Exception handling is important for me :)这个 4 层架构好吗?(异常处理对我来说很重要 :)
【发布时间】:2011-04-05 11:56:04
【问题描述】:

我的应用程序中有这些层:

  • 实体
  • 数据库(带有实体参考)
  • 业务(包含数据库和实体参考)
  • 用户界面(带有业务和实体参考)

这是我的代码示例:

  • 数据库层中的 UserDAL 类:

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;
        }
    }

等等……


在业务层的 UserBLL 类中,我这样写:

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);
        }
    }

我在 UI 中写道:

    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
        }
    }

好吧,我使用一个名为 ProjectException 的类来产生一个错误,其中包含我创建的 BLL 消息和操作系统自动操作的异常消息。 我还创建了一个可能错误的枚举和一个字典 以下是有关它的一些详细信息:

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!";
            }
        }
    }

这样好吗? 它对我有用。 你有什么想法?

【问题讨论】:

    标签: n-tier-architecture 3-tier


    【解决方案1】:

    catch (ex) 中执行throw ex; 几乎总是错误的。要么只做throw;,要么做throw new whateverException("someMessage", ex);。是否使用前一种形式或后一种形式的决定通常取决于您是否跨越应用程序层。如果派生自 DatabaseWrapper 类型的 AcmeServerDatabaseWrapper 在引发 AcmeDatabaseTableNotFoundException 时正在执行查询,它应该捕获该异常并将其作为 DatabaseWrapperTableNotFoundException(如果存在此类类型)或作为 DatabaseWrapperOperationFailedException 重新引发。具有从 DatabaseWrapper 派生的对象的客户端代码应该能够知道该对象将抛出什么类型的异常,而不必知道它是什么类型的对象。任何从数据库层逃逸而没有被包装的异常都是客户端代码不太可能能够合理处理但可能会错误处理的异常(认为它发生在实际发生位置之外的上下文中)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多