Shopping Cart Application Part-06 (Defining Product Catalog)

By | April 10, 2015

@.We already completed our Infrastructure settings
@. The most vital part of a Shopping Cart application is Product.
@. Now we’ll work with Product catalog part of our application.
@. This part of the domain is fairly anemic with little business logic.
@. This part contains following classes

@@. ProductTilte : Represents the name of a product
@@.Brand: Represents a products brand
@@. Color:
Represents a products color
@@.Category:
Represents the category of a product
@@.Product: Represents the physical product that a user can add to his Basket including the size as well as the reference to its ProductTitle.
@@. Size: Represents the Size of a product.

@. Our entire product catalog models will be placed inside Shoppingcart.Models project. Inside that project we’ll have two folders for Product catalog classes. So add two new folder named


+. Categories
+. Products

@. Our users will be able to refine list of categories by attributes.
@. So create an interface named IProductAttribute with following definition:

namespace Shoppingcart.Models
{
    public interface IProductAttribute
    {
        int Id { get; set; }
        string Name { get; set; }
    }
}

*. All our product attributes will implement above interface.

Brand Class

@. Now define a class for Brand.
@. Brand class will placed inside Products folder.
@. Brand is one of the product attribute.
@. So it will implement IProductAttribute interface.
@. Brand class will contain following definition:

namespace Shoppingcart.Models
{
    public class Brand : BaseEntity, IProductAttribute
    {
        public Brand()
        {
        }
    
        public string Name { get; set; }
    
        protected override void Validate()
        {
            throw new NotImplementedException();
        }
    }
}

Though we’ll not implement Save, Update, Delete for Brand so we don’t need to add any Validation.
We don’t need Brand anymore outside of Product Aggregate . So it would not require any Repository. For that reason we didn’t marked as IAggregateRoot i.e. we didn’t inherit this entity by IAggregateRoot

Category Class

@. The next attribute of product is Category
@. So add a new class named Category inside Categories folder with following definition:

namespace Shoppingcart.Models
{
    
    public class Category: BaseEntity, IAggregateRoot, IProductAttribute
    {
        public Category()
        {

        }
    
        public string Name { get; set; }
    
        protected override void Validate()
        {
            throw new NotImplementedException();
        }
    }
}

Category is an aggregate root. So it is inherited from IAggregateRoot interface.
Though Category is inherited by IAggregateRoot, so we will be able to create Repository for this class. Though we will create the repository for Category, so category available outside of Product Aggregate
We’ll not implement Save, Update, Delete for Category so we don’t need to add any Validation.

Size Class

@. The next attribute is Size.
@. Inside Products folder add new class named Size
@. Same as Brand entity it wouldn’t be available outside of product aggregation. So it would not require any Repository. So it wouldn’t mark as aggregate root.
@. The definition of this class is given below:

public class Size : BaseEntity,  IProductAttribute
{
        public Size()
        {
        }
    
        public string Name { get; set; }
    
        protected override void Validate()
        {
            throw new NotImplementedException();
        }
 }

Though we’ll not implement Save, Update, Delete for Size, so we don’t need to add any Validation.
We don’t need Size anymore outside of Product Aggregate . So it would not require any Repository. For that reason didn’t marked as IAggregateRoot i.e. we didn’t inherit this entity by IAggregateRoot

Colour Class

@.Our last attribute is Colour.
@. Add new class inside Products folder with name Colour that will have following definition:

public class Colour: BaseEntity,  IProductAttribute
{
        public Colour()
        {

        }
        public string Name { get; set; }
    
        
	protected override void Validate()
        {
            throw new NotImplementedException();
        }
}

Same as Brand and Size it wouldn’t be available outside of Product Aggregate. So it would not require any Repository. So we didn’t mark it as IAggregateRoot
We also wouldn’t perform Save, Update, Delete from Colour. So no need to implement validation.

ProductTitle Class

@. Now we’ll define ProductTitle class
@. The definition of ProductTitle class is given below:

public class ProductTitle : BaseEntity, IAggregateRoot
{
        public ProductTitle()
        {
        }
    
        public string ProductName { get; set; }
        public int ColourId { get; set; }
        public int BrandId { get; set; }
        public int CategoryId { get; set; }
        public decimal Price { get; set; }
    
        protected override void Validate()
        {
            throw new NotImplementedException();
        }
    }

@Our above defined entities will have relationship between each other.
@. Now we’ll map these relations by using navigation property:

i). Create relationship between Brand and ProductTitle:
@@. A Brand can include many ProductTitle.
@@. So the relationship would be:

Define single navigation property i.e. one entity of Brand inside ProductTitle class as follows:

//Single navigation property 
public virtual Brand Brand { get; set; }

