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说:
假设您有一个用户故事,说无论何时在您的数据库中找不到产品描述,您都应该从其他地方(调用外部服务或其他东西)进行检索.然后很明显你的ProductService必须有一个 private readonly IProductRepository _productRepository; 还有一个 private readonly IProductDescriptionService _productDescriptionService; 在这种情况下,在存储库之上添加服务层确实很有意义. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 如何以完全信任的方式部署IIS ASP .NET应用程序
- asp.net-mvc – 如何找出在MVC3中调用我的视图的控制器动作
- asp.net-mvc – 如何让ASP.NET MVC遵守我的customErrors设置
- asp.net – 为什么DotNetNuke禁用了验证?
- asp.net-mvc – ASP.NET MVC,Nhibernate和小型/中型项目的存
- asp.net-mvc – 如何使用Ninject将服务注入授权过滤器?
- asp.net-mvc-3 – 如果Controller.OnAuthorization()返回vo
- asp.net – GridView:选择页面按钮(数字)样式
- asp.net-mvc – Gzip压缩无法运行ASP.net MVC5
- asp.net-mvc-4 – 无法加载文件或程序集’System.Web.Http.
推荐文章
站长推荐
- asp.net-mvc – angularjs和mvc5之间通信的最佳方
- asp.net-mvc – 如何在MVC的耗时操作中使用Async
- 具有Entity Framework Code Only和ASP.NET MVC的
- asp.net-core – 提供Google身份验证背后的静态文
- ASP.NET WebAPI中的模拟和异步
- asp.net-mvc – 测量时间调用ASP.NET MVC控制器操
- asp.net-mvc – 如何在MVC3中托管Web服务?
- 如何编译x64 asp.net网站?
- asp.net-core – 恢复AspNet标识核心包时出错
- asp.net-mvc – View中的意外NullReferenceExcep
热点阅读