[英]Am i using Unity and UnitOfWork in right way?
Am I correct doing this? 我这样做是对的吗? Can there be problems with memory usage or performance?
内存使用或性能是否存在问题?
UPD:Bug with disposed context is fixed. UPD:修复已处理上下文的错误。
I'm using Unity with the Unit of Work pattern. 我正在使用Unity与工作单元模式。 When I run my application for the first time, everything works as expected, but once I refresh the page, the app crashes with the following exception:
当我第一次运行我的应用程序时,一切都按预期工作,但一旦我刷新页面,应用程序崩溃时出现以下异常:
The operation cannot be completed because the DbContext has been disposed.
由于已经处理了DbContext,因此无法完成操作。
My DI configuration looks like this: 我的DI配置如下所示:
private static IUnityContainer BuildUnityContainer()
{
var container = new UnityContainer();
container.RegisterType<DataContext>();
container.RegisterType<IUnitOfWorkFactory, UnitOfWorkFactory>();
container.RegisterType<IUnitOfWork, UnitOfWork>();
container.RegisterType<IProductRepository, ProductRepository>();
return container;
}
My UnitOfWork
and UnitOfWorkFactory
look like this: 我的
UnitOfWork
和UnitOfWorkFactory
看起来像这样:
public class UnitOfWork : IUnitOfWork
{
private readonly IUnityContainer _container;
private bool _disposed;
public UnitOfWork(IUnityContainer container)
{
_container = container;
Context = _container.Resolve<DataContext>();
}
internal DataContext Context { get; set; }
public TRepository GetRepository<TRepository>()
where TRepository : class
{
return _container.Resolve<TRepository>();
}
public void Commit()
{
Context.SaveChanges();
}
public void Dispose()
{
if (!_disposed)
{
_disposed = true;
Context.Dispose();
}
}
}
public class UnitOfWorkFactory : IUnitOfWorkFactory
{
private readonly IUnityContainer _container;
public UnitOfWorkFactory(IUnityContainer container)
{
_container = container;
}
public IUnitOfWork CreateUnitOfWork()
{
return _container.Resolve<UnitOfWork>();
}
}
The UnitOfWork
contains a GetRepository
method that allows returning any repository. UnitOfWork
包含一个允许返回任何存储库的GetRepository
方法。 Here is an example of such a repository: 以下是此类存储库的示例:
public class ProductRepository : BaseRepository<Product>,
IProductRepository
{
public ProductRepository(UnitOfWork unitOfWork)
:base(unitOfWork)
{
}
public Product Create(Product entity)
{
return DataBase.Add(entity);
}
public Product Remove(Product entity)
{
return DataBase.Remove(entity);
}
public Product Get(Int32 entityId)
{
return DataBase.FirstOrDefault(x => x.Id == entityId);
}
}
public class BaseRepository<TEntity>
where TEntity : class
{
internal BaseRepository(UnitOfWork unitOfWork)
{
UnitOfWork = unitOfWork;
}
internal UnitOfWork UnitOfWork { get; private set; }
internal virtual IDbSet<TEntity> DataBase
{
get { return UnitOfWork.Context.Set<TEntity>(); }
}
}
My MVC controllers depend on IUnitOfWorkFactory
. 我的MVC控制器依赖于
IUnitOfWorkFactory
。 Here is the AccountController
for example: 以下是
AccountController
的示例:
public class AccountController : Controller
{
private readonly IUnitOfWorkFactory _unitOfWorkFactory;
public AccountController(IUnitOfWorkFactory unitOfWorkFactory)
{
_unitOfWorkFactory = unitOfWorkFactory;
}
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
using (var uow = _unitOfWorkFactory.CreateUnitOfWork())
{
var rep = uow.GetRepository<IProductRepository>();
var prod = rep.GetById(1);
}
return View();
}
}
You are telling Unity to create UnitOfWork once (singleton): 您告诉Unity创建一次UnitOfWork(单例):
container.RegisterType<IUnitOfWork, UnitOfWork>(
new ContainerControlledLifetimeManager());
I dont think you want that. 我不认为你想要那个。 Remove the ContainerControlledLifetimeManager()....
删除ContainerControlledLifetimeManager()....
I think this is because of the LifetimeManager you are using. 我认为这是因为你正在使用的LifetimeManager。 (A LifetimeManager holds onto an instance given to it.) Your instance is disposed, but becouse of the ContainerControlledLifetimeManager, it doesn't create a new one.
(LifetimeManager保留给它的实例。)您的实例被释放,但由于ContainerControlledLifetimeManager,它不会创建新的实例。 In this article you can read how you can resolve this issue.
在此文章中,你可以看到你是如何解决这个问题。
Basically it is a good idea to use Unity.MVC3 package, and define a HierarchicalLifetimeManager for your IDisposable dependencies. 基本上,最好使用Unity.MVC3包,并为您的IDisposable依赖项定义HierarchicalLifetimeManager。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.