簡體   English   中英

在這種情況下,如何使用StructureMap DI?

[英]How can I use StructureMap DI for this situation?

我有一個叫做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>();

摘要:我要注入的一個實例unitofworkSecurityContextLoginController ,注入的一個實例unitofworkHrContextSalaryController 為此,構造函數需要進行哪些配置/更改?

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

一種方法是將所有內容分開一些,以便為每個工作單元和相關類提供額外的接口。 像這樣:

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分辨率更易於理解。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM