LINQ to SQL (Part 5 - Binding UI using the ASP:LinqDataSource Control)
page 5 of 8
by Scott Guthrie
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 51432/ 86

Adding a Data Model Validation Rule

The wrong place to add this type of business validation rule is in the UI layer of our application.  Adding it in the UI layer of our application will mean that the rule will be specific to only that one place, and will not be automatically enforced when we add another page to our application that also updates Products.  Distributing business rules/logic in our UI layer will also make life extremely painful as our application grows in size - since changes/updates to our business will necessitate making code changes all over the place.

The right place to specify this type of business logic validation is instead in our LINQ to SQL data model classes that we defined earlier.  As I discussed in Part 4 of this series, all classes generated by the LINQ to SQL designer are defined as "partial" classes - which means that we can easily add additional methods/events/properties to them.  The LINQ to SQL data model classes automatically call validation methods that we can implement to enforce custom validation logic within them.

For example, I could add a partial Product class to my project that implements the OnValidate() partial method that LINQ to SQL calls prior to persisting a Product entity.  Within this OnValidate() method I could add the following business rule to enforce that products can't have a Reorder Level if the product is discontinued:

Figure 29

Once I add the above class into my LINQ to SQL project, the above business rule will be enforced anytime anyone uses my data model to try and modify the database.  This is true for both updating existing Products, as well as adding new Products into the database.

Because the <asp:LinqDataSource> that we defined in our pages above works against our LINQ to SQL data model classes, all of its update/insert/delete logic will now have to pass the above validation check prior to the change being persisted.  We do not need to-do anything to our UI tier in order for this validation to occur - it will automatically be applied anywhere and everywhere our LINQ to SQL data model is used.


View Entire Article

User Comments

Title: Add new   
Name: Aditya
Date: 2010-05-29 1:38:06 AM
Comment:
Hi boss this is really nice.
Title: Add column on selecting   
Name: Peter
Date: 2010-04-18 7:19:36 PM
Comment:
Can you actually add column on the linq data source while selecting?
Title: Nice   
Name: vijay
Date: 2009-03-02 5:52:16 AM
Comment:
Nice detailed article.
Title: Adding new item   
Name: Evans
Date: 2009-02-25 1:28:39 PM
Comment:
Can we add new item directly with linq?
Title: Cleaning Up SupplierID and CategoryID   
Name: brian
Date: 2008-10-12 4:03:05 PM
Comment:
If the associated category or supplier no longer exists, you will get a NullReferenceException if you attempt to sort on the associated column in the GridView. Is there some way to handle this?

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-05-05 1:07:50 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search