How to Use Dependenecy injection (StructureMap DI) RRS feed

  • Question

  • User-1388383071 posted

    I have class called unitofwork which Implements IUnitOfWork

    // Constructor
    public UnitOfWork(IDbContext dbContext)
        _context = dbContext;  // Depends to an IDbContext  

    And Two Class which implements IDbContext

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

    also Two controllers. these controller are depended to 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 Structure map to achieve this. my current configuration I can only configure one instance for IDbContext


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

    Saturday, January 4, 2014 9:52 PM

All replies

  • User-821857111 posted

    You should create a separate interface for each of your DbContext implementations. These interfaces will be empty, but will serve to differentiate one from the other, and remove the current ambiguity that exists in your design:

    public interface ISecurityContext{}
    public interface IHrContext{}
    public class SecurityContext : IDbContext, ISecurityContext{}
    public class HrContext : DbContext, IHrContext{}

    Then you can register the empty interfaces.

    Sunday, January 5, 2014 4:40 AM