如何使用StructureMap DI这种情况? [英] How can I use StructureMap DI for this situation?

查看:91
本文介绍了如何使用StructureMap DI这种情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一类称为的UnitOfWork 它实现了 IUnitOfWork

I have a class called unitofwork which implements 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>();

摘要:我想与 SecurityContext的的UnitOfWork 的实例的LoginController 并注入的UnitOfWork HrContext 的实例 SalaryController 。要做到这一点有什么配置/在构造变化所需?

Summary: I want to inject an instance of unitofwork with SecurityContext to LoginController and inject an instance of unitofwork with HrContext to SalaryController. To do that what are the configuration /changes in constructor required ?

推荐答案

如果在的UnitOfWork 逻辑是常见的那么最简单的解决方案是让的UnitOfWork 通用

If the UnitOfWork logic is common then the easiest solution is to make UnitOfWork generic

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

然后,使用下面的语法来注册的UnitOfWork IDbContext

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

您可以控制​​哪些 IDbContext 注入的UnitOfWork

You can control which IDbContext is injected into the UnitOfWork

public LoginController(IUnitOfWork<SecurityContext> securityContextUow) { }

public SalaryController(IUnitOfWork<HrContext> hrContextUow) { }


下面是一个简单的单元测试,以证明它按预期工作


Here's a quick unit test to prove it works as expected

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

这篇关于如何使用StructureMap DI这种情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