Shopping Cart Application Part-11 (ViewModels)

By | April 17, 2015

Before we ditails discuss about ViewModels we’ll discuss about the role of Service layer in our application.
@. In our project the service layer will coordinate the retrieval and persistence of business entities.
@. A service receives requests from the controllers for updates to the domain model as well as a specific view of the domain.

@. In that case:

+. The controller receives the initial request from client and calls the appropriate service method(s)”. In this example, controller receive the request is for homepage of the site.

+.The service then contacts repository to obtain a collection of Product domain entities.

+. After obtaining converts those entities into a collection of “ProductViews” and returns to the controller

+.The controller receives the collection of ProductViews and all other necessary information to display on the web page, such as a basket summary or customer name etc.

Note:
+.In our project we’ll use all the views are based on a strongly typed ViewModel.

@.Now we’ll create all the ViewModel’s required for Product Catalog. Before creting ViewModel’s we’ll shortly discuss about ViewModel.

 What is ViewModel?
 In ASP.Net MVC ViewModel are the model that uses to represent the view i.e. ViewModel is a class optimized for use with a specific view template. ViewModels allow you to shape multiple entities from one or more data models or sources into a single object, optimized for consumption and rendering by the view.

ViewModel also helps to detach the view from the underlying domain model and can be a lot more flexible than using real domain entities. Jonathan Allen wrote a nice blog about ViewModel at here

Following figure shows the basic concept about ViewModel

ViewModel

Viewmodels for Product Catalog:

 @. Now we’ll create some viewmodels that’s are required for our Product Catalog.

@. Create a new folder in the Services project named ViewModels.

@. Add to it a new class named ProductSizeOption with the following code listing:

public class ProductSizeOption
{
    public int Id { get; set; }
    public string SizeName { get; set; }
} 

+. This view represents the physical size of the product that can buy.

@. Next create another view model and named it ProductView with following definition:

public class ProductView
{
     public int Id { get; set; }
     public string BrandName { get; set; }
     public string Name { get; set; }
     public string Price { get; set; }
     public IEnumerable Products { get; set; }
}

+. Above view model contains the collection of ProductSizeOptions that will be used to display size wise the product details:

@. Our next view model will use to display summary information of group of product list. So add another class and name it to ProductSummaryView with following code definition:

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

@. Then we’ll create another view model to represent all product category objects. It will also be uses for category navigation. So add new class and named it CategoryView with following definition:

public class CategoryView
{
     public int Id { get; set; }
     public string Name { get; set; }
} 

@. The next three views are

+. RefinementGroupings,

+. Refinement,

+. RefinementGroup

@. Above views are utilized in the product category refinement function, which allows customers to refine the results of viewing all products within a particular category:

public enum RefinementGroupings
{
    brand = 1,
    size = 2,
    color = 3
} 
public class Refinement
{
     public int Id { get; set; }
     public string Name { get; set; }
} 
public class RefinementGroup
{
      public string Name { get; set; }
      public int GroupId { get; set; }
      public IEnumerable Refinements { get; set; }
} 

@. We have added all the view models that are required for the product catalog pages.

@. Next we will create the ProductCatalogService class that the controllers will use to obtain the view models to power the product catalog browsing experience

One can download entire source from here

581 Total Views 1 Views Today
Md. Mojammel Haque

CSM, CSPO, CSD, CSP-SM, CSP-PO (ScrumAlliance.org)
Certification Profile Link-
https://www.scrumalliance.org/community/profile/mhaque13

Currently working as Lead Team (Application Architecture) at Raven Systems Ltd. Passion for software development especially agile practices such as TDD with in depth knowledge of Object Oriented Programming, SOLID Principles, Gang of Four Design Patterns, Some Enterprise Application Architectural Patterns. Over 8 years of software development experience ASP.NET. Has the ability to understand and transform complex business requirements into software ensuring applications are delivered on time. Also experience in non Microsoft .NET technologies such as Dapper.Net, Git, Structure Map & Angular, Bootstrap, HTML-5, CSS-3 etc.

Category: Asp.net MVC Learn by doing - ASP.Net MVC Tags: , , , ,
Md. Mojammel Haque

About Md. Mojammel Haque

CSM, CSPO, CSD, CSP-SM, CSP-PO (ScrumAlliance.org) Certification Profile Link- https://www.scrumalliance.org/community/profile/mhaque13 Currently working as Lead Team (Application Architecture) at Raven Systems Ltd. Passion for software development especially agile practices such as TDD with in depth knowledge of Object Oriented Programming, SOLID Principles, Gang of Four Design Patterns, Some Enterprise Application Architectural Patterns. Over 8 years of software development experience ASP.NET. Has the ability to understand and transform complex business requirements into software ensuring applications are delivered on time. Also experience in non Microsoft .NET technologies such as Dapper.Net, Git, Structure Map & Angular, Bootstrap, HTML-5, CSS-3 etc.

Leave a Reply

Your email address will not be published. Required fields are marked *