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 ILog log;
|
||||||
private int id;
|
private int id;
|
||||||
|
|
||||||
|
public Engine(ILog log, int id)
|
||||||
|
{
|
||||||
|
this.log = log;
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
public Engine(ILog log)
|
public Engine(ILog log)
|
||||||
{
|
{
|
||||||
this.log = log;
|
this.log = log;
|
||||||
@ -70,13 +77,13 @@ namespace AutoFacSamles
|
|||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
var builder = new ContainerBuilder();
|
var builder = new ContainerBuilder();
|
||||||
//builder.RegisterType<ConsoleLog>().As<ILog>();
|
builder.RegisterType<ConsoleLog>().As<ILog>();
|
||||||
var log = new ConsoleLog();
|
|
||||||
builder.RegisterInstance(log).As<ILog>(); //val av redan instantierat objekt
|
|
||||||
|
|
||||||
builder.RegisterType<Engine>();
|
builder.Register( c =>
|
||||||
builder.RegisterType<Car>()
|
new Engine(c.Resolve<ILog>(), 123));
|
||||||
.UsingConstructor(typeof(Engine)); //val av contructor
|
|
||||||
|
//builder.RegisterType<Engine>();
|
||||||
|
builder.RegisterType<Car>();
|
||||||
|
|
||||||
IContainer container = builder.Build();
|
IContainer container = builder.Build();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user