我想知道是否有更好的方法来设计类似的数据结构。
object --> list of objects --> list of lists
代码示例如下:
class Customer
{
public string Name {get; set;}
public string LastName {get; set;}
}
class Customers
{
public List<Customer> {get; set}
}
class MultipleLists_Customers
{
public List<Customers> {get; set;}
}
答案 0 :(得分:1)
设计是关于代表一个问题域,所以你的设计可能是好的还是坏的,这取决于问题是什么....重命名一些东西,它可能对像
这样的东西非常有效class Customer
{
public string Name {get;set;}
public string LastName {get; set;}
}
class Vendor
{
public string Name {get; set;}
public List<Customer> Customers {get;set}
}
class Organization
{
public string Name {get; set;}
public List<Vendor> Vendors {get;set;}
}
答案 1 :(得分:0)
正如Keith所说,重命名是一个良好的开端。
如果您真正想要的只是一个表示对象列表列表的对象,您可以通过简单地嵌套泛型来实现它而无需创建新的数据结构:
public List<List<Customer>> CustomerLists;
那就是说,您似乎正在尝试为订购系统建模。这样的事情可以解决问题:
class Customer
{
public Customer(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
}
public string FirstName { get; }
public string LastName { get; }
}
class Vendor
{
public Vendor(string name, IEnumerable<Product> products)
{
Name = name;
Products = new HashSet<Product>(products);
}
public bool HasStock(Product product, double quantity)
{
// determine if product is currently in stock...
}
public string Name { get; }
public HashSet<Product> Products { get; }
}
class Product
{
// ... common product data.
}
class Order
{
public Order(
DateTime createDate, Customer customer,
Vendor vendor, Product product, double quantity)
{
CreateDate = createDate;
Customer = customer;
Vendor = vendor;
Product = product;
Quantity = quantity;
}
public DateTime CreateDate { get; }
public Customer Customer { get; }
public Vendor Vendor { get; }
public Product Product { get; }
public double Quantity { get; }
}
答案 2 :(得分:0)
最基本的方法是:
class Customer
{
public string Name {get; set;}
public string LastName {get; set;}
}
class MultipleLists_Customers
{
public List<Customer> Customers {get; set;}
}