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.
The New ASP.NET MVC 4 Project window is shown. Then:
- From this window select "Internet Application".
- Click on the "OK" button.
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.
.content {
clear: both;
width: 90%;
}
li {
list-style-type: none;
}
#products li {
width: 300px;
background-color: #aaf;
font-size: 1.5em;
font-weight: bold;
color: #ff0;
margin: 0 0 5px 0;
padding: 0 5px 0 5px;
}
.price {
float: right;
color: #c00;
font-size: 0.75em;
}
.details thead td {
background-color: #CCCCCC;
color: #333333;
}
.details td {
padding: 6px;
}
.details td.qty {
text-align: center;
}
#cart a {
color: Blue;
font-size: .75em;
}
#update-products li {
padding: 5px;
color: #666;
border-style: dashed;
border-width: 2px;
border-color: #666;
}
#update-products li .item {
width: 120px;
display: inline-block;
text-align: right;
}
We create the three model classes.
- In the Solution Explorer.
- Right-click on the "model folder".
- Select "Add" -> "Class".
Select the class and change its name.
Add the class "Material" class. In this class we write this code:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
namespace MaterialStore.Models
{
public class Material
{
[ScaffoldColumn(false)]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public decimal Cost { get; set; }
public decimal ActualCost { get; set; }
}
}
Add the second "Order" class. We write this code in this class:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace MaterialStore.Models
{
public class Order
{
public int Id { get; set; }
[Required]
public string Employee { get; set; }
// Navigation property
public ICollection<OrderRecord> OrderRecords { get; set; }
}
}
Add the third class "OrderRecord". We write this code in this class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MaterialStore.Models
{
public class OrderRecord
{
public int Id { get; set; }
public int Quantity { get; set; }
public int OrderId { get; set; }
public int MaterialId { get; set; }
// Navigation properties
public Material Material{ get; set; }
public Order Order { get; set; }
}
}
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.
- Select the tables and it shows the relation among the tables.
Relatioship among tables.
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:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web.Http;
namespace MaterialStore
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling =
Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);
}
}
}