Search This Blog

Thursday, July 21, 2011

Autofac register class with different constructors; Autofac registering and constructor priority

This examples exaplains the basic operation of constructor priority in Autofac which is a dependency injection framework simmilar to spring.

 

class MemoChecker{
    public MemoChecker(IList<Memo> memos, IMemoDueNotifier notifier){}

    public MemoChecker(IList<Memo> memos, IMemoDueNotifier notifier, string unnecessaryParam){}
}


var builder = new ContainerBuilder();
builder.Register(c => new MemoChecker(c.Resolve<IList<Memo>>(), c.Resolve<IMemoDueNotifier>(), "aa")).InstancePerLifetimeScope();
builder.Register(c => new MemoChecker(c.Resolve<IList<Memo>>(), c.Resolve<IMemoDueNotifier>())).InstancePerLifetimeScope();
using (var cd = _container.BeginLifetimeScope())
{
    cd.Resolve<MemoChecker>().identifyItslef();
}

Now:
this - MemoChecker(IList<Memo> memos, IMemoDueNotifier notifier) constructor will be called


builder.Register(c => new MemoChecker(c.Resolve<IList<Memo>>(), c.Resolve<IMemoDueNotifier>(), "aa")).InstancePerLifetimeScope();
builder.Register(c => new MemoChecker(c.Resolve<IList<Memo>>(), c.Resolve<IMemoDueNotifier>())).InstancePerLifetimeScope();
builder.Register(c => new MemoChecker(c.Resolve<IList<Memo>>(), c.Resolve<IMemoDueNotifier>(), "aa")).InstancePerLifetimeScope();

Now:
this - MemoChecker(IList<Memo> memos, IMemoDueNotifier notifier, string unnecessaryParam) constructor will be called


===================================================

New constructors:
public MemoChecker(IList<Memo> memos, IMemoDueNotifier notifier){}
public MemoChecker(IList<Memo> memos, IMemoDueNotifier notifier, string unnecessaryParam){}


var builder = new ContainerBuilder();
builder.RegisterType<MemoChecker>().InstancePerLifetimeScope();
builder.RegisterType<PrintingNotifier>().As<IMemoDueNotifier>();
builder.RegisterInstance(memos).As<IList<Memo>>();
builder.RegisterInstance(Console.Out).As<TextWriter>().ExternallyOwned();
using (var cd = _container.BeginLifetimeScope())
{
    cd.Resolve<MemoChecker>().identifyItslef();
}

Now:
this - MemoChecker(IList<Memo> memos, IMemoDueNotifier notifier) constructor will be called

===================================================

New constructors:
public MemoChecker(IList<Memo> memos, IMemoDueNotifier notifier){}
public MemoChecker(IList<Memo> memos, IMemoDueNotifier notifier, TextWriter unnecessaryParam){}


var builder = new ContainerBuilder();
builder.RegisterType<MemoChecker>().InstancePerLifetimeScope();
builder.RegisterType<PrintingNotifier>().As<IMemoDueNotifier>();
builder.RegisterInstance(memos).As<IList<Memo>>();
builder.RegisterInstance(Console.Out).As<TextWriter>().ExternallyOwned();
using (var cd = _container.BeginLifetimeScope())
{
    cd.Resolve<MemoChecker>().identifyItslef();
}

Now:
this - MemoChecker(IList<Memo> memos, IMemoDueNotifier notifier, TextWriter unnecessaryParam) constructor will be called

No comments:

Post a Comment

If you like this post, please leave a comment :)