Unity.mvc3 - 没有为由HierarchicalLifetimeManager管理的一次性类调用Dispose方法

时间:2013-05-29 15:37:03

标签: asp.net-mvc asp.net-mvc-3 dependency-injection unity-container unit-of-work

下面是我的代码,问题是我的UnitOfWork类的dispose方法没有被调用。对于DI,我在Asp.net MVC3应用程序中使用Unity v2.1.505和Unity.Mvc3 v1.2

[assembly: PreApplicationStartMethod(typeof(Program), "Initialize")]

namespace Practice.DependencyResolution.Concrete
{
public class Program
{
    private static IUnityContainer container;

    public static void Initialize()
    {
        if (container == null) container = new UnityContainer();

        string databaseSource = Settings.Default.DatabaseSource;

        var dependencyMapperType = Type.GetType("Practice.DependencyResolution.Concrete." + databaseSource + "DependencyMapper", true);
        var dependencyMapper = (IDependencyMapper)Activator.CreateInstance(dependencyMapperType);
        var dependencyMapperContext = new DependencyMapperContext(dependencyMapper);
        dependencyMapperContext.MapDependencies(container);


        ControllerBuilder.Current.SetControllerFactory(new UnityControllerFactory(container));
        var locator = new UnityServiceLocator(container);
        ServiceLocator.SetLocatorProvider(() => locator);
        DependencyResolver.SetResolver(new UnityDependencyResolver(container));
    }
}
}

internal class DependencyMapperContext
{
    private IDependencyMapper dependencyMapper;

    public DependencyMapperContext(IDependencyMapper dependencyMapper)
    {
        this.dependencyMapper = dependencyMapper;
    }

    public void MapDependencies(IUnityContainer container)
    {
        dependencyMapper.MapDependencies(container);

    }
}

internal class AnyDependencyMapper : IDependencyMapper
{
    public void MapDependencies(IUnityContainer container)
    {
        container.RegisterType<ISupplierRepository, SupplierRepository>();
        container.RegisterType<IUnitOfWork, UnitOfWork>(new HierarchicalLifetimeManager());
    }
}

public class UnitOfWork : IUnitOfWork
{
    private readonly TransactionScope transactionScope;
    private readonly ModelDataContext context;
    private bool disposed = false;

    public UnitOfWork()
    {
        transactionScope = new TransactionScope();
        this.context = new ModelDataContext();
    }

    ModelDataContext IUnitOfWork.Context
    {
        get
        {
            Debug.WriteLine("context get called");

            return context;
        }
    }

    public void Commit()
    {
        if (disposed) throw new ObjectDisposedException("transactionScope");
        transactionScope.Complete();
    }

    protected virtual void Dispose(bool disposing)
    {
        if (disposed == false)
        {
            if (disposing)
            {
                if (context != null)
                {
                    context.Dispose();
                }

                if (transactionScope != null)
                {
                    transactionScope.Dispose();
                }
                disposed = true;
            }
        }
    }

    public void Dispose()
    {
        Debug.WriteLine("Access dispose called");
        if (HttpContext.Current != null && HttpContext.Current.Error != null)
        {
            //transaction transactionScope will be disposed automatically, do nothing
        }
        else
        {
            Commit();
        }

        Dispose(true);

        GC.SuppressFinalize(this);
    }
}

 public class SupplierRepository : ISupplierRepository
{
    private readonly IUnitOfWork unitOfWork;
    private bool disposed = false;

    public SupplierRepository(IUnitOfWork unitOfWork)
    {
        this.unitOfWork = unitOfWork;
    }

    public IList<SupplierItem> GetAll()
    {
        return unitOfWork.Context.SupplierItems.ToList();
    }

    public SupplierItem GetById(object id)
    {
        return unitOfWork.Context.SupplierItems.SingleOrDefault(a => a.SupplierID == (int)id);
    }

    public void Insert(SupplierItem entity)
    {
        unitOfWork.Context.SupplierItems.InsertOnSubmit(entity);
        unitOfWork.Context.SubmitChanges();
    }

    public void Delete(object id)
    {
        var supplier = unitOfWork.Context.SupplierItems.SingleOrDefault(a => a.SupplierID == (int)id);
        unitOfWork.Context.SupplierItems.DeleteOnSubmit(supplier);
        unitOfWork.Context.SubmitChanges();
    }

    public void Delete(SupplierItem entityToDelete)
    {
        Delete(entityToDelete.SupplierID);
    }

    public void Update(SupplierItem entityToUpdate)
    {
        var supplier = unitOfWork.Context.SupplierItems.SingleOrDefault(a => a.SupplierID == entityToUpdate.SupplierID);
        supplier.Address = entityToUpdate.Address;
        supplier.City = entityToUpdate.City;
        supplier.CompanyName = entityToUpdate.CompanyName;
        supplier.ContactName = entityToUpdate.ContactName;
        supplier.ContactTitle = entityToUpdate.ContactTitle;
        supplier.Country = entityToUpdate.Country;
        supplier.Fax = entityToUpdate.Fax;
        supplier.HomePage = entityToUpdate.HomePage;
        supplier.Phone = entityToUpdate.Phone;
        supplier.PostalCode = entityToUpdate.PostalCode;
        supplier.Region = entityToUpdate.Region;
        unitOfWork.Context.SubmitChanges();
    }

    public SupplierItem GetDefault()
    {
        return new SupplierItem();
    }
}

我是DI和Unity的新手,先谢谢。

1 个答案:

答案 0 :(得分:0)

我确实读到你正在使用MVC 3.然而,如果你有可能更新到MVC 4,那么新的Unity 3支持开箱即用的MVC,并且可以使用HierarchicalLifetimeManager。

我不熟悉Unity.Mvc3 NuGet包(微软不支持)。