2014-01-18 43 views
3

实体类:类的设计结构,在C#中

public class Customer { 
     public int CustomerId { get; set; } 
     public string Name { get; set; } 
} 

public class Invoice { 
     public int InvoiceId { get; set; } 
     public int CustomerId { get; set; } 
     public string InvoiceNo { get; set; } 
} 

接口:

public interface ICustomerService { 
    Customer GetCustomerById(int customerId); 
    void DeleteCustomer(int customerId); 
} 

public interface IInvoiceService { 
    Invoice GetInvoiceById(int invoiceId); 
    void DeleteInvoice(int invoiceId); 
    List<Invoice> GetAllInvoiceByCustomer(int customerId); 
    Customer GetInvoiceCustomer(int invoiceId); 
} 

类:

public class CustomerService : ICustomerService { 

    private readonly IInvoiceService _invoiceService = new InvoiceService(); 

    public Customer GetCustomerById(int customerId) { 
     //return customer from db 
     return new Customer(); 
    } 

    public void DeleteCustomer(int customerId) { 
     var invoiceList = _invoiceService.GetAllInvoiceByCustomer(customerId); 
     foreach (var invoice in invoiceList) { 
      _invoiceService.DeleteInvoice(invoice.InvoiceId); 
     } 

     //delete customer from db 

    } 
} 

public class InvoiceService : IInvoiceService { 

    private readonly ICustomerService _customerService = new CustomerService(); 

    public Invoice GetInvoiceById(int invoiceId) { 
     //return invoice from db 
     return new Invoice(); 
    } 

    public void DeleteInvoice(int invoiceId) { 
     //delete invoice from db 
    } 

    public List<Invoice> GetAllInvoiceByCustomer(int customerId) { 
     //get all invoice by customer id 
     return new List<Invoice>(); 
    } 

    public Customer GetInvoiceCustomer(int invoiceId) { 
     Invoice invoice = GetInvoiceById(invoiceId); 
     return _customerService.GetCustomerById(invoice.CustomerId); 
    } 

} 

当我创造 “的CustomerService” 一个新的实例。它会返回一个错误:

An unhandled exception of type 'System.StackOverflowException' occurred 

因为当我创造“的CustomerService”新实例“的CustomerService”将创造“InvoiceService”一个新的实例,“InvoiceServer”也创造了“CustomerServer”的新实例。

1)我应该将所有的方法设置为静态吗?

2)“InvoiceService”将具有来自“CustomerService”的调用方法,“CustomerService”也将调用来自“InvoiceSercie”的方法。我如何编写这些类?如果我将所有方法设置为静态,问题将得到解决,但我想这不是一个好的解决方案。

非常感谢!

回答

0

一般来说,我建议在类之间少耦合。每个班级都应该做一件事(客户和发票),然后创建一个同时使用两者的第三班。例如,您可以创建一个名为“CustomerInvoicer”的类,该类在其构造函数中使用两个接口,并将方法“GetInvoiceCustomer”移至该新类。根据我的经验,从长远来看,这将使其更具可维护性,因为每个班级都有单独的责任,而且您的最终用户只需要使用一个主要班级(可能有更先进的逻辑)。

public class CustomerInvoicer { 

    private readonly ICustomerService _customerService; 
    private readonly IInvoiceService _invoiceService; 

    public CustomerInvoicer(ICustomerService cust, IInvoiceService inv) { 
     _invoiceService = inv; 
     _customerService = cust; 
    } 


    public Customer GetInvoiceCustomer(int invoiceId) { 
     Invoice invoice = _invoiceService.GetInvoiceById(invoiceId); 
     return _customerService.GetCustomerById(invoice.CustomerId); 
    } 
} 

此外,我会建议使用此方法的依赖注入库。

4

您必须选择其中一个类别作为参考传递给另一个类别。比方说,它的客户服务:

public class CustomerService : ICustomerService { 

    private readonly IInvoiceService _invoiceService = new InvoiceService(this); 

    ... 
} 

public class InvoiceService : IInvoiceService { 

    private readonly ICustomerService _customerService; 

    public class InvoiceService(ICustomerService customerService) { 
    _customerService = customerService; 
    } 

} 

现在的循环被打破......

另一种选择是使用依赖注入框架,像StructureMap或Ninject。

+0

+1依赖注入 –