Dependency Injection with Autofac : Named Registration

I’m really having fun with Autofac and It is a great IOC container.  As its tag line suggest it it a addictive IOC container.  This post will be a part of Autofac series. If you have not gone through my previous blog post then I would suggest you go through it. Following is a list of blog post I’ve written for Autofac series.

Dependency Injection with Autofac : Getting Started
Dependency Injection with Autofac : Constructor Injection
Dependency Injection with Autofac : Module Feature
Dependency Injection with Autofac : Registration Ordering

Named Registration:

Named registration can be very useful when you have to create multiple object of same type with different states. Autofac provides that functionalities with Named Registration. I’m going to create a sample console application and added Autofac via Nuget package.  To demonstrate named registration I’ve created customer class like following.

Now let’s write some code to create named instance like following.
As you can see in above code, I have created a named registration with build container and then I have resolve the instance with same name which I have done registration for it. Then I populated properties of Customer and Printed that customer with “Print” function.
One thing to note here that when you register types with named instance then it can not be resolved without name. If try to resolve it it will give error.
Now when you run this application. It will print Customer as expected.


That’s it. Hope you like it. Stay tuned for more!.
You can find complete source code of  this Autofac series on Github at -


Popular posts from this blog

How to create Rest API(Web API) with ASP.NET Core 1.0

Creating PDF with ASP.Net MVC and RazorPDF

Solution: gulp is not recognized as internal or external command- Visual Studio