0

Below is my code, and the issue is that the dispose method of my UnitOfWork class does not get called. For DI, I am using Unity v2.1.505 with Unity.Mvc3 v1.2 in Asp.net MVC3 Application

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

I am new to DI and Unity, thanks in advance.

4

1 回答 1

0

我确实读到您正在使用 MVC 3。不过,如果您有可能更新到 MVC 4,那么新的 Unity 3 支持开箱即用的 MVC,并与 HierarchicalLifetimeManager 一起使用。

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

于 2013-06-05T21:42:15.023 回答