或者我根本不明白这一点。
我已经使用 Controller --> ViewModel --> Service --> Repository 模式启动了我的 ASP.NET MVC 应用程序。
是否每种类型的对象(客户、产品、类别、发票等)都需要有自己的存储库和服务?如果是这样,您如何将常用元素放在一起?
我的意思是,很多时候这些东西中的一些会显示在同一页面上。所以我不明白这一点,我不认为。
所以我想我需要一个 ShopController,它有一个 ShopViewModel,它可以有类别、子类别、产品等。但对我来说,问题是它似乎不能很好地啮合。
也许 ASP.NET WebForms 适合像我这样的人:)
编辑
聚合也是这样,比如:
Category、SubCategory、Product、ChildProduct、ProductReview,其中 Product 是聚合根?
然后在 ViewModel 中,您将访问 Product 以获取它的子产品、评论等。
我正在使用 Entity Framework 4,那么您将如何使用存储库/服务模式实现延迟加载?
最佳答案
Does every type of object (Customer, Product, Category, Invoice, etc..) need to have it's own repository
您的域中每个聚合根应该有一个存储库。看这个question有关什么是聚合根的更多信息。
在您提供的示例中,我可以看到一个 CustomerReposiotry,它将处理检索所有相关的客户数据(客户有订单,订单有客户)。处理检索产品信息的 ProductRepository。
and service? If so, how do you bring common items together?
服务层很好,但前提是添加该层会带来附加值。如果您的服务只是直接传递到存储库中,则可能不需要它。但是,如果您需要在产品上执行某些业务逻辑,则 ProductService 可能有意义。
这可能没有意义
public void UpdateProduct(Product product)
{
_repo.Update(product);
}
但是,如果您有逻辑,这一层就可以封装您的产品业务规则。
public void UpdateProduct(Product productToUpdate)
{
//Perform some sort of business on the productToUpdate, raise domain events, ....
_repo.Update(productToUpdate);
}
So I was thinking I need a ShopController, which has a ShopViewModel, which could have categories, sub categoires, products, etc. But the problem, for me, is that it just does not seem to mesh well.
如果域被清除, View 模型就有意义了
public ActionResult Index()
{
ShopViewModel shopViewModel = new ShopViewModel();
shopViewModel.Products = _productRepo.GetAll();
//other stuff on the view model.
return(shopViewModel);
}
更新
What happens when you also need to provide data unobtainable from an aggregate root? For example, say I have a create Customer view and in that view, I also need to provide the user with a collection of Companies to choose from to associate a new customer with. Does the collection of Companies come from CustomerRepository or would you also need a CompanyRepository?
如果公司可以独立存在(例如,您编辑、更新、删除公司),我建议公司也是您域的聚合根(客户有公司,公司有客户列表)。但是,如果公司只能通过客户获得,我会将公司视为 ValueType/Value 对象。如果是这种情况,我会在客户存储库上创建一个方法来检索所有公司名称。
_repo.GetAllCompanyNames();
关于asp.net-mvc-2 - ASP.NET MVC - 我想我做错了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5085286/