简体   繁体   English

存储库模式 - MVC店面

[英]Repository Pattern - MVC storefront

Have been looking at the MVC storefront and see that IQueryable is returned from the repository classes. 一直在查看MVC店面,并看到从存储库类返回IQueryable。 Wondering if you are not using LINQ does it makes sense to return that object? 想知道如果你没有使用LINQ,那么返回该对象是否有意义? In the case of LINQ in makes sense because of deferred execution, so adding filtering in the service layer makes sense, but if you don't use LINQ you would want to filter in the DB in many cases. 在LINQ因为延迟执行而有意义的情况下,因此在服务层中添加过滤是有意义的,但如果不使用LINQ,则在许多情况下您希望在DB中进行过滤。 In this case would I just add methods that do the filtering to the repository? 在这种情况下,我只是添加对存储库进行过滤的方法吗? If I do, is the service layer really useful? 如果我这样做,服务层真的有用吗?

参数可以采用任何一种方式,请参阅最近的博客文章: 我的存储库是否应该公开IQueryable?

The IQueryable stuff that Rob Conery put into the MVC Storefront is innovative, but by no means the norm when it comes to creating repositories. Rob Conery投入MVC店面的IQueryable是创新的,但在创建存储库方面绝不是常态。 Usually, a repository is responsible for mapping your domain to and from the database. 通常,存储库负责将域映射到数据库和从数据库映射。 Returning IQueryable doesn't really perform any mapping and relies on the services layer to do that. 返回IQueryable并不真正执行任何映射,并依赖于服务层来执行此操作。 This has its advantages and disadvantages, but suffice it to say that it's not the only way to do it. 这有其优点和缺点,但足以说它不是唯一的方法。

You will notice, however, that your services end up becoming a little smelly because of all the duplicate code. 但是,您会注意到,由于所有重复的代码,您的服务最终会变得有点臭。 For instance, if you just want to get a list of all the users in your database you'd have to define that function in both the repository and the service layer. 例如,如果您只想获取数据库中所有用户的列表,则必须在存储库和服务层中定义该功能。 Where the service layer shines, however, is when multiple transactions to/from the database are needed for one operation. 但是,服务层闪耀的地方是,当一个操作需要与数据库进行多次事务时。

The issue I have with exposing IQueryable to the Service Layer is that if you ever wanted to wrap the Repository layer behind a Web Service without breaking the Service Layer code you couldn't, well not without using ADO.NET Data Services but then all your Repository code would essentially become redundant. 将IQueryable暴露给服务层的问题是,如果你想在不破坏服务层代码的情况下将存储库层包装在Web服务之后,那么就不能使用ADO.NET数据服务,但是存储库代码实际上将变得多余。

Whilst I think it can be pretty productive for small apps, when you start looking at scaling and distribution it does more bad than good. 虽然我认为对于小型应用程序来说它可以非常高效,但是当你开始考虑扩展和分发时,它确实比坏事更糟糕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM