c# - 一般业务对象实践(和异常错误 - redux)

标签 c# error-handling object

最近,I made a post about the developers I'm working with not using try catch blocks properly ,不幸的是在危急情况下使用 try...catch block 并一起忽略异常错误。让我心痛。这是他们执行此操作的数千段代码之一的示例(一些代码遗漏并不特别重要:

public void AddLocations(BOLocation objBllLocations)
{
    try
    {
        dbManager.Open();
        if (objBllLocations.StateID != 0)
        {
             // about 20 Paramters added to dbManager here
        }
        else
        {
           // about 19 Paramters added here
        }
        dbManager.ExecuteNonQuery(CommandType.StoredProcedure, "ULOCATIONS.AddLocations");
    }
    catch (Exception ex)
    {
    }
    finally
    {
        dbManager.Dispose();
    }
}

在我看来,这绝对令人厌恶,并且在发生某些潜在问题时不会通知用户。我知道很多人说 OOP 是邪恶的,添加多层会增加代码行数和程序的复杂性,从而导致代码维护可能出现问题。我个人的大部分编程背景在这个领域都采用了几乎相同的方法。下面我列出了在这种情况下我通常编码方式的基本结构,在我的职业生涯中,我一直在使用多种语言进行编码,但这个特定的代码是用 C# 编写的。但是下面的代码是我如何使用对象的一个​​很好的基本想法,它似乎对我有用,但由于这是一些相当智能的编程地雷的良好来源,我想知道我是否应该重新评估它我用了这么多年的技术。主要是因为在接下来的几周内,我将深入研究外包开发人员提供的不太好的代码并修改大量代码。我想尽可能地做到这一点。很抱歉引用了很长的代码。

// *******************************************************************************************
/// <summary>
/// Summary description for BaseBusinessObject
/// </summary>
/// <remarks>
/// Base Class allowing me to do basic function on a Busines Object
/// </remarks>
public class BaseBusinessObject : Object, System.Runtime.Serialization.ISerializable
{
    public enum DBCode
    {   DBUnknownError,
        DBNotSaved,
        DBOK
    }

    // private fields, public properties
    public int m_id = -1;
    public int ID { get { return m_id; } set { m_id = value; } }
    private int m_errorCode = 0;
    public int ErrorCode { get { return m_errorCode; } set { m_errorCode = value; } }
    private string m_errorMsg = "";
    public string ErrorMessage { get { return m_errorMsg; } set { m_errorMsg = value; } }
    private Exception m_LastException = null;
    public Exception LastException { get { return m_LastException; } set { m_LastException = value;} }

    //Constructors
    public BaseBusinessObject()
    {
        Initialize();
    }
    public BaseBusinessObject(int iID)
    {
        Initialize();
        FillByID(iID);
    }
    // methods
    protected void Initialize()
    {
        Clear();
        Object_OnInit();
        // Other Initializable code here
    }
    public void ClearErrors()
    {
        m_errorCode  = 0; m_errorMsg = ""; m_LastException = null;
    }

