C#中的类设计结构

时间:2014-01-18 04:33:30

标签: c# asp.net entity-framework

实体类:

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”的方法。我怎么写这些课程?如果我将所有方法设置为静态,问题将得到解决,但我想这不是一个好的解决方案。

非常感谢!

2 个答案:

答案 0 :(得分: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。

答案 1 :(得分:0)

一般来说,我建议在类之间减少耦合。每个类应该做一件事(Customer和Invoice),然后创建一个使用两者的第三个类。例如,您可以创建一个名为“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);
    }
}

另外,我建议使用这种方法的依赖注入库。