Separating Your Concerns- Antidote to Smart UI Part 3

Now.. if you remember our use case.. we have products.. and price of the products will vary based on Type of Customer..

So to proceed further , let us create two objects Product and Customer(since customer will be a type..it should be an enum in our Domain model “ASPPatterns.Chap3.Layered.Model”.

Product.cs


public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public Price Price { get; set; }
    }

Since Price of the product has a behaviour now..it is going to vary by the Customer Type.. it should be an object.. so for now create an empty price class as shown below..we will see implementation of Price class later(why..?? since we are going to apply new patterns in this class..)

 public class Price
    {
    }

Now create CustomerType enum

 public enum CustomerType
    {
        Standard=0,
        Trade=1
    }

lets retro where we are heading…

Typical Three Layered Architecture

This is what we are going to do…

  • Presentation Layer : we are half way done here..if you remember in previous blog we have created our presentation layer
  • Business Logic Layer : we have only created basic infrastructure i.e we have created Product Class, defined price class and Customer class
  • Data Access Later Yet to start

    Note If you observe the above diagram we will be having two Service classes
    1. First one In Domain model..
    2. One in the service layer..
    Reason for this…??? since we are following Layered architecture… Responsibility of Service class in domain layer will be fetching data from Repository Later and Responsibility of Service class in Service Layer will be interacting with service class in Domian Layer and Giving Presentation Model to Presentation Layer..

    Lets code to get clarity…

    Now we will build repository layer/Data Access layer…

  • As with the Smart UI, you need a database to store the products. Create a database in the WebUI project with the same schema, name, and data that you used in the Smart UI Blog
  • Creating database

    We will be using Linq to SQL here…(this is the first time i am working with LinqtoSQL.. but it rocks…!! will try to post more about LinqToSQL after finishing this book)

  • Add a new Linq to SQL class to the ASPPatterns.Chap3.Layered.Repository project
    by right-clicking the project name and selecting Add ➪ New Item. Then select Linq to SQL Classes,and name the class Shop.dbml
  • With the Server Explorer window open, drag the Products table onto the design surface. Visual Studio creates a Linq to SQL entity named Product
  • Now What.. what is the use of Linq to SQL.. you will see now..

  • Create Interface IProductRepository in ASPPatterns.Chap3.Layered.Model Project..Why a Repository Interface in Model ..?? Domain Service Layer will interact with Data Layer to retrieve products. and we don’t want Model Project to be concerned with what kind of technologies will be used to query the data
  • public interface IProductRepository
        {
            IList<Product> FindAll();
        }
    
  • Now create Concrete Implementation of IProductRepository in ASPPatterns.Chap3.Layered.Repository project, by creating class ProductRepository
  •  public IList<Model.Product> FindAll()
            {
                var products = from p in new ShopDataContext().Products
                               select new Model.Product
                               {
                                   Id = p.ProductId,
                                   Name = p.ProductName,
                                   //Price = new Model.Price(p.RRP, p.SellingPrice) -- Price implmentation
                               };
    
                return products.ToList();
    
            }
    
  • Check out line 3 in the above code… since we have implemented LinqToSQl we can directly query over Product table… isn’t it cool
  • coming to line 8 in the above code.. we need to implement Price model..which we will do later and come back here again..
  • With this we have completed implementation of Data Layer … in the next blog we will implement Service layer

    Advertisements