Separating Your Concerns- Antidote to Smart UI Part 4

Now lets start building Service layer


  • The role of Service layer is to act as entry point in to application..sometimes it is known as a FACADE
  • Service layer provides the Presentation Layer with a strongly Typed View model, sometimes called Presentation Model
  • Add a new class to ASPPatterns.Chap3.Layered.Service project named ProductViewModel
    public class ProductViewModel
            public int ProductId { get; set; }
            public string Name { get; set; }
            public string  RRP { get; set; }
            public string SellingPrice { get; set; }
            public String Discount { get; set; }
            public string Savings { get; set; }
  • Client will be interacting with Service layer and for this we will use REQUEST-RESPONSE Messaging Pattern
  • From client we will be getting a Request to Service and Request will be of CustomerType. So create a Request based on CustomerType
  •  public class ProductListRequestcs
            public CustomerType CustomerType { get; set; }
  • Response from Service would be of ProductViewModel along with Success/Failure message..
     public class ProductListResponse
            public bool Success { get; set; }
            public string Message { get; set; }
            public IList<ProductViewModel> Products { get; set; }
  • If you observe in Response we are sending ProductViewModel as Response.. so we need to have a mechanism to convert Products in to ProductViewModels.. so lets build this mechanism..
  • Below class is an extension method to Products, which will Transform Products to ProductViewModels
  •   public static IList<ProductViewModel> ConvertToProductListViewModel(this IList<Model.Product> products)
                IList<ProductViewModel> productViewModels = new List<ProductViewModel>();
                foreach (var product in products)
                return productViewModels;
  • The below class is an extension method to Product , which will convert Product to ProductViewModel
  •  public static ProductViewModel ConvertToProductViewModel(this Model.Product product)
                ProductViewModel productViewModel = new ProductViewModel();
                productViewModel.ProductId = product.Id;
                productViewModel.Name = product.Name;
                productViewModel.RRP = string.Format("{0:C}", product.Price.RRP);
                productViewModel.SellingPrice = string.Format("{0:C}", product.Price.SellingPrice);
                if(product.Price.Discount > 0)
                    productViewModel.Discount = string.Format("{0:C}", product.Price.Discount);
                if (product.Price.Savings > 0 && product.Price.Savings<1)
                    productViewModel.Savings = product.Price.Savings.ToString("#%");
                return productViewModel;

    Now if you remember we have yet to build our domain… lets start Building Domain Domain

    Domain Layer

    Gear Up!!!!!… we are going to implement some Patterns here…

    See the below diagram…

  • We have Product Class and Product Class contains price class which will implement Discount Strategy
  • What is the need of Strategy Pattern in our implementation..??
    Strategy Pattern enables algorithms to be selected and changed at runtime and there is a need for our Price object to display Discount based on Customer Type. so we are going to apply Discount Algorithms to Price Object.

    Lets implement Strategy Pattern Discount Strategy

  • Create an Interface IDiscountStrategy in ASPPatterns.Chap3.Layered.Model
  • public interface IDiscountStrategy
            decimal ApplyExternalDiscountsTo(decimal originalSalesPrice);
  • Now we have created Interface IDiscountStrategy , lets add two implementations of Discount Strategy TradeDiscountStrategy and NullDiscountStrategy
  • Trade discount strategy will apply discount to original sales price..see below implementation
    public class TradeDiscountStrategy : IDiscountStrategy
            public decimal ApplyExternalDiscountsTo(decimal originalSalesPrice)
                decimal price = originalSalesPrice;
                price = price * .095M;
                return price;
  • If you observe below implementation of NullDiscountStrategy, we are employing Null Object Pattern here
  •     public class NullDiscountStrategy : IDiscountStrategy
            public decimal ApplyExternalDiscountsTo(decimal originalSalesPrice)
                return originalSalesPrice;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s