kanayabhattad / autofac

Automatically exported from code.google.com/p/autofac
Other
0 stars 0 forks source link

Inject Specific Type With Autofac #436

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I want to inject specific type when some conditions are met. For example, i 
have got an interface like below.
[code]
public interface IMyInterface{

}
[/code]
And also two classes that implement this interface
[code]
public class MyClassA : IMyInterface {
}[/code]
and
[code]
public class MyClassB : IMyInterface {
}[/code]
Finally i have some service class that gets a constructor parameter as 
IMyInterface.
[code]
public class ServiceA{
private IMyInterface _interfaceClass;
public ServiceA(IMyInterface interfaceClass){
_interfaceClass = interfaceClass
}
}[/code]
and
[code]
public class ServiceB{
private IMyInterface _interfaceClass;
public ServiceA(IMyInterface interfaceClass){
_interfaceClass = interfaceClass
}
}[/code]
Now because of some architectural constraints ServiceA must get MyClassA as 
constructor parameter and ServiceB must get MyClassB as constructor parameter.

Normally i am using Autofac like below

var builder = new ContainerBuilder();
builder.RegisterType<MyClassA>().As<IMyInterface>().InstancePerApiRequest();
But i couldn't find the way how can i force Autofac to select right type for 
specific type. I looked to Named<> feature but couldn't solve problem.

Original issue reported on code.google.com by bahadira...@gmail.com on 23 May 2013 at 11:57

GoogleCodeExporter commented 8 years ago
I forgot that [code][/code] tags not working here and realized after posting. 
But i couldn't edit, because there is no edit link. Sorry for this

Original comment by bahadira...@gmail.com on 23 May 2013 at 11:59

GoogleCodeExporter commented 8 years ago
I see that this question has been asked on StackOverflow here:
http://stackoverflow.com/questions/16699588/inject-specific-type-with-autofac

Given it is a usage/design question rather than a defect in Autoac, I'm going 
to close the issue.

Original comment by travis.illig on 23 May 2013 at 5:04

GoogleCodeExporter commented 8 years ago
After i can't get answer for 2 days, i posted here, too. 
you can close, if it is not suitable here.

Original comment by bahadira...@gmail.com on 23 May 2013 at 5:06