开发者

.net layered design: return error code to forms indicating which controls have an error

开发者 https://www.devze.com 2023-02-15 19:03 出处:网络
Let\'s say I have a simple application. It tries to add a new entry to a database grabbing the info from the user. The information is a typical MasterDetail object like an invoice.

Let's say I have a simple application. It tries to add a new entry to a database grabbing the info from the user. The information is a typical MasterDetail object like an invoice.

  1. The Invoice object used to collect the data has some simple attributes (Id, Date, etc...) and a List of InvoiceDetail, consisting of some simple attributes (invoiceId, productId, quantity, etc...)
  2. A form in the UI Layer collects the data from the user, creates an instance of Invoice and sends it to the Business Layer. It has some simple controls for the simple atributes and a DataGridView for the InvoiceDetails list.
  3. The Business layer sends the data to a Validation Module. If any error in the input is detected it has to return some kind of error code.
  4. The Business Layer has to send the error code back to the Form to let the user correct the wrong information.

I want the application to display some kind of warning (a different background color for example) in every form control with an error.

  • What's a good approach to manage the errors to be able to inform the form which controls are wrong?
  • Can I create an error object?
  • Use delegates?

The harder problem I see is locating a specific item in the details since this list is are arbitrary long, but even without this problem I have no idea where to start.

I'm using VB.ne开发者_Go百科t but don't think this is relevant to the solution of the problem. P.D: Please suggest me some tags to this post.


If the error is to be notified to the user (so that the data in the form can be corrected) then the best way to notify the user is to use exception rather than using cryptic error codes to denote the error.

Suppose you have a method in the Validation module that accepts the instance of Invoice, then the code in the Validate() method should be something like this,


Approach 1: Break when first encounter of the exception

public void Validate(Invoice invoiceInstance)
{
    foreach(InvoiceDetail invoiceDetail in invoiceInstance.InvoiceDetailsList)
    {
        if(invoiceDetail.quantity < 0)
        {
            throw new InvoiceDetailException("The invoice detail quantity is less than zero", InvoiceDetailExceptionType.Quantity, invoiceDetail);
        }

        // Validate other details
    }  
}

and in the UI, catch the exception and handle it,

try
{
    businessLayer.Validate();
}
catch(InvoiceDetailException invoiceDetailException)
{
    MessageBox.Show(invoiceDetailException.Message);
}

The custom InvoiceException can be created like this,

[Serializable]
public class InvoiceDetailException : Exception
{
    public InvoiceDetailExceptionType ExceptionType {get; private set;}

    public InvoiceDetail Detail {get; private set; }

    public InvoiceDetailException () { }

    public InvoiceDetailException (string message, InvoiceDetailExceptionType exType, InvoiceDetail detail) : base(message) 
    {
        this.ExceptionType = exType;
        this.Detail = detail;
    }

    public InvoiceDetailException (string message, Exception inner) : base(message, inner) { }

    protected InvoiceDetailException (
      System.Runtime.Serialization.SerializationInfo info,
      System.Runtime.Serialization.StreamingContext context)
        : base(info, context) { }
}

And the InvoiceDetailExceptionType enumeration is,

public enum InvoiceDetailExceptionType
{
    Quantity,
    ProductId
}


Approach 2: Show cumulative exceptions
The entity classes,

class Invoice
{
    public int Id { get; set; }
    public List<InvoiceDetail> Details { get; set; }

    public Invoice()
    {
        Details = new List<InvoiceDetail>();
    }
}

class InvoiceDetail
{
    public int InvoiceId
    { get; set; }

    public int Quantity
    { get; set; }

    public int ProductId
    { get; set; }
}


The validator

static class InvoiceValidator
{
    public static void Validate(Invoice invoice)
    {
        List detailsException = new List<InvoiceDetailException>();

        foreach (InvoiceDetail detail in invoice.Details)
        {
            try
            {
                ValidateDetail(detail);
            }
            catch (InvoiceDetailException detailException)
            {
                detailsException.Add(detailException);
            }
        }

        InvoiceException invoiceException = null;

        if (invoice.Id < 0)
        {
            // If error in the invoice field
             invoiceException = new InvoiceException("InvoiceId is invalid");
        }

        // Validate other fields of invoice...

        if (detailsException.Count > 0)
        {
            // If error in the details list
            if (invoiceException == null)
            {
                // If there is no error in the invoice field, then error only in details
                invoiceException = new InvoiceException("Exception only in details", detailsException);
            }
            else
            {
                // If there is error in invoice field as well as details
                invoiceException.DetailExceptionList = detailsException;
            }
        }

        if (invoiceException != null)
        {
            // If there is an error then throw exception
            throw invoiceException;
        }
    }

    private static void ValidateDetail(InvoiceDetail detail)
    {
        if (detail.Quantity <= 0)
        {
            throw new InvoiceDetailException("Quantity is invalid", InvoiceDetailExceptionType.Quantity, detail.InvoiceId, detail.ProductId);
        }

        // Validate other fields of invoice details...
    }
}


The custom exceptions,

[Serializable]
public class InvoiceDetailException : Exception
{
    public InvoiceDetailExceptionType DetailExceptionType { get; private set; }
    public int InvoiceId { get; private set; }
    public int ProductId { get; private set; }

    public InvoiceDetailException() { }

    public InvoiceDetailException(string message, InvoiceDetailExceptionType exceptionType, int invoiceId, int productId)
        : base(message)
    {
        this.DetailExceptionType = exceptionType;
        this.InvoiceId = invoiceId;
        this.ProductId = productId;
    }

    public InvoiceDetailException(string message, Exception inner) : base(message, inner) { }

    protected InvoiceDetailException(
    System.Runtime.Serialization.SerializationInfo info,
    System.Runtime.Serialization.StreamingContext context)
        : base(info, context) { }
}

[Serializable]
public class InvoiceException : Exception
{
    public List<InvoiceDetailException> DetailExceptionList { get; set; }
    public InvoiceException() { }

    public InvoiceException(string message): base(message) { }

    public InvoiceException(string message, List detailsExceptions) : base(message)
    {
        DetailExceptionList = detailsExceptions;
    }

    public InvoiceException(string message, Exception inner) : base(message, inner) { }

    protected InvoiceException(
      System.Runtime.Serialization.SerializationInfo info,
      System.Runtime.Serialization.StreamingContext context)
        : base(info, context) { }
}


The exception type enumeration,

public enum InvoiceDetailExceptionType
{
    Quantity,
    ProductId
}


And finally the call to the validator,

try
{
    InvoiceValidator.Validate(invoice);
}
catch (InvoiceException invoiceException)
{
    MessageBox.Show(invoiceException.Message);
    if (invoiceException.DetailExceptionList != null && invoiceException.DetailExceptionList.Count > 0)
    {
        Console.WriteLine("Exception in details :");
        foreach (InvoiceDetailException detailException in invoiceException.DetailExceptionList)
        {
            string printMessage = string.Format("{0}, InvoiceId = {1}, ProductId = {2}", detailException.Message, detailException.InvoiceId, detailException.ProductId);
            Console.WriteLine(printMessage);
        }
    }

    // If you want the caller to further handle the exception
    //throw;
}

More information

  • Exception handling
  • Exception Handling Best Practices in .NET
  • Custom Exception Handling in C#
  • Designing Custom Exceptions
0

精彩评论

暂无评论...
验证码 换一张
取 消