Creating The Domain Model in ASP.Net Web API

Introduction

This article explains how to create the Domain Model in ASP. NET Web API. A Domain Model identifies the relationship among the entities that are in the scope of the problem domain. It describes the various entities that are attributes, roles and relationships that govern the problem domain.

Now I describe the procedure for creating the Domain Model in the ASP.NET Web API.

First we create the MVC4 Application using the following:

  • Start Visual Studio 2012.
  • From the Start window select "New project".
  • On the template window select "Installed" -> "Visual C#"->"Web" ->"ASP.NET MVC4 Application".
  • Click on the "OK" button.

    m1.jpg

The New ASP.NET MVC 4 Project window is shown. Then:

  • From this window select "Internet Application".
  • Click on the "OK" button.

    m2.jpg

Now write this code in the "Site.css" file.

  • In the "Solution Explorer".
  • Expand the  "Content folder".
  • Select the "Site.css" file and write the code.

    m3.jpg 

  1. .content {  
  2.     clearboth;  
  3.     width90%;  
  4. }  
  5. li {  
  6.     list-style-typenone;  
  7. }  
  8. #products li {  
  9.     width300px;  
  10.     background-color#aaf;  
  11.     font-size1.5em;  
  12.     font-weightbold;  
  13.     color#ff0;  
  14.     margin0 0 5px 0;  
  15.     padding0 5px 0 5px;  
  16. }  
  17. .price  {  
  18.     floatright;  
  19.     color#c00;  
  20.     font-size0.75em;  
  21. }  
  22. .details thead td {  
  23.     background-color#CCCCCC;  
  24.     color#333333;  
  25. }  
  26. .details td {  
  27.        padding6px;  
  28. }  
  29. .details td.qty {  
  30.        text-aligncenter;  
  31. }  
  32. #cart a {  
  33.        color: Blue;  
  34.        font-size: .75em;  
  35. }  
  36. #update-products li {  
  37.     padding5px;  
  38.     color#666;  
  39.     border-styledashed;  
  40.     border-width2px;  
  41.     border-color#666;  
  42. }  
  43. #update-products li .item {  
  44.        width120px;  
  45.        display: inline-block;  
  46.        text-alignright;  
  47. }   

We create the three model classes.
  • In the Solution Explorer.
  • Right-click on the "model folder".
  • Select "Add" -> "Class".

    m4.jpg
Select the class and change its name.

m5.jpg

Add the class  "Material" class. In this class we write this code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel.DataAnnotations;  
  4. using System.Linq;  
  5. using System.Text;  
  6. namespace MaterialStore.Models  
  7. {  
  8.     public  class Material  
  9.     {  
  10.         [ScaffoldColumn(false)]  
  11.         public int Id { getset; }  
  12.         [Required]  
  13.         public string Name { getset; }  
  14.         public decimal Cost { getset; }  
  15.         public decimal ActualCost { getset; }  
  16.     }  
  17. } 

Add the second  "Order" class. We write this code in this class:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel.DataAnnotations;  
  4. using System.Linq;  
  5. using System.Web;  
  6. namespace MaterialStore.Models  
  7. {  
  8.     public class Order  
  9.     {  
  10.         public int Id { getset; }  
  11.         [Required]  
  12.         public string Employee { getset; }  
  13.         // Navigation property  
  14.         public ICollection<OrderRecord> OrderRecords { getset; }  
  15.     }  
  16. } 

Add the third class "OrderRecord". We write this code in this class:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. namespace MaterialStore.Models  
  6. {  
  7.     public class OrderRecord  
  8.     {  
  9.         public int Id { getset; }  
  10.         public int Quantity { getset; }  
  11.         public int OrderId { getset; }  
  12.         public int MaterialId { getset; }  
  13.         // Navigation properties  
  14.         public Material Material{ getset; }  
  15.         public Order Order { getset; }  
  16.     }  
  17. } 

Foreign Key relations

There are many order records but each order record contains a single Material. For representing the relation the "OrderRecord" class has the two fields orderId and MaterialId. There is we define the Foreign key constraints. We create the database and show the relationship among these tables. For representing the relationship use the following procedure.

  • Create a database.
  • Create three tables.
  • In the Object Explorer seelct "database" -> "Record" -> "Database diagram".
  • Right-click on the database diagram and display the new database diagram.

    dia.jpg

  • Select the tables and it shows the relation among the tables.

    dia2.jpg
  • Relationship among tables.

    dbdia.jpg

Configure the media type formatters

It is the object used for serializing the data at the time of writing the Web API the HTTP response body. The formatters support both JSON and XML. These two JSON and XML initialize the object by value. Here two classes "Order" and "OrederRecord" both have the references of the other. The formatters follow the references.

Now we write this code in the "WebApiConfig.cs" file.

  • In the "Solution Explorer"
  • Expand the folder"App_Start"
  • Open the file "WebApiConfig .cs"

Write this code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net.Http;  
  5. using System.Web.Http;  
  6. namespace MaterialStore  
  7. {  
  8.     public static class WebApiConfig  
  9.     {  
  10.         public static void Register(HttpConfiguration config)  
  11.         {  
  12.             config.Routes.MapHttpRoute(  
  13.                 name: "DefaultApi",  
  14.                 routeTemplate: "api/{controller}/{id}",  
  15.                 defaults: new { id = RouteParameter.Optional }  
  16.             );  
  17.             var json = config.Formatters.JsonFormatter;  
  18.             json.SerializerSettings.PreserveReferencesHandling =  
  19.                 Newtonsoft.Json.PreserveReferencesHandling.Objects;  
  20.             config.Formatters.Remove(config.Formatters.XmlFormatter);  
  21.         }  
  22.     }  
  23. }