Reputation: 211
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);
}
Upvotes: 1
Views: 778
Reputation: 5671
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");
Upvotes: -1