服务层都在重复我的资料库 [英] Service Layer are repeating my Repositories

查看:137
本文介绍了服务层都在重复我的资料库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用asp.net mvc的,NHibernate和DDD的应用程序。我由我的应用程序控制器使用的服务层。一切都使用Unity注入(服务ISessionFactory在仓库,仓库和服务控制器)依赖和正常工作。

I'm developing an application using asp.net mvc, NHibernate and DDD. I have a service layer that are used by controllers of my application. Everything are using Unity to inject dependencies (ISessionFactory in repositories, repositories in services and services in controllers) and works fine.

不过,这是很常见的,我需要的服务的方法来获得唯一对象在我的仓库,像这样(服务类):

But, it's very common I need a method in service to get only object in my repository, like this (in service class):

public class ProductService {

   private readonly IUnitOfWork _uow;
   private readonly IProductRepository _productRepository;

   public ProductService(IUnitOfWork unitOfWork, IProductRepository productRepository) {
      this._uow = unitOfWork;
      this._productRepository = productRepository;
   }

   /* this method should be exists in DDD ??? It's very common */
   public Domain.Product Get(long key) {
      return _productRepository.Get(key);
   }

   /* other common method... is correct by DDD ? */
   public bool Delete(long key) {
      usign (var tx = _uow.BeginTransaction()) {
         try 
         {
           _productRepository.Delete(key);
           tx.Commit();
           return true;
         } catch {
           tx.RollBack();
           return false;
         }        
      }
   }

   /* ... others methods ... */

}

这code是正确的由DDD?为每个服务类我有一个库,并为每个服务类,我需要做的方法获得一个实体?

This code is correct by DDD ? For each Service class I have a Repository, and for each service class need I do a method "Get" for an entity ?

谢谢你们

干杯

推荐答案

这看起来从我的角度是正确的。我真的不喜欢在我的asp.net MVC项目一遍又一遍地重复服务和存储库的方法名称,所以我去了一个通用的方法库/模式。这意味着,我真的只需要一个或两个的get()方法在我的仓库取回我的对象​​。这对我来说是可能的,因为我使用实体框架和我有我的存储库的get()方法返回一个IQueryable的。然后,我可以做到以下几点:

That looks correct from my perspective. I really didn't like repeating service and repository method names over and over in my asp.net MVC project, so I went for a generic repository approach/pattern. This means that I really only need one or two Get() methods in my repository to retrieve my objects. This is possible for me because I am using Entity Framework and I just have my repository's get() method return a IQueryable. Then I can just do the following:

Product product = from p in _productRepository.Get() where p.Id == Id select p; 

您也许可以与LINQ复制此NHibernate的 - > NHibernate的

You can probably replicate this in NHibernate with linq -> NHibernate.



编辑:的这适用于DDD,因为这仍允许作为数据库我使用​​的,只要我交换我的DAL /仓库(NHibernate的,EF等)支持IQueryable的。


This works for DDD because this still allows me to interchange my DAL/repositories as long as the data library I am using (Nhibernate, EF, etc..) supports IQueryable.

我不知道如何做一个普通的仓库,但不会IQueryable的,但你也许可以使用委托/ lambda函数纳入它。

I am not sure how to do a generic repository without IQueryable, but you might be able to use delegates/lambda functions to incorporate it.



EDIT2:的和公正的情况下,我没有正确地回答你的问题,如果你是问你是否应该从服务调用资源库的get()方法,那么是的,这是正确的DDD设计为好。其原因是服务层应该处理所有的业务逻辑,所以决定究竟如何,并要检索的数据(例如,你想让它按字母顺序排列,无序的,等...)。这也意味着,它可以根据需要或验证删除和/或保存之前加载后执行验证。


And just in case I didn't answer your question correctly, if you are asking if you are supposed to call your repository's Get() method from the service then yes, that is the correct DDD design as well. The reason is that the service layer is supposed to handle all your business logic, so it decides exactly how and what data to retrieve (for example, do you want it in alphabetical order, unordered, etc...). It also means that it can perform validation after loading if needed or validation before deleting and/or saving.

这意味着在服务层不关心数据是如何存储和检索,只决定哪些数据存储和检索。然后,它调用的库正确处理请求和检索/存储数据服务层的告诉它的方式。因此,你有顾虑正确分离。

This means that the service layer doesn't care exactly how that data is stored and retrieved, it only decides what data is stored and retrieved. It then calls on the repository to handle the request correctly and retrieve/store the data in the way the service layer tells it to. Thus you have correct separation of concerns.

这篇关于服务层都在重复我的资料库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