加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 洋葱架构 – 服务层责任

发布时间:2020-12-16 07:33:10 所属栏目:asp.Net 来源:网络整理
导读:我正在学习Jeffrey Palermo的Onion Architecture两周多了.我按照 this tutorial创建了一个测试项目.在学习期间,我遇到了关于SO的 this问题.根据接受的答案,一个人nwang建议像GetProductsByCategoryId这样的方法不应该在Repository中,另一方面不应该在Dennis
我正在学习Jeffrey Palermo的Onion Architecture两周多了.我按照 this tutorial创建了一个测试项目.在学习期间,我遇到了关于SO的 this问题.根据接受的答案,一个人nwang建议像GetProductsByCategoryId这样的方法不应该在Repository中,另一方面不应该在Dennis Traub中
表明它是存储库的责任.我在做的是:

我在Domain.Interface中有一个通用存储库,其中我有一个方法查找:

public interface IRepository<TEntity> where TEntity : class
{
     IEnumerable<TEntity> Find(Expression<Func<TEntity,bool>> filter = null);
     .......
     .......
     .......
}

然后我在Infrastucture.Data中创建了一个BaseRepository:

public class RepositoryBase<TEntity> : IRepository<TEntity> where TEntity : class
{
     internal readonly DbSet<TEntity> dbSet;
     public virtual IEnumerable<TEntity> Find(
            Expression<Func<TEntity,bool>> filter = null)
     {
            IQueryable<TEntity> query = dbSet;

            if (filter != null)
            {
                query = query.Where(filter);
            }
            return query.ToList();
     }
}

我在Infrastructure.Data中有一个具体的存储库

public class ProductRepository : RepositoryBase<Product>,IProductRepository
{
      public ProductRepository(MyDBContext context)
           : base(context)
       {         

       }
}

现在,我在服务层中所做的是将存储库注入服务并调用Repository.Find以获取GetProductsByCategoryId等方法.喜欢 :

public class ProductService : IProductService
{
     private readonly IUnitOfWork _unitOfWork;
     private readonly IProductRepository _productRepository;

     public ProductService(IUnitOfWork unitOfWork,IProductRepository productRepository)
     {
          _unitOfWork = unitOfWork;
          _productRepository = productRepository;
     }

     public IList<Product> GetProductsByCategoryId(int CategoryId)
     {
          // At the moment,My code is like this:
          return _productRepository.Find(e => e.CategoryId == CategoryId).ToList();

          // My confusion is here. Am I doing it right or I need to take this code to 
          // ProductRepository and call _productRepositoy.GetProductsByCategoryId(CategoryId) here instead.
          // If I do this,then Service Layer will become more of a wrapper around repository. Isn't it?
          // My question is : What exactly will be the responsibility of the Service Layer in Onion Architecture?
         }
    }

解决方法

您设计应用程序的方式是可以的……但前提是您的服务将处理其他事情,而不仅仅是整理存储库方法!

始终牢记YAGNI principle说:

Always implement things when you actually need them,never when you just foresee that you need them

假设您有一个用户故事,说无论何时在您的数据库中找不到产品描述,您都应该从其他地方(调用外部服务或其他东西)进行检索.然后很明显你的ProductService必须有一个

private readonly IProductRepository _productRepository;

还有一个

private readonly IProductDescriptionService _productDescriptionService;

在这种情况下,在存储库之上添加服务层确实很有意义.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读