EF6(code第一),MVC,团结,没有一个仓库服务层 [英] EF6 (code first), MVC, Unity, and a service layer without a repository

查看:120
本文介绍了EF6(code第一),MVC,团结,没有一个仓库服务层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序正在使用SQL Server 2012,EF6,MVC和Web API。

My application is using SQL Server 2012, EF6, MVC and Web API.

它也使用一个仓库和各种文件,如:

It's also using a repository and assorted files such as:

DatabaseFactory.cs
Disposable.cs
IDatabaseFactory.cs
IRepository.cs
IUnitOfWork.cs
RepositoryBase.cs
UnitOfWork.cs

我们已经用我们的控制器和存储库之间的服务层
对于一些复杂的业务逻辑。我们没有计划EVER切换到不同的数据库,它已经指出
给我看,最近的想法是,EF6是一个仓库,所以为什么要建立
在它的顶部和另一个仓库里的为什么有我上面有文件的

We already use a service layer between our controllers and the repository for some complicated business logic. We have no plans EVER to change to a different database and it has been pointed out to me that the recent thinking is that EF6 is a repository so why build another repository on top of it and why have all of the files I have above.

我开始认为这是一个明智的做法。

I am starting to think this is a sensible approach.

有谁知道实现EF6没有任何的例子在那里
库,与服务层。我在网络上搜索已经揭示了许多
复杂的code的例子,似乎太过复杂毫无理由可言。

Does anyone know of any examples out there that implement EF6 without a repository, with a service layer. My search on the web has revealed many complex code examples that seem over complicated for no reason at all.

我的问题是还使用服务层时,又在哪里我把:

My problem is also when using a Service Layer then where do I put:

context = new EFDbContext()

在控制器中,服务层或两者兼而有之?我读,我可以依赖关系注入做到这一点。我已经使用统一为国际奥委会,但我不知道我能做到这一点。

In the controller, the service layer or both ? I read that I can do this with dependancy injection. I already use Unity as an IOC but I don't know how I can do this.

推荐答案

实体框架的的已经工作模式实现的单位,以及一个通用仓库实现(的DbContext是UOW和DbSet是通用库)。我同意它的方式矫枉过正的大多数应用程序的工程师在上面另一个UOW或通用库(此外,GenericRepsitory被认为是一些反模式)。

Entity Framework IS already a Unit of Work pattern implementation as well as a generic repository implementation (DbContext is the UoW and DbSet is the Generic Repository). And I agree that it's way overkill in most apps to engineer another UoW or Generic Repository on top of them (besides, GenericRepsitory is considered to be an anti-pattern by some).

一个服务层可以作为一个具体的资料库,里面有很多的封装特定于您的业务需求的数据逻辑的好处。如果使用这一点,那么还有一点需要建立在它上面的存储库(除非你希望能够改变你的后端服务技术,从WCF说的WebAPI或任何..)

A Service layer can act as a concrete repository, which has a lot of benefits of encapsulating data logic that is specific to your business needs. If using this, then there is little need to build a repository on top of it (unless you want to be able to change your backend service technology, say from WCF to WebApi or whatever..)

我把你所有的数据访问在你的服务层。不要做在你的控制器的数据访问。这是您的泄露数据层到你的UI层,这只是设计不佳。它违反了许多核心SOLID概念。

I would put all your data access in your service layer. Don't do data access in your controller. That's leaking your data layer into your UI layer, and that's just poor design. It violates many of the core SOLID concepts.

但你并不需要一个额外的UnitOfWork,或者超出了其他层在大多数情况下,除非你的应用程序是非常复杂的,并打算在多种环境中工作......

But you do NOT need an additional UnitOfWork, or other layers beyond that in most cases, unless your apps are very complex and intended to work in multiple environments...

这篇关于EF6(code第一),MVC,团结,没有一个仓库服务层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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