如何在这种情况下使用StructureMap DI?

时间:2014-01-04 09:57:30

标签: c# asp.net asp.net-mvc dependency-injection structuremap

我有一个名为unitofwork的类,它实现了IUnitOfWork

public class UnitOfWork : IUnitOfWork
{
    private readonly IDbContext _context;

    private bool _disposed;
    private Hashtable _repositories;

    public UnitOfWork(IDbContext dbContext)
    {
        _context = dbContext;

    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    public void Save()
    {
        _context.SaveChanges();
    }

    public virtual void Dispose(bool disposing)
    {
        if (!_disposed)
            if (disposing)
                _context.Dispose();

        _disposed = true;
    }

    public IRepository<T> Repository<T>() where T : class
    {
        if (_repositories == null)
            _repositories = new Hashtable();

        var type = typeof(T).Name;

        if (!_repositories.ContainsKey(type))
        {
            var repositoryType = typeof(BaseRepository<>);

            var repositoryInstance =
                Activator.CreateInstance(repositoryType
                                             .MakeGenericType(typeof(T)), _context);

            _repositories.Add(type, repositoryInstance);
        }

        return (IRepository<T>)_repositories[type];
    }
}

两个实现IDbContext

的类
 public class SecurityContext:IDbContext{}
 public class HrContext:IDbContext{}

我还有两个控制器,它们依赖于IUnitOfWork

public LoginController(IUnitOfWork _securityContextUow)
{
    // Here the injected unitofwork 
    // object must have  SecurityContext ,as its dependent instance
}

public SalaryController(IUnitOfWork _hrContextUow)
{
    // Here the injected unitofwork 
    // object must have  HrContext,as its dependent instance
}

如何配置StructureMap来实现这一目标?在我当前的配置中,我只能为IDbContext

配置一个实例
x.For<IDbContext>().Use<SecurityContext>();

摘要:我想向unitofwork注入一个SecurityContext LoginController的实例,并向unitofwork注入一个HrContext的实例到{ {1}}。要做到这一点,构造函数中的配置/更改是什么?

2 个答案:

答案 0 :(得分:3)

如果UnitOfWork逻辑很常见,那么最简单的解决方案就是使UnitOfWork通用

interface IUnitOfWork<TContext> where TContext: IDbContext { }

public class UnitOfWork<TContext> : IUnitOfWork<TContext>
    where TContext : IDbContext
{
    private readonly TContext context;

    public UnitOfWork(TContext context)
    {
        this.context = context;
    }
}

然后,使用以下语法注册UnitOfWorkIDbContext

ObjectFactory.Configure(x =>
{
    x.For(typeof(IUnitOfWork<>)).Use(typeof(UnitOfWork<>));
});

您可以控制将IDbContext注入UnitOfWork

public LoginController(IUnitOfWork<SecurityContext> securityContextUow) { }

public SalaryController(IUnitOfWork<HrContext> hrContextUow) { }

这是一个快速单元测试,以证明它按预期工作

public class TestUnitOfWork<TContext> : IUnitOfWork<TContext>
    where TContext : IDbContext
{
    public TContext context { get; set; }

    public TestUnitOfWork(TContext context)
    {
        this.context = context;
    }
}

[Test]
public void GetCorrectUnitOfWork()
{
    ObjectFactory.Configure(x =>
    {
        x.For(typeof(IUnitOfWork<>)).Use(typeof(TestUnitOfWork<>));
    });

    //ObjectFactory.AssertConfigurationIsValid();

    var securityContextUow = ObjectFactory
        .GetInstance<IUnitOfWork<SecurityContext>>();
    var hrContextUow = ObjectFactory
        .GetInstance<IUnitOfWork<HrContext>>();

    Assert
        .That((securityContextUow as TestUnitOfWork<SecurityContext>).context, 
        Is.InstanceOf<SecurityContext>());
    Assert
        .That((hrContextUow as TestUnitOfWork<HrContext>).context, 
        Is.InstanceOf<HrContext>());
}

答案 1 :(得分:0)

一种方法是将所有内容分开,以便为每个工作单元和相关类提供额外的接口。像这样:

public interface ISecurityContext : IDbContext {}

public interface IHrContext : IDbContext {}

public interface ISecurityUnitOfWork : IUnitOfWork {}

public interface IHrUnitOfWork : IUnitOfWork {}

然后,您的工作单元课程将涉及相关背景:

public class SecurityUnitOfWork : UnitOfWork,ISecurityUnitOfWork 
{
    public SecurityUnitOfWork(ISecurityContext context)
        : base(context)
    {
        // This change
    }    

}

您的控制员可以接受相关的工作单元:

public LoginController(ISecurityUnitOfWork securityUnitOfWork)
{
}

您仍然拥有基本的IUnitOfWorkIDbContext接口,但现在每组操作都有一个逻辑相关的分组。它比你正在寻找的方法更重要,但它应该让你的DI分辨率更容易理解。