0

我这样做对吗?内存使用或性能是否存在问题?

UPD:已处理上下文的错误已修复。

我将 Unity 与工作单元模式一起使用。当我第一次运行我的应用程序时,一切都按预期工作,但是一旦我刷新页面,应用程序就会崩溃,并出现以下异常:

操作无法完成,因为 DbContext 已被释放。

我的 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;
}

我的UnitOfWorkUnitOfWorkFactory看起来像这样:

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>();
    }
}

UnitOfWork包含一个GetRepository允许返回任何存储库的方法。以下是此类存储库的示例:

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>(); }
    }
}

我的 MVC 控制器依赖于IUnitOfWorkFactory. 以下是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();
    }
}
4

2 回答 2

2

您告诉 Unity 创建一次 UnitOfWork(单例):

    container.RegisterType<IUnitOfWork, UnitOfWork>(
    new ContainerControlledLifetimeManager());

我不认为你想要那个。删除 ContainerControlledLifetimeManager()....

于 2013-02-12T22:00:55.797 回答
0

我认为这是因为您正在使用 LifetimeManager。(LifetimeManager 持有给它的实例。)您的实例已被释放,但由于 ContainerControlledLifetimeManager,它不会创建新实例。在本文中,您可以阅读如何解决此问题。

基本上,使用 Unity.MVC3 包并为您的 IDisposable 依赖项定义一个 HierarchicalLifetimeManager 是一个好主意。

于 2013-02-12T22:05:42.607 回答