ASP.NET MVC Web应用程序中的控制器是否应该调用存储库,服务或两者?

前端之家收集整理的这篇文章主要介绍了ASP.NET MVC Web应用程序中的控制器是否应该调用存储库,服务或两者?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的ASP.NET MVC Web应用程序中的控制器开始有一些业务逻辑的膨胀。网络上的示例都显示简单的控制器操作,只需将数据从存储库中提取出来,并将其传递给视图。但是,如果您还需要支持业务逻辑呢?

比方说,履行订单的动作也需要发送电子邮件。我是否将此粘贴到控制器中并将此逻辑复制/粘贴到任何其他履行订单的操作?我的第一个直觉将是创建一个像OrderFulfillerService这样的服务,它将处理所有这些逻辑,并且控制器动作被调用。但是,对于从数据库中检索用户列表或订单等简单操作,我想直接与存储库进行交互,而不是将该调用包含在服务中。

这是可以接受的设计模式吗?控制器操作在需要数据访问时需要业务逻辑和存储库时调用服务?

解决方法

您的控制器(在MVC项目中)应该在服务项目中调用对象。服务项目是处理所有业务逻辑的地方。

这是一个很好的例子:

  1. public ActionResult Index()
  2. {
  3. ProductServices productServices = new ProductServices();
  4.  
  5. // top 10 products,for example.
  6. IList<Product> productList productServices.GetProducts(10);
  7.  
  8. // Set this data into the custom viewdata.
  9. ViewData.Model = new ProductViewData
  10. {
  11. ProductList = productList;
  12. };
  13.  
  14. return View();
  15. }

或依赖注射(我的fav)

  1. // Field with the reference to all product services (aka. business logic)
  2. private readonly ProductServices _productServices;
  3.  
  4. // 'Greedy' constructor,which Dependency Injection auto finds and therefore
  5. // will use.
  6. public ProductController(ProductServices productServices)
  7. {
  8. _productServices = productServices;
  9. }
  10.  
  11. public ActionResult Index()
  12. {
  13. // top 10 products,for example.
  14. // NOTE: The services instance was automagically created by the DI
  15. // so i din't have to worry about it NOT being instansiated.
  16. IList<Product> productList _productServices.GetProducts(10);
  17.  
  18. // Set this data into the custom viewdata.
  19. ViewData.Model = new ProductViewData
  20. {
  21. ProductList = productList;
  22. };
  23.  
  24. return View();
  25. }

现在..什么是服务项目(或什么是ProductServices)?这是一个具有业务逻辑的类库。例如。

  1. public class ProductServices : IProductServices
  2. {
  3. private readonly ProductRepository _productRepository;
  4. public ProductServices(ProductRepository productRepository)
  5. {
  6. _productRepository = productRepository;
  7. }
  8.  
  9. public IList<Product> GetProducts(int numberOfProducts)
  10. {
  11. // GetProducts() and OrderByMostRecent() are custom linq helpers...
  12. return _productRepository.GetProducts()
  13. .OrderByMostRecent()
  14. .Take(numberOfProducts)
  15. .ToList();
  16. }
  17. }

但这可能是所有如此铁杆和混乱…所以一个简单版本的ServiceProduct类可能是(但我不会真的推荐)…

  1. public class ProductServices
  2. {
  3. public IList<Product> GetProducts(int numberOfProducts)
  4. {
  5. using (DB db = new Linq2sqlDb() )
  6. {
  7. return (from p in db.Products
  8. orderby p.DateCreated ascending
  9. select p).Take(10).ToList();
  10. }
  11. }
  12. }

所以你去您可以看到所有的逻辑都在服务项目中,这意味着您可以在其他地方重用该代码

我在哪里学习?

Rob Conery年的MVC StoreFront媒体和tutorials.切片面包最好的东西。
他的教程以完整的解决方代码示例详细解释(我做了什么)。他使用依赖注入,这是SOO kewl,现在我已经看到他如何使用它,在MVC。

HTH。

猜你在找的asp.Net相关文章