none
How to do centralized exception handling when using O/R Mapping Framework RRS feed

  • Question

  • I have a C# ASP.NET project with an SQL Server backend.  The data is accessed via the object-relational mapping (ORM) framework. 

    There are a number of lookup tables that supply values to combo boxes.  These tables have an identity "ID" field and a lookup entry.  The lookup entry column has a 'unique' index on it in order to prevent duplicates. 

    For example, for the "RequestType" table, the two columns are "ID" and "RequestType".  Entries includes values like "HW", "SW", and "Service".  If one attempts to enter "HW" a second time, SQL Server throws an error.

    I have a page which allows the user to add/update/delete each of these lookup tables.  In each case, a ListView control shows the existing entries.  The listview gets its data from a LinqDataSource, whose 'ContextTypeName' property is set via the property field's dropdown listbox to the O/R model, and whose 'TableName' property is set via the property field's dropdown list box to the appropriate table (i.e., no guesswork).

    Using the previous example, the listview is named "lvRequestType" and the LinqDataSource is named "ldsRequestType".

    In addition, I have a class file that implements the OnValidate() method.  For example:

    namespace MyApp.DataModel
    {
        public partial class RequestType
        {
            partial void OnValidate(System.Data.Linq.ChangeAction action)
            {
                if (RequestType == null || RequestType == "")
                {
                    throw new ArgumentException("A Request Type is required.");
                }
            }
        }
    }
    

    Now (finally!) to the question.  I want to handle exceptions in the same class file as the one that contains the "OnValidate" method.  This way, I can centralize the error handling, rather than having to implement it via a method(s) attached to the listview (e.g., "lvRequestType_ItemInserted") or the LinqDataSource (e.g., "ldsRequestType_ItemInserted").

    Some might suggest using a try{}catch{} with .SubmitChanges , but since I'm using a LinqDataSource, I don't see how I could do this.

    There are methods ala "OnValidate()" for handling changes (e.g., OnRequestTypeChanged()).  However, I'm not sure how to access any exceptions that might be thrown by the database -- there are no parameters defined for OnRequestTypeChanged() type methods.

    I know that there are detailed SQLExceptions being passed by the ORM framework, because I can see them in the lvRequestType_Updated and the ldsRequestType_Updated methods.  But, I then have to write an _Update method on each and every page, and as I said, I'd like to centralize that code.

    Anyone have any suggestions?  I find it unbelievable that Microsoft would provide an OnValidate() method for the O/R framework to allow centralization of validation, and not provide a corresponding means to handle stuff coming out of the database.  Then again ...

    Your help is greatly appreciated!

     

     

     

    Saturday, May 9, 2015 4:28 AM

Answers

  • Hello,

    >> But, I then have to write an _Update method on each and every page, and as I said, I'd like to centralize that code.

    In .NET, for centralize that code which does same thing, usually, we could use the abstract class, however, as you mentions, you are developing a web applicaiotn, as far as I remembered, you could write a custom class implement the Page and use that class as the based page class as below:

    public partial class WebForm1 : MyPage
    
        {
    
            protected void Page_Load(object sender, EventArgs e)
    
            {
    
                OnValidation();
    
            }
    
    
            //public override void OnValidation()
    
            //{
    
            //    //throw new NotImplementedException();
    
            //}
    
        }
    
    
        public abstract class MyPage : System.Web.UI.Page
    
        {
    
            public void OnValidation()
    
            {
    
    
            }
    
        }
    

    Then all OnValidation method could be reused by each page.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, May 13, 2015 7:34 AM
    Moderator

All replies

  • Hello Richard Hildreth,

    For issues related with asp.net, i suggest you could post it to the asp.net forum:http://forums.asp.net/, the current forum is used to discuss about questions and discussion about LINQ to SQL.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, May 11, 2015 5:28 AM
    Moderator
  • This is a LINQ to SQL question, not an ASP.NET question -- I only mentioned ASP.NET to give context.
    Monday, May 11, 2015 1:38 PM
  • Hello,

    >> But, I then have to write an _Update method on each and every page, and as I said, I'd like to centralize that code.

    In .NET, for centralize that code which does same thing, usually, we could use the abstract class, however, as you mentions, you are developing a web applicaiotn, as far as I remembered, you could write a custom class implement the Page and use that class as the based page class as below:

    public partial class WebForm1 : MyPage
    
        {
    
            protected void Page_Load(object sender, EventArgs e)
    
            {
    
                OnValidation();
    
            }
    
    
            //public override void OnValidation()
    
            //{
    
            //    //throw new NotImplementedException();
    
            //}
    
        }
    
    
        public abstract class MyPage : System.Web.UI.Page
    
        {
    
            public void OnValidation()
    
            {
    
    
            }
    
        }
    

    Then all OnValidation method could be reused by each page.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, May 13, 2015 7:34 AM
    Moderator
  • Huh, I hadn't thought of that. Makes sense.

    Timing-wise though, we forged ahead before I got your replay, and we made the decision to throw all validation into the database, not have any validation in the app, and handle any returned exceptions (which we have to do, anyway).  It's working out OK.

    I like your post though, and I've marked it as an answer accordingly.

    Thanks!

    Friday, May 15, 2015 3:57 PM