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:

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

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

  1. .content {
  2. clear: both;
  3. width: 90%;
  4. }
  5. li {
  6. list-style-type: none;
  7. }
  8. #products li {
  9. width: 300px;
  10. background-color: #aaf;
  11. font-size: 1.5em;
  12. font-weight: bold;
  13. color: #ff0;
  14. margin: 0 0 5px 0;
  15. padding: 0 5px 0 5px;
  16. }
  17. .price {
  18. float: right;
  19. color: #c00;
  20. font-size: 0.75em;
  21. }
  22. .details thead td {
  23. background-color: #CCCCCC;
  24. color: #333333;
  25. }
  26. .details td {
  27. padding: 6px;
  28. }
  29. .details td.qty {
  30. text-align: center;
  31. }
  32. #cart a {
  33. color: Blue;
  34. font-size: .75em;
  35. }
  36. #update-products li {
  37. padding: 5px;
  38. color: #666;
  39. border-style: dashed;
  40. border-width: 2px;
  41. border-color: #666;
  42. }
  43. #update-products li .item {
  44. width: 120px;
  45. display: inline-block;
  46. text-align: right;
  47. }

We create the three model classes.
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 { get; set; }
  12. [Required]
  13. public string Name { get; set; }
  14. public decimal Cost { get; set; }
  15. public decimal ActualCost { get; set; }
  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 { get; set; }
  11. [Required]
  12. public string Employee { get; set; }
  13. // Navigation property
  14. public ICollection<OrderRecord> OrderRecords { get; set; }
  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 { get; set; }
  10. public int Quantity { get; set; }
  11. public int OrderId { get; set; }
  12. public int MaterialId { get; set; }
  13. // Navigation properties
  14. public Material Material{ get; set; }
  15. public Order Order { get; set; }
  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.

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.

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. }