Define Collection of Navigation Properties of ProductTitle class inside Brand class as follows:

public virtual ICollection ProductTitles { get; set; }

ii). Create relationship between Category and ProductTitle:
@@. A category can include Many ProductTitle.
@@. So the relationship would be:

Define single navigation property i.e. one entity of Category inside ProductTitle class as follows:

//Single navigation property 
public virtual Category Category { get; set; }

Define Collection of Navigation Properties of ProductTitle class inside Category class as follows:

public virtual ICollection ProductTitles { get; set; }

iii). Create relationship between Colour and ProductTitle:
@@. A Colour can include Many ProductTitle.
@@. So the relationship would be:
Define single navigation property i.e. one entity of Colour inside ProductTitle class as follows:

//Single navigation property 
public virtual Colour Colour { get; set; }

Define Collection of Navigation Properties of ProductTitle class inside Colour class as follows:

public virtual ICollection ProductTitles { get; set; }

Product Class

@. Our Last class is Product.
@. Add new class named Product that’s definition will be as follows:

public class Product : BaseEntity, IAggregateRoot
{
public int ProductTitleId{ get; set; }
public Nullable SizeId { get; set; }

       
        protected override void Validate()
        {
            throw new NotImplementedException();
        }
  }

@.Now we’ll manage the entity relationship between ProductTitle, Product and Size classes

i). Create relationship between ProductTitle and Product:
@@. A ProductTitle can include many Product.
@@. So the relationship would be:

Define single navigation property i.e. one entity of ProductTitle inside Product class as follows:

//Single navigation property 
public virtual ProductTitle ProductTitle { get; set; }

Define Collection of Navigation Properties of Product class inside ProductTitle class as follows:

public virtual ICollection Products { get; set; }

ii). Create relationship between Product and Size:
@@. A Size can include many Product.
@@. So the relationship would be:
Define single navigation property i.e. one entity of Size inside Product class as follows:

//Single navigation property 
public virtual Size Size { get; set; }

Define Collection of Navigation Properties of Product class inside Size class as follows:

public virtual ICollection Products { get; set; }

@. So, we have completed mapping relationship between entities.
@. Now our entity class definition would be as follows:

public class Brand : BaseEntity, IProductAttribute
{
        public Brand()
        {
            this.ProductTitles = new HashSet();
        }
    
        public string Name { get; set; }
    
        public virtual ICollection ProductTitles { get; set; }

        protected override void Validate()
        {
            throw new NotImplementedException();
        }
}
public class Category : BaseEntity, IAggregateRoot, IProductAttribute
    {
        public Category()
        {
            this.ProductTitles = new HashSet();
        }

        public string Name { get; set; }

        public virtual ICollection ProductTitles { get; set; }

        protected override void Validate()
        {
            throw new NotImplementedException();
        }
    }
 
public class Size : BaseEntity,  IProductAttribute
    {
        public Size()
        {
            this.Products = new HashSet();
        }
    
        public string Name { get; set; }
    
        public virtual ICollection Products { get; set; }

        protected override void Validate()
        {
            throw new NotImplementedException();
        }
    }
public class Colour: BaseEntity,  IProductAttribute
    {
        public Colour()
        {
            this.ProductTitles = new HashSet();
        }
        public string Name { get; set; }
    
        public virtual ICollection ProductTitles { get; set; }

        protected override void Validate()
        {
            throw new NotImplementedException();
        }
    }
public class ProductTitle : BaseEntity, IAggregateRoot
    {
        public ProductTitle()
        {
            this.Products = new HashSet();
        }
    
        public string ProductName { get; set; }
        public int ColourId { get; set; }
        public int BrandId { get; set; }
        public int CategoryId { get; set; }
        public decimal Price { get; set; }
    
        public virtual Brand Brand { get; set; }
        public virtual Category Category { get; set; }
        public virtual Colour Colour { get; set; }
        public virtual ICollection Products { get; set; }

        protected override void Validate()
        {
            throw new NotImplementedException();
        }
    }
 
public class Product : BaseEntity, IAggregateRoot
    {

        public string Name
        {
            get { return ProductTitle.ProductName; }
        }

        public Decimal Price
        {
            get { return ProductTitle.Price; }
        }
        public Brand Brand
        {
            get { return ProductTitle.Brand; }
        }
        public Colour Colour
        {
            get { return ProductTitle.Colour; }
        }

        public Category Category
        {
            get { return ProductTitle.Category; }
        }
        public Nullable SizeId { get; set; }
        public int ProductTitleId { get; set; }
    
        public virtual ProductTitle ProductTitle { get; set; }
        public virtual Size Size { get; set; }

        protected override void Validate()
        {
            throw new NotImplementedException();
        }
    }

One can download entire source from here

486 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 *