我的ASP.NET MVC Web应用程序中的控制器开始有一些业务逻辑的膨胀。网络上的示例都显示简单的控制器操作,只需将数据从存储库中提取出来,并将其传递给视图。但是,如果您还需要支持业务逻辑呢?
比方说,履行订单的动作也需要发送电子邮件。我是否将此粘贴到控制器中并将此逻辑复制/粘贴到任何其他履行订单的操作?我的第一个直觉将是创建一个像OrderFulfillerService这样的服务,它将处理所有这些逻辑,并且控制器动作被调用。但是,对于从数据库中检索用户列表或订单等简单操作,我想直接与存储库进行交互,而不是将该调用包含在服务中。
这是可以接受的设计模式吗?控制器操作在需要数据访问时需要业务逻辑和存储库时调用服务?
解决方法
您的控制器(在MVC项目中)应该在服务项目中调用对象。服务项目是处理所有业务逻辑的地方。
这是一个很好的例子:
- public ActionResult Index()
- {
- ProductServices productServices = new ProductServices();
- // top 10 products,for example.
- IList<Product> productList productServices.GetProducts(10);
- // Set this data into the custom viewdata.
- ViewData.Model = new ProductViewData
- {
- ProductList = productList;
- };
- return View();
- }
或依赖注射(我的fav)
- // Field with the reference to all product services (aka. business logic)
- private readonly ProductServices _productServices;
- // 'Greedy' constructor,which Dependency Injection auto finds and therefore
- // will use.
- public ProductController(ProductServices productServices)
- {
- _productServices = productServices;
- }
- public ActionResult Index()
- {
- // top 10 products,for example.
- // NOTE: The services instance was automagically created by the DI
- // so i din't have to worry about it NOT being instansiated.
- IList<Product> productList _productServices.GetProducts(10);
- // Set this data into the custom viewdata.
- ViewData.Model = new ProductViewData
- {
- ProductList = productList;
- };
- return View();
- }
现在..什么是服务项目(或什么是ProductServices)?这是一个具有业务逻辑的类库。例如。
- public class ProductServices : IProductServices
- {
- private readonly ProductRepository _productRepository;
- public ProductServices(ProductRepository productRepository)
- {
- _productRepository = productRepository;
- }
- public IList<Product> GetProducts(int numberOfProducts)
- {
- // GetProducts() and OrderByMostRecent() are custom linq helpers...
- return _productRepository.GetProducts()
- .OrderByMostRecent()
- .Take(numberOfProducts)
- .ToList();
- }
- }
但这可能是所有如此铁杆和混乱…所以一个简单版本的ServiceProduct类可能是(但我不会真的推荐)…
- public class ProductServices
- {
- public IList<Product> GetProducts(int numberOfProducts)
- {
- using (DB db = new Linq2sqlDb() )
- {
- return (from p in db.Products
- orderby p.DateCreated ascending
- select p).Take(10).ToList();
- }
- }
- }
所以你去您可以看到所有的逻辑都在服务项目中,这意味着您可以在其他地方重用该代码。
我在哪里学习?
从Rob Conery年的MVC StoreFront媒体和tutorials.切片面包最好的东西。
他的教程以完整的解决方案代码示例详细解释(我做了什么)。他使用依赖注入,这是SOO kewl,现在我已经看到他如何使用它,在MVC。
HTH。