Build a Model with Business Rule Validations
by Microsoft
This is step 3 of a free “NerdDinner” application tutorial that walks-through how to build a small, but complete, web application using ASP.NET MVC 1.
Step 3 shows how to create a model that we can use to both query and update the database for our NerdDinner application.
If you are using ASP.NET MVC 3, we recommend you follow the Getting Started With MVC 3 or MVC Music Store tutorials.
NerdDinner Step 3: Building the Model
In a model-view-controller framework the term “model” refers to the objects that represent the data of the application, as well as the corresponding domain logic that integrates validation and business rules with it. The model is in many ways the “heart” of an MVC-based application, and as we’ll see later fundamentally drives the behavior of it.
The ASP.NET MVC framework supports using any data access technology, and developers can choose from a variety of rich .NET data options to implement their models including: LINQ to Entities, LINQ to SQL, NHibernate, LLBLGen Pro, SubSonic, WilsonORM, or just raw ADO.NET DataReaders or DataSets.
For our NerdDinner application we are going to use LINQ to SQL to create a simple model that corresponds fairly closely to our database design, and adds some custom validation logic and business rules. We will then implement a repository class that helps abstract away the data persistence implementation from the rest of the application, and enables us to easily unit test it.
LINQ to SQL
LINQ to SQL is an ORM (object relational mapper) that ships as part of .NET 3.5.
LINQ to SQL provides an easy way to map database tables to .NET classes we can code against. For our NerdDinner application we’ll use it to map the Dinners and RSVP tables within our database to Dinner and RSVP classes. The columns of the Dinners and RSVP tables will correspond to properties on the Dinner and RSVP classes. Each Dinner and RSVP object will represent a separate row within the Dinners or RSVP tables in the database.
LINQ to SQL allows us to avoid having to manually construct SQL statements to retrieve and update Dinner and RSVP objects with database data. Instead, we’ll define the Dinner and RSVP classes, how they map to/from the database, and the relationships between them. LINQ to SQL will then takes care of generating the appropriate SQL execution logic to use at runtime when we interact and use them.
We can use the LINQ language support within VB and C# to write expressive queries that retrieve Dinner and RSVP objects from the database. This minimizes the amount of data code we need to write, and allows us to build really clean applications.
Adding LINQ to SQL Classes to our project
We’ll begin by right-clicking on the “Models” folder within our project, and select the Add->New Item menu command:
This will bring up the “Add New Item” dialog. We’ll filter by the “Data” category and select the “LINQ to SQL Classes” template within it:
We’ll name the item “NerdDinner” and click the “Add” button. Visual Studio will add a NerdDinner.dbml file under our directory, and then open the LINQ to SQL object relational designer:
Creating Data Model Classes with LINQ to SQL
LINQ to SQL enables us to quickly create data model classes from existing database schema. To-do this we’ll open the NerdDinner database in the Server Explorer, and select the Tables we want to model in it:
We can then drag the tables onto the LINQ to SQL designer surface. When we do this LINQ to SQL will automatically create Dinner and RSVP classes using the schema of the tables (with class properties that map to the database table columns):
By default the LINQ to SQL designer automatically “pluralizes” table and column names when it creates classes based on a database schema. For example: the “Dinners” table in our example above resulted in a “Dinner” class. This class naming helps make our models consistent with .NET naming conventions, and I usually find that having the designer fix this up convenient (especially when adding lots of tables). If you don’t like the name of a class or property that the designer generates, though, you can always override it and change it to any name you want. You can do this either by editing the entity/property name in-line within the designer or by modifying it via the property grid.
By default the LINQ to SQL designer also inspects the primary key/foreign key relationships of the tables, and based on them automatically creates default “relationship associations” between the different model classes it creates. For example, when we dragged the Dinners and RSVP tables onto the LINQ to SQL designer a one-to-many relationship association between the two was inferred based on the fact that the RSVP table had a foreign-key to the Dinners table (this is indicated by the arrow in the designer):
The above association will cause LINQ to SQL to add a strongly typed “Dinner” property to the RSVP class that developers can use to access the Dinner associated with a given RSVP. It will also cause the Dinner class to have a “RSVPs” collection property that enables developers to retrieve and update RSVP objects associated with a particular Dinner.
Below you can see an example of intellisense within Visual Studio when we create a new RSVP object and add it to a Dinner’s RSVPs collection. Notice how LINQ to SQL automatically added a “RSVPs” collection on the Dinner object:
By adding the RSVP object to the Dinner’s RSVPs collection we are telling LINQ to SQL to associate a foreign-key relationship between the Dinner and the RSVP row in our database:
If you don’t like how the designer has modeled or named a table association, you can override it. Just click on the association arrow within the designer and access its properties via the property grid to rename, delete or modify it. For our NerdDinner application, though, the default association rules work well for the data model classes we are building and we can just use the default behavior.
NerdDinnerDataContext Class
Visual Studio will automatically create .NET classes that represent the models and database relationships defined using the LINQ to SQL designer. A LINQ to SQL DataContext class is also generated for each LINQ to SQL designer file added to the solution. Because we named our LINQ to SQL class item “NerdDinner”, the DataContext class created will be called “NerdDinnerDataContext”. This NerdDinnerDataContext class is the primary way we will interact with the database.
Our NerdDinnerDataContext class exposes two properties - “Dinners” and “RSVPs” - that represent the two tables we modeled within the database. We can use C# to write LINQ queries against those properties to query and retrieve Dinner and RSVP objects from the database.
The following code demonstrates how to instantiate a NerdDinnerDataContext object and perform a LINQ query against it to obtain a sequence of Dinners that occur in the future. Visual Studio provides full intellisense when writing the LINQ query, and the objects returned from it are strongly-typed and also support intellisense:
In addition to allowing us to query for Dinner and RSVP objects, a NerdDinnerDataContext also automatically tracks any changes we subsequently make to the Dinner and RSVP objects we retrieve through it. We can use this functionality to easily save the changes back to the database - without having to write any explicit SQL update code.
For example, the code below demonstrates how to use a LINQ query to retrieve a single Dinner object from the database, update two of the Dinner properties, and then save the changes back to the database:
[!code-csharpMain]
1: NerdDinnerDataContext db = new NerdDinnerDataContext();
2:
3: // Retrieve Dinner object that reprents row with DinnerID of 1
4: Dinner dinner = db.Dinners.Single(d => d.DinnerID == 1);
5:
6: // Update two properties on Dinner
7: dinner.Title = "Changed Title";
8: dinner.Description = "This dinner will be fun";
9:
10: // Persist changes to database
11: db.SubmitChanges();
The NerdDinnerDataContext object in the code above automatically tracked the property changes made to the Dinner object we retrieved from it. When we called the “SubmitChanges()” method, it will execute an appropriate SQL “UPDATE” statement to the database to persist the updated values back.
Creating a DinnerRepository Class
For small applications it is sometimes fine to have Controllers work directly against a LINQ to SQL DataContext class, and embed LINQ queries within the Controllers. As applications get larger, though, this approach becomes cumbersome to maintain and test. It can also lead to us duplicating the same LINQ queries in multiple places.
One approach that can make applications easier to maintain and test is to use a “repository” pattern. A repository class helps encapsulate data querying and persistence logic, and abstracts away the implementation details of the data persistence from the application. In addition to making application code cleaner, using a repository pattern can make it easier to change data storage implementations in the future, and it can help facilitate unit testing an application without requiring a real database.
For our NerdDinner application we’ll define a DinnerRepository class with the below signature:
[!code-csharpMain]
1: public class DinnerRepository {
2:
3: // Query Methods
4: public IQueryable<Dinner> FindAllDinners();
5: public IQueryable<Dinner> FindUpcomingDinners();
6: public Dinner GetDinner(int id);
7:
8: // Insert/Delete
9: public void Add(Dinner dinner);
10: public void Delete(Dinner dinner);
11:
12: // Persistence
13: public void Save();
14: }
Note: Later in this chapter we’ll extract an IDinnerRepository interface from this class and enable dependency injection with it on our Controllers. To begin with, though, we are going to start simple and just work directly with the DinnerRepository class.
To implement this class we’ll right-click on our “Models” folder and choose the Add->New Item menu command. Within the “Add New Item” dialog we’ll select the “Class” template and name the file “DinnerRepository.cs”:
We can then implement our DinnerRespository class using the code below:
[!code-csharpMain]
1: public class DinnerRepository {
2:
3: private NerdDinnerDataContext db = new NerdDinnerDataContext();
4:
5: //
6: // Query Methods
7:
8: public IQueryable<Dinner> FindAllDinners() {
9: return db.Dinners;
10: }
11:
12: public IQueryable<Dinner> FindUpcomingDinners() {
13: return from dinner in db.Dinners
14: where dinner.EventDate > DateTime.Now
15: orderby dinner.EventDate
16: select dinner;
17: }
18:
19: public Dinner GetDinner(int id) {
20: return db.Dinners.SingleOrDefault(d => d.DinnerID == id);
21: }
22:
23: //
24: // Insert/Delete Methods
25:
26: public void Add(Dinner dinner) {
27: db.Dinners.InsertOnSubmit(dinner);
28: }
29:
30: public void Delete(Dinner dinner) {
31: db.RSVPs.DeleteAllOnSubmit(dinner.RSVPs);
32: db.Dinners.DeleteOnSubmit(dinner);
33: }
34:
35: //
36: // Persistence
37:
38: public void Save() {
39: db.SubmitChanges();
40: }
41: }
Retrieving, Updating, Inserting and Deleting using the DinnerRepository class
Now that we’ve created our DinnerRepository class, let’s look at a few code examples that demonstrate common tasks we can do with it:
Querying Examples
The code below retrieves a single Dinner using the DinnerID value:
[!code-csharpMain]
1: DinnerRepository dinnerRepository = new DinnerRepository();
2:
3: // Retrieve specific dinner by its DinnerID
4: Dinner dinner = dinnerRepository.GetDinner(5);
The code below retrieves all upcoming dinners and loops over them:
[!code-csharpMain]
1: DinnerRepository dinnerRepository = new DinnerRepository();
2:
3: // Retrieve all upcoming Dinners
4: var upcomingDinners = dinnerRepository.FindUpcomingDinners();
5:
6: // Loop over each upcoming Dinner and print out its Title
7: foreach (Dinner dinner in upcomingDinners) {
8: Response.Write("Title" + dinner.Title);
9: }
Insert and Update Examples
The code below demonstrates adding two new dinners. Additions/modifications to the repository aren’t committed to the database until the “Save()” method is called on it. LINQ to SQL automatically wraps all changes in a database transaction – so either all changes happen or none of them do when our repository saves:
[!code-csharpMain]
1: DinnerRepository dinnerRepository = new DinnerRepository();
2:
3: // Create First Dinner
4: Dinner newDinner1 = new Dinner();
5: newDinner1.Title = "Dinner with Scott";
6: newDinner1.HostedBy = "ScotGu";
7: newDinner1.ContactPhone = "425-703-8072";
8:
9: // Create Second Dinner
10: Dinner newDinner2 = new Dinner();
11: newDinner2.Title = "Dinner with Bill";
12: newDinner2.HostedBy = "BillG";
13: newDinner2.ContactPhone = "425-555-5151";
14:
15: // Add Dinners to Repository
16: dinnerRepository.Add(newDinner1);
17: dinnerRepository.Add(newDinner2);
18:
19: // Persist Changes
20: dinnerRepository.Save();
The code below retrieves an existing Dinner object, and modifies two properties on it. The changes are committed back to the database when the “Save()” method is called on our repository:
[!code-csharpMain]
1: DinnerRepository dinnerRepository = new DinnerRepository();
2:
3: // Retrieve specific dinner by its DinnerID
4: Dinner dinner = dinnerRepository.GetDinner(5);
5:
6: // Update Dinner properties
7: dinner.Title = "Update Title";
8: dinner.HostedBy = "New Owner";
9:
10: // Persist changes
11: dinnerRepository.Save();
The code below retrieves a dinner and then adds an RSVP to it. It does this using the RSVPs collection on the Dinner object that LINQ to SQL created for us (because there is a primary-key/foreign-key relationship between the two in the database). This change is persisted back to the database as a new RSVP table row when the “Save()” method is called on the repository:
[!code-csharpMain]
1: DinnerRepository dinnerRepository = new DinnerRepository();
2:
3: // Retrieve specific dinner by its DinnerID
4: Dinner dinner = dinnerRepository.GetDinner(5);
5:
6: // Create a new RSVP object
7: RSVP myRSVP = new RSVP();
8: myRSVP.AttendeeName = "ScottGu";
9:
10: // Add RSVP to Dinner's RSVP Collection
11: dinner.RSVPs.Add(myRSVP);
12:
13: // Persist changes
14: dinnerRepository.Save();
Delete Example
The code below retrieves an existing Dinner object, and then marks it to be deleted. When the “Save()” method is called on the repository it will commit the delete back to the database:
[!code-csharpMain]
1: DinnerRepository dinnerRepository = new DinnerRepository();
2:
3: // Retrieve specific dinner by its DinnerID
4: Dinner dinner = dinnerRepository.GetDinner(5);
5:
6: // Mark dinner to be deleted
7: dinnerRepository.Delete(dinner);
8:
9: // Persist changes
10: dinnerRepository.Save();
Integrating Validation and Business Rule Logic with Model Classes
Integrating validation and business rule logic is a key part of any application that works with data.
Schema Validation
When model classes are defined using the LINQ to SQL designer, the datatypes of the properties in the data model classes correspond to the datatypes of the database table. For example: if the “EventDate” column in the Dinners table is a “datetime”, the data model class created by LINQ to SQL will be of type “DateTime” (which is a built-in .NET datatype). This means you will get compile errors if you attempt to assign an integer or boolean to it from code, and it will raise an error automatically if you attempt to implicitly convert a non-valid string type to it at runtime.
LINQ to SQL will also automatically handles escaping SQL values for you when using strings - which helps protect you against SQL injection attacks when using it.
Validation and Business Rule Logic
Schema validation is useful as a first step, but is rarely sufficient. Most real-world scenarios require the ability to specify richer validation logic that can span multiple properties, execute code, and often have awareness of a model’s state (for example: is it being created /updated/deleted, or within a domain-specific state like “archived”). There are a variety of different patterns and frameworks that can be used to define and apply validation rules to model classes, and there are several .NET based frameworks out there that can be used to help with this. You can use pretty much any of them within ASP.NET MVC applications.
For the purposes of our NerdDinner application, we’ll use a relatively simple and straight-forward pattern where we expose an IsValid property and a GetRuleViolations() method on our Dinner model object. The IsValid property will return true or false depending on whether the validation and business rules are all valid. The GetRuleViolations() method will return a list of any rule errors.
We’ll implement IsValid and GetRuleViolations() for our Dinner model by adding a “partial class” to our project. Partial classes can be used to add methods/properties/events to classes maintained by a VS designer (like the Dinner class generated by the LINQ to SQL designer) and help avoid the tool from messing with our code. We can add a new partial class to our project by right-clicking on the folder, and then select the “Add New Item” menu command. We can then choose the “Class” template within the “Add New Item” dialog and name it Dinner.cs.
Clicking the “Add” button will add a Dinner.cs file to our project and open it within the IDE. We can then implement a basic rule/validation enforcement framework using the below code:
[!code-csharpMain]
1: public partial class Dinner {
2:
3: public bool IsValid {
4: get { return (GetRuleViolations().Count() == 0); }
5: }
6:
7: public IEnumerable<RuleViolation> GetRuleViolations() {
8: yield break;
9: }
10:
11: partial void OnValidate(ChangeAction action) {
12: if (!IsValid)
13: throw new ApplicationException("Rule violations prevent saving");
14: }
15: }
16:
17: public class RuleViolation {
18:
19: public string ErrorMessage { get; private set; }
20: public string PropertyName { get; private set; }
21:
22: public RuleViolation(string errorMessage, string propertyName) {
23: ErrorMessage = errorMessage;
24: PropertyName = propertyName;
25: }
26: }
A few notes about the above code:
- The Dinner class is prefaced with a “partial” keyword – which means the code contained within it will be combined with the class generated/maintained by the LINQ to SQL designer and compiled into a single class.
- The RuleViolation class is a helper class we’ll add to the project that allows us to provide more details about a rule violation.
- The Dinner.GetRuleViolations() method causes our validation and business rules to be evaluated (we’ll implement them shortly). It then returns back a sequence of RuleViolation objects that provide more details about any rule errors.
- The Dinner.IsValid property provides a convenient helper property that indicates whether the Dinner object has any active RuleViolations. It can be proactively checked by a developer using the Dinner object at anytime (and does not raise an exception).
- The Dinner.OnValidate() partial method is a hook that LINQ to SQL provides that allows us to be notified anytime the Dinner object is about to be persisted within the database. Our OnValidate() implementation above ensures that the Dinner has no RuleViolations before it is saved. If it is in an invalid state it raises an exception, which will cause LINQ to SQL to abort the transaction.
This approach provides a simple framework that we can integrate validation and business rules into. For now let’s add the below rules to our GetRuleViolations() method:
[!code-csharpMain]
1: public IEnumerable<RuleViolation> GetRuleViolations() {
2:
3: if (String.IsNullOrEmpty(Title))
4: yield return new RuleViolation("Title required","Title");
5:
6: if (String.IsNullOrEmpty(Description))
7: yield return new RuleViolation("Description required","Description");
8:
9: if (String.IsNullOrEmpty(HostedBy))
10: yield return new RuleViolation("HostedBy required", "HostedBy");
11:
12: if (String.IsNullOrEmpty(Address))
13: yield return new RuleViolation("Address required", "Address");
14:
15: if (String.IsNullOrEmpty(Country))
16: yield return new RuleViolation("Country required", "Country");
17:
18: if (String.IsNullOrEmpty(ContactPhone))
19: yield return new RuleViolation("Phone# required", "ContactPhone");
20:
21: if (!PhoneValidator.IsValidNumber(ContactPhone, Country))
22: yield return new RuleViolation("Phone# does not match country", "ContactPhone");
23:
24: yield break;
25: }
We are using the “yield return” feature of C# to return a sequence of any RuleViolations. The first six rule checks above simply enforce that string properties on our Dinner cannot be null or empty. The last rule is a little more interesting, and calls a PhoneValidator.IsValidNumber() helper method that we can add to our project to verify that the ContactPhone number format matches the Dinner’s country.
We can use .NET’s regular expression support to implement this phone validation support. Below is a simple PhoneValidator implementation that we can add to our project that enables us to add country-specific Regex pattern checks:
[!code-csharpMain]
1: public class PhoneValidator {
2:
3: static IDictionary<string, Regex> countryRegex = new Dictionary<string, Regex>() {
4: { "USA", new Regex("^[2-9]\\d{2}-\\d{3}-\\d{4}$")},
5: { "UK", new Regex("(^1300\\d{6}$)|(^1800|1900|1902\\d{6}$)|(^0[2|3|7|8]{1}[0-9]{8}$)|(^13\\d{4}$)|(^04\\d{2,3}\\d{6}$)")},
6: { "Netherlands", new Regex("(^\\+[0-9]{2}|^\\+[0-9]{2}\\(0\\)|^\\(\\+[0-9]{2}\\)\\(0\\)|^00[0-9]{2}|^0)([0-9]{9}$|[0-9\\-\\s]{10}$)")},
7: };
8:
9: public static bool IsValidNumber(string phoneNumber, string country) {
10:
11: if (country != null && countryRegex.ContainsKey(country))
12: return countryRegex[country].IsMatch(phoneNumber);
13: else
14: return false;
15: }
16:
17: public static IEnumerable<string> Countries {
18: get {
19: return countryRegex.Keys;
20: }
21: }
22: }
Handling Validation and Business Logic Violations
Now that we’ve added the above validation and business rule code, any time we try to create or update a Dinner, our validation logic rules will be evaluated and enforced.
Developers can write code like below to proactively determine if a Dinner object is valid, and retrieve a list of all violations in it without raising any exceptions:
[!code-csharpMain]
1: Dinner dinner = dinnerRepository.GetDinner(5);
2:
3: dinner.Country = "USA";
4: dinner.ContactPhone = "425-555-BOGUS";
5:
6: if (!dinner.IsValid) {
7:
8: var errors = dinner.GetRuleViolations();
9:
10: // do something to fix the errors
11: }
If we attempt to save a Dinner in an invalid state, an exception will be raised when we call the Save() method on the DinnerRepository. This occurs because LINQ to SQL automatically calls our Dinner.OnValidate() partial method before it saves the Dinner’s changes, and we added code to Dinner.OnValidate() to raise an exception if any rule violations exist in the Dinner. We can catch this exception and reactively retrieve a list of the violations to fix:
[!code-csharpMain]
1: Dinner dinner = dinnerRepository.GetDinner(5);
2:
3: try {
4:
5: dinner.Country = "USA";
6: dinner.ContactPhone = "425-555-BOGUS";
7:
8: dinnerRepository.Save();
9: }
10: catch {
11:
12: var errors = dinner.GetRuleViolations();
13:
14: // do something to fix errors
15: }
Because our validation and business rules are implemented within our model layer, and not within the UI layer, they will be applied and used across all scenarios within our application. We can later change or add business rules and have all code that works with our Dinner objects honor them.
Having the flexibility to change business rules in one place, without having these changes ripple throughout the application and UI logic, is a sign of a well-written application, and a benefit that an MVC framework helps encourage.
Next Step
We’ve now got a model that we can use to both query and update our database.
Let’s now add some controllers and views to the project that we can use to build an HTML UI experience around it.
|