2017-09-19 63 views
-4

我想知道是否有更好的方法来设计类似的数据结构。代码数据结构清单替代解决方案列表C#

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;} 
} 
+2

很难判断一个设计,不知道的要求。这个数据结构正在解决什么问题? –

回答

0

设计是关于代表问题域,让您的设计可能是好还是坏取决于是什么问题....一些重命名事情,它可能是相当有效的像

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;} 
} 
0

正如基思注意到,重命名是一个好的开始。

如果你真正想要的是表示对象的名单列表的对象,就可以实现它没有简单地嵌套泛型创建新的数据结构:

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; } 
} 
0

最基本的方法是这样的:

class Customer 
{ 
    public string Name {get; set;} 
    public string LastName {get; set;} 
} 

class MultipleLists_Customers 
{ 
    public List<Customer> Customers {get; set;} 
}