简体   繁体   中英

Add multiple classes data in single class using SOLID design pattern

I have Product , Item1 and Item2 classes.The goal is to consolidate all of the Items in product class into a single list so that they can be sorted and displayed on the client's website.I want this in C# using SOLID principle

public class Product
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
        public string Type { get; set; }
    }
and here are list of datas are in this class
-- var list1 = new List<Item1>();
public class Item1
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
    }
-- var list2 = new List<Item2>();
public class Item2
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
    }

Any help would be appreciated.

Use a ViewModel to handle your display concerns. This respects the single responsibility principle. The responsibilities of the ViewModel are displaying data and validating <form> inputs filled in by the user (if any). The data to be displayed can be aggregated from many sources.

Use a mapping between your persisted entities/ domain entities/ data transfer objects and your ViewModel. Define a mapping from each source class to your ViewModel. Using a library such as AutoMapper for this is highly recommended.

For Example:

public class ProductDetailsViewModel {
    // Display attribute as an example of a view concern
    [Display(Name = "ProductDetails_Name", ResourceType = typeof(Resources))]
    public string Name {get; set;}
}

public class ProductListViewModel {
    public IEnumerable<ProductDetailsViewModel> ProductDetails {get; set;}
}


// Example of mapping by hand:
var item1 = new Item1();
var item2 = new Item2();
var vm1 = new ProductDetailsViewModel { Name = item1.Name };
var vm2 = new ProductDetailsViewModel { Name = item2.Name };
var productList = new ProductListViewModel {ProductDetails = new [] {vm1, vm2}}; 

Assuming you want to override the values of product as well as have a list of children Items, I would suggest that you declare values you intend to override as virtual, and make another class available that can also take a list of items as required, while inheriting from the parent product table.

    public class Product
    {
        public virtual Guid Id { get; set; }
        public virtual string Name { get; set; }
        public virtual string Type { get; set; }
    }

    public class ProductFull : Product
    {
        public override Guid Id { get; set; }
        public override string Name { get; set; }
        public override string Type { get; set; }

        public List<Item1> Item1s { get; set; }

        public List<Item2> Item2s { get; set; }
    }

    public class Item1
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
    }

    public class Item2
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
    }

From MSDN:

Virtual properties behave like abstract methods, except for the differences in declaration and invocation syntax. It is an error to use the virtual modifier on a static property. A virtual inherited property can be overridden in a derived class by including a property declaration that uses the override modifier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM