设计存储库DI(构造函数注入)为服务层 [英] Designing repositories for DI (constructor injection) for service layer

查看:123
本文介绍了设计存储库DI(构造函数注入)为服务层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个MVC3应用程序,试图用国际奥委会和构造器注入。我的数据库有(到目前为止)约50桌。我使用EF4(W / POCO T4模板)为我的DAC code。我使用存储库模式,并且每个表都有它自己的仓库。我在服务层我的服务类注射瓦特/这些存储库。

I'm building an MVC3 app, trying to use IoC and constructor injection. My database has (so far) about 50 tables. I am using EF4 (w/ POCO T4 template) for my DAC code. I am using the repository pattern, and each table has its own repository. My service classes in my service layer are injected w/ these repositories.

问题:我的服务类正在成长中,他们需要存储库的数量。在某些情况下,我快到10库,并且它开始闻。

Problem: My service classes are growing in the number of repositories they need. In some cases, I am approaching 10 repositories, and it's starting to smell.

是否有设计库和服务类,这样的服务并不需要那么多库?一个共同的方法

Is there a common approach for designing repositories and service classes such that the services don't require so many repositories?

下面是我的想法,我只是不知道哪一个是正确的:

Here are my thoughts, I'm just not sure which one is right:

1)这是一个迹象,我应该考虑合并/编组我的仓库到表中的相关部分,减少数量或每个服务类相关的资源库。这种方法的问题,虽然,是它会膨胀,我的仓库复杂,并会阻止我能够使用的所有存储库的通用接口(标准方法进行数据检索/更新)。

1) This is a sign I should consider combining/grouping my repositories into related sections of tables, reducing the number or dependent repositories per service class. The problem with this approach, though, is that it will bloat and complicate my repositories, and will keep me from being able to use a common interface for all repositories (standard methods for data retrieval/update).

2)这是一个迹象,我应该考虑打破我的服务整合到基于我的仓库(表)组。这个问题是我的一些服务方式有着共同实施,并打破这些跨类可能复杂化我的依赖关系。

2) This is a sign I should consider breaking my services into groups based on my repositories (tables). Problem with this is that some of my service methods share common implementation, and breaking these across classes may complicate my dependencies.

3)这是我不知道我在做什么,有什么根本性的错误,我不是,甚至能看到一个标志。

3) This is a sign that I don't know what I'm doing, and have something fundamentally wrong that I'm not even able to see.

更新:对于我如何实施EF4和存储库的想法,检查出的在codePLEX此示例应用(我用版本1 )。然而,寻找一些有(在这里)的评论,看起来像我需要做一点阅读,以确保这就是我想要走的路线 - 听起来像它可能不是

UPDATE: For an idea of how I'm implementing EF4 and repositories, check out this sample app on codeplex (I used version 1). However, looking at some of the comments there (and here), looks like I need to do a bit more reading to make sure this is the route I want to take -- sounds like it may not be.

推荐答案

<一个href=\"http://stackoverflow.com/questions/8032852/designing-repositories-for-di-constructor-injection-for-service-layer/8033021#8033021\">Chandermani是你的一些表可​​能不是核心领域类权。这意味着你将永远不会搜索数据除了在单一类型的母公司的条款。在这种情况下,你可以参考他们为复杂类型,而不是全面的实体和EF仍然会照顾你。

Chandermani is right that some of your tables might not be core domain classes. This means you would never search for that data except in terms of a single type of parent entity. In those cases you can reference them as "complex types" rather than full-blown entities, and EF will still take care of you.

我使用存储库模式,并且每个表都有它自己的存储库

I am using the repository pattern, and each table has its own repository

我希望你不是自己从头开始编写这些。

I hope you're not writing these yourself from scratch.

在EF 4.1已经实现 Repository模式(<一个href=\"http://msdn.microsoft.com/en-us/library/system.data.entity.dbset%28v=vs.103%29.aspx\">DbSet),和工作模式单位(<一个href=\"http://msdn.microsoft.com/en-us/library/system.data.entity.dbcontext%28v=vs.103%29.aspx\">DbContext).老版本做太多,虽然的DbContext 模板,可以很容易地进行调整,通过改变这些属性到 IDbSet

The EF 4.1 already implements the Repository Pattern (DbSet), and the Unit of Work pattern (DbContext). The older versions do too, though the DbContext template can easily be tweaked to provide a clean mockable implementation by changing those properties to an IDbSet.

我见过几个教程文章,人们还是写自己,虽然。奇怪的是我,因为他们通常不提供理由,比他们贯彻存储库模式的事实等。

I've seen several tutorial articles where people still write their own, though. It is strange to me, because they usually don't provide a justification, other than the fact that they are "implementing the Repository Pattern".

对于这些存储库访问方法,如 FindById 写作封装使其稍微容易访问,但正如你所看到的是力气大的量可能小的回报。就个人而言,除非我发现有有趣的是,域逻辑或复杂的查询进行封装,我甚至不打扰,只是使用的LINQ 直接对 IDbSet

Writing wrappers for these repositories for access methods like FindById make it slightly easier to access, but as you've seen is a big amount of effort potentially little payback. Personally, unless I find that there is interesting domain logic or complex queries to be encapsulated, I don't even bother and just use Linq directly against the IDbSet.

在我的服务层我的服务类注射瓦特/这些存储库。

My service classes in my service layer are injected w/ these repositories.

即使您选择使用自定义查询包装,你可以选择简单地注入的DbContext ,并让服务code实例它所需要的包装。你仍然可以嘲笑你的数据访问层,你只是将无法小样包装code。我还是建议你注入比较特殊的人,但因为复杂的实现正是你希望能在维护分解出,或嘲笑替代型的事。

Even if you choose to use custom query wrappers, you might choose to simply inject the DbContext, and let the service code instantiate the wrappers it needs. You'd still be able to mock your data access layer, you just wouldn't be able to mock up the wrapper code. I'd still recommend you inject less generic ones though, because complex implementation is exactly the type of thing you'd like to be able to factor out in maintenance, or replace with mocks.

这篇关于设计存储库DI(构造函数注入)为服务层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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