Instantiating through autofac but still choose which instance to use
This commit is contained in:
@ -29,6 +29,13 @@ namespace AutoFacSamles
|
||||
{
|
||||
private ILog log;
|
||||
private int id;
|
||||
|
||||
public Engine(ILog log, int id)
|
||||
{
|
||||
this.log = log;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Engine(ILog log)
|
||||
{
|
||||
this.log = log;
|
||||
@ -70,13 +77,13 @@ namespace AutoFacSamles
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var builder = new ContainerBuilder();
|
||||
//builder.RegisterType<ConsoleLog>().As<ILog>();
|
||||
var log = new ConsoleLog();
|
||||
builder.RegisterInstance(log).As<ILog>(); //val av redan instantierat objekt
|
||||
builder.RegisterType<ConsoleLog>().As<ILog>();
|
||||
|
||||
builder.RegisterType<Engine>();
|
||||
builder.RegisterType<Car>()
|
||||
.UsingConstructor(typeof(Engine)); //val av contructor
|
||||
builder.Register( c =>
|
||||
new Engine(c.Resolve<ILog>(), 123));
|
||||
|
||||
//builder.RegisterType<Engine>();
|
||||
builder.RegisterType<Car>();
|
||||
|
||||
IContainer container = builder.Build();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user