在Unity.Mvc5中使用EF DBContext的正确方法(Correct way to use EF DBContext with Unity.Mvc5)

编程入门 行业动态 更新时间:2024-10-28 12:18:21
在Unity.Mvc5中使用EF DBContext的正确方法(Correct way to use EF DBContext with Unity.Mvc5)

我一直在使用EntityFramework。 到目前为止,我只使用了离散使用块中的EF DBContext。 我现在正在尝试使用请求范围依赖注入。 Unity似乎是最简单的,但我不确定我是否正确使用它。

到目前为止,我已经创建了一个示例存储库和服务类(具有相应的接口)。 我在Unity中注册这些,就像这样......

Public Shared Sub RegisterComponents() Dim container = New UnityContainer() container.RegisterType(Of IMyService, MyService)() container.RegisterType(Of IRepository(Of MyModel), MyRepository)() DependencyResolver.SetResolver(New UnityDependencyResolver(container)) End Sub

我的存储库看起来像这样......

Public Class MyRepository Implements IRepository(Of EF.MyModel) <Dependency> Public Property Context() As MyDBEntities Public Function Create(item As MyModel) As Boolean Implements IRepository(Of MyModel).Create Context.MyModel.Add(item) Return Context.SaveChanges() > 0 End Function Public Function Delete(id As Integer) As Boolean Implements IRepository(Of MyModel).Delete Context.MyModel.Remove([Get](id)) Return Context.SaveChanges() > 0 End Function Public Function [Get](id As Integer) As MyModel Implements IRepository(Of MyModel).Get Return Context.MyModel.Find(id) End Function Public Function List() As IQueryable(Of MyModel) Implements IRepository(Of MyModel).List Return Context.MyModel End Function End Class

我的服务类包括以下对存储库的引用......

<Dependency> Public Property Repository() As IRepository(Of MyModel)

目前服务类的其余部分几乎只是存储库方法的包装器,但最终将具有更多高级功能。

在我的控制器中,我像这样引用我的服务......

<Dependency> Public Property MyModelService() As IMyService

无论如何这一切似乎工作正常,我可以在存储库,服务或控制器级别的查询中添加额外的位,并且它似乎总是具有正确的上下文。 但是我不确定如何跟踪和关闭DBContext,因为它没有在Unity中明确注册。

由于<Dependency>属性修饰,它是否只是解决了这个问题?

I've been using EntityFramework for a short time. Up until now I've only used the EF DBContext within discrete using blocks. I'm now trying to use request scope dependency injection. Unity seems to be the easiest, but I'm not sure if I'm using it correctly.

So far I've created a sample repository and service class (with corresponding interfaces). I register these in Unity, like so...

Public Shared Sub RegisterComponents() Dim container = New UnityContainer() container.RegisterType(Of IMyService, MyService)() container.RegisterType(Of IRepository(Of MyModel), MyRepository)() DependencyResolver.SetResolver(New UnityDependencyResolver(container)) End Sub

My repository looks like this...

Public Class MyRepository Implements IRepository(Of EF.MyModel) <Dependency> Public Property Context() As MyDBEntities Public Function Create(item As MyModel) As Boolean Implements IRepository(Of MyModel).Create Context.MyModel.Add(item) Return Context.SaveChanges() > 0 End Function Public Function Delete(id As Integer) As Boolean Implements IRepository(Of MyModel).Delete Context.MyModel.Remove([Get](id)) Return Context.SaveChanges() > 0 End Function Public Function [Get](id As Integer) As MyModel Implements IRepository(Of MyModel).Get Return Context.MyModel.Find(id) End Function Public Function List() As IQueryable(Of MyModel) Implements IRepository(Of MyModel).List Return Context.MyModel End Function End Class

My service class includes the following reference to the repository...

<Dependency> Public Property Repository() As IRepository(Of MyModel)

The rest of the service class at the moment is pretty much just a wrapper for the repository methods, but will eventually have more high-level functionality.

In my controller I reference my service like so...

<Dependency> Public Property MyModelService() As IMyService

Anyway this all seems to work fine, I can add additional bits to the queries at repository, service, or controller level and it always seems to have the correct context. However I'm not sure how it will know to track and close the DBContext, as it's not explicitly registered with Unity.

Does it just figure this out itself because of the <Dependency> property decoration?

最满意答案

是的,团结能够隐含地解决上下文。 但是,当您明确注册它时,您可以更好地控制生命周期,因为您可以告诉Unity使用哪个生命周期管理器。 由于使用transient lifetimemanager创建了隐式解析类型(另请参阅: Unity:更改默认生命周期管理器以进行隐式注册和/或禁用它们 ),上下文的生命周期与存储库的生命周期相同。

供参考: https : //msdn.microsoft.com/en-us/library/ff660872%28v=pandp.20%29.aspx

yes, unity is able to resolve the context implicitly. However, when you register it explicitly you'll have more control over the lifetime, because you can tell Unity which lifetimemanager to use. As implicit resolved types are created with the transient lifetimemanager (see also: Unity: Change default lifetime manager for implicit registrations and/or disable them ) the lifetime of your context is the same as the lifetime of your repository.

for reference: https://msdn.microsoft.com/en-us/library/ff660872%28v=pandp.20%29.aspx

更多推荐

本文发布于:2023-08-07 14:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464926.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正确   方法   Unity   EF   Correct

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!