Question

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

And two classes which implement IDbContext

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

I also have two controllers, which depend on 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
}

How can I configure StructureMap to achieve this? In my current configuration, I can only configure one instance for IDbContext

x.For<IDbContext>().Use<SecurityContext>();

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 ?

Was it helpful?

Solution

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

Then, using the following syntax to register the UnitOfWork and the IDbContext's

ObjectFactory.Configure(x =>
{
    x.For(typeof(IUnitOfWork<>)).Use(typeof(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>());
}

OTHER TIPS

One approach would be to separate everything out a little more so that you have extra interfaces for each unit of work and related classes. Something like this:

public interface ISecurityContext : IDbContext {}

public interface IHrContext : IDbContext {}

public interface ISecurityUnitOfWork : IUnitOfWork {}

public interface IHrUnitOfWork : IUnitOfWork {}

Then your unit of work classes take in the relevant context:

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

}

And your controller can take in the relevant unit of work:

public LoginController(ISecurityUnitOfWork securityUnitOfWork)
{
}

You still have the base IUnitOfWork and IDbContext interfaces but now have a logically related grouping for each set of operations. It's a bit more heavyweight than the approach you're looking for but it should make your DI resolution simpler to understand.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top