1

I am using structure map IOC container I am not using the scan with default name convention here.

public interface ICompanyRepository

{

    IEnumerable<Company> GetAll();

    Company Get(int id);

    Company Add(Company item);


    bool Update(Company item);

    bool Delete(int id);


}

public class Company1: ICompanyRepository
{
   // Proivide implementation for all interface  methods
}

public class Company2: ICompanyRepository
{
   // Provide implementation for all interface  methods

//Class Company2 will also have new method called DisplayLog
    public void DisplayLog()
    {
        //To do
    }
}

I am trying to implement DI using structuremap in my Customer controller class how can I tell the that i need methods ofcompany1 to be called or methods Company2 to be called?

_.Scan(x =>
     {
        x.TheCallingAssembly();

        x.AddAllTypesOf<ICompanyRepository>();

        // or

    });

my code: private readonly ICustomerRepository customerRepository;

    public CustomerController(ICustomerRepository CustomerRepository)
    {
        customerRepository = CustomerRepository;
    }


    // GET: Customer  
    public ActionResult Index()
    {
        var customers = customerRepository.GetAll
       //Here i need to be specfic telling i need to call company1 or company2 class methods ?
        return View(customers);
    }
Kumee
  • 201
  • 1
  • 4
  • 11
  • Possible duplicate of [structuremap - two implementations of same interface](https://stackoverflow.com/questions/28602403/structuremap-two-implementations-of-same-interface) – Joe Wilson Mar 08 '18 at 04:15
  • This is not an Duplicate Question – Kumee Mar 08 '18 at 04:22

1 Answers1

-1

If you have two concrete implementations of the same interface, you can use StructureMap's named instances so it knows which one to build.

// Repo1
public class CompanyRepository1: ICompanyRepository
{
}

// Repo2
public class CompanyRepository2: ICompanyRepository
{
}

// Controller
public CompanyController(ICompanyRepository companyRespository2)
{
    _companyRepository2 = companyRespository2;
}

// StrucureMap registration
For<ICompanyRepository>().Use<CompanyRepository1>().Named("companyRepository1");
For<ICompanyRepository>().Use<CompanyRepository2>().Named("companyRepository2");
Joe Wilson
  • 5,591
  • 2
  • 27
  • 38
  • I have made the above changes in my MVC controller how do I explicitly specify which class of interface to be used? – Kumee Mar 08 '18 at 06:57
  • I did changes as you suggested but still, I am not able to see DisplayLog() – Kumee Mar 08 '18 at 07:02
  • When i did this change it always loads me data from companyRepository2 – Kumee Mar 08 '18 at 11:15
  • Yes. By naming the parameter in the controller companyRepository2 and setting up StructureMap to provide that instance when that parameter name is used, you are binding the controller to using CompanyRepository2. – Joe Wilson Mar 08 '18 at 15:25
  • even if i try to load companyRepository1 in the controller it always by default load me companyRepository2 – Kumee Mar 08 '18 at 18:23