我一直在使用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 SubMy 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 ClassMy 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 IMyServiceAnyway 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
更多推荐
发布评论