初始警告:很长的帖子,无论如何我可能完全错误地理解了模式:
给定以下类,它是客户聚合的开始:
public class Customer : KeyedObject
{
public Customer(int customerId)
{
_customerRepository.Load(this);
}
private ICustomerRepository _customerRepository = IoC.Resolve(..);
private ICustomerTypeRepository = _customerTypeRepository = IoC.Resolve(..);
public virtual string CustomerName {get;set;}
public virtual int CustomerTypeId [get;set;}
public virtual string CustomerType
{
get
{
return _customerTypeRepository.Get(CustomerTypeId);
}
}
}
CustomerType 由一个值对象表示:
public class CustomerType : ValueObject
{
public virtual int CustomerTypeId {get;set;}
public virtual string Description {get;set;}
}
当我有一个带有 CustomerTypeId 的客户对象时,这一切都很好。但是,当我想在我的 MVC 视图中填充 DropDownList 时,我正在努力解决如何从 ICustomerTypeRepostory 正确获取 CustomerType 值列表的概念。
ICustomerTypeRepository
非常简单:
public interface ICustomerTypeRepository
{
public CustomerType Get(int customerTypeId);
public IEnumerable<CustomerType> GetList();
}
基本上,我想要的是能够ICustomerTypeRepository
从我的控制器正确调用,但是我认为最好将 DAL(存储库)层与控制器分开。现在,我只是把事情复杂化了吗?
这就是我的控制器当前的状态:
public class CustomerController : ControllerBase
{
private ICustomerTypeRepository _customerTypeRepository = IoC.Resolve(..);
public ActionResult Index()
{
Customer customer = new Customer(customerId);
IEnumerable<CustomerType> customerTypeList =
_customerTypeRepository.GetList();
CustomerFormModel model = new CustomerFormModel(customer);
model.AddCustomerTypes(customerTypeList );
}
}
这对我来说似乎是错误的,因为我在 Controller 和 Customer 中有存储库。对我来说,应该有一个用于 CustomerType 的隔离访问层,这似乎是合乎逻辑的。即CustomerType.GetList()
:
public class CustomerType : ValueObject
{
// ... Previous Code
private static ICustomerTypeRepository _customerTypeRepository = IoC.Resolve(..);
public static IEnumerable<CustomerType> GetList()
{
_customerTypeRepository.GetList();
}
}
那么,我应该通过哪种方式将CustomerType
对象暴露ICustomerTypeRepository
给CustomerController
?