    void System.Runtime.Serialization.ISerializable.GetObjectData(
         System.Runtime.Serialization.SerializationInfo info, 
        System.Runtime.Serialization.StreamingContext context)
    {
      //Serialization code for Object must be implemented here
    }
    // overrideable methods
    protected virtual void Object_OnInit()     
    {
        // User can override to add additional initialization stuff. 
    }
    public virtual BaseBusinessObject FillByID(int iID)
    {
        throw new NotImplementedException("method FillByID Must be implemented");
    }
    public virtual void Clear()
    {
        throw new NotImplementedException("method Clear Must be implemented");
    }
    public virtual DBCode Save()
    {
        throw new NotImplementedException("method Save Must be implemented");
    }
}
// *******************************************************************************************
/// <summary>
/// Example Class that might be based off of a Base Business Object
/// </summary>
/// <remarks>
/// Class for holding all the information about a Customer
/// </remarks>
public class BLLCustomer : BaseBusinessObject
{
    // ***************************************
    // put field members here other than the ID
    private string m_name = "";
    public string Name { get { return m_name; } set { m_name = value; } }
    public override void Clear()
    {
        m_id = -1;
        m_name = "";
    }
    public override BaseBusinessObject FillByID(int iID)
    {
        Clear();
        try
        {
            // usually accessing a DataLayerObject, 
            //to select a database record
        }
        catch (Exception Ex)
        {
            Clear();
            LastException = Ex;
            // I can have many different exception, this is usually an enum
            ErrorCode = 3;
            ErrorMessage = "Customer couldn't be loaded";
        }
        return this;
    }
    public override DBCode Save()
    {
        DBCode ret = DBCode.DBUnknownError;
        try
        {
            // usually accessing a DataLayerObject, 
            //to save a database record
            ret = DBCode.DBOK;
        }
        catch (Exception Ex)
        {
            LastException = Ex;
            // I can have many different exception, this is usually an enum
            // i do not usually use just a General Exeption
            ErrorCode = 3;
            ErrorMessage = "some really weird error happened, customer not saved";
            ret = DBCode.DBNotSaved;
        }
        return ret;
    }
}
// *******************************************************************************************
// Example of how it's used on an asp page.. 
    protected void Page_Load(object sender, EventArgs e)
    {
        // Simplifying this a bit, normally, I'd use something like, 
        // using some sort of static "factory" method
        // BaseObject.NewBusinessObject(typeof(BLLCustomer)).FillByID(34);
        BLLCustomer cust = ((BLLCustomer)new BLLCustomer()).FillByID(34);
        if (cust.ErrorCode != 0)
        {
            // There was an error.. Error message is in 
            //cust.ErrorMessage
            // some sort of internal error code is in
            //cust.ErrorCode

            // Give the users some sort of message through and asp:Label.. 
            // probably based off of cust.ErrorMessage
            //log can be handled in the data, business layer... or whatever
            lab.ErrorText = cust.ErrorMessage;
        }
        else
        {
            // continue using the object, to fill in text boxes, 
            // literals or whatever. 
            this.labID = cust.ID.toString();
            this.labCompName = cust.Name;
        }
    }

归根结底,我的问题是,我是否过度复杂化了多层和继承的类,或者我说明的旧概念是否仍然有效且稳定?现在有更好的方法来完成这些事情吗?我是否应该像同事同事开发人员建议的那样从页面后面的 asp.net 页面代码直接进行 SQL 调用(尽管最后一个解决方案让我感到恶心),而不是通过业务对象和数据层(数据层不是显示,但基本上包含所有存储过程调用)。是的,另一位开发人员确实问我为什么我要费力地分层,当你可以直接在页面后面的 *.aspx.cs 代码中输入你需要的东西,然后我就可以享受超过 1k 行代码的乐趣在后面。这里有什么建议?

最佳答案

您是否考虑过使用像 NHibernate 这样的 ORM?重新发明轮子没有意义。

对我来说这是一种代码味道:

BLLCustomer cust = ((BLLCustomer)new BLLCustomer()).FillByID(34);

括号太多了!

我发现在像 C# 这样的语言中使用事件记录模式总是以泪流满面,因为它很难(呃)进行单元测试。

关于c# - 一般业务对象实践(和异常错误 - redux),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/235240/

相关文章:

c# - SmtpClient.SendMailAsync 在抛出特定异常时导致死锁

python - 为什么jupyter即将出现名称未定义

objective-c - 涉及绑定(bind)时如何覆盖 NSError 表示?

python - 无效的语法Python Inside打印2.7.11

javascript - javascript 'function'/object 中的成员函数表示它不是一个函数

php - 如何获取数组列表中的对象php

javascript - 如何将一个对象的元素插入另一个对象?

c# - 用于创建标签的 itextsharp 编辑器

C# - 将十进制转换为 int32

c# - 无法使用适用于 .NET 的 EMDK