Click here to Skip to main content
15,895,011 members
Articles / Programming Languages / C# 5.0
Tip/Trick

How to register and use Multiple Classes Implementing Same Interface in AutoFac?

Rate me:
Please Sign up or sign in to vote.
2.75/5 (3 votes)
28 Jan 2015CPOL1 min read 50.6K   5   8
Here I will show you how we can register and use multiple classes implementing same interface when Autofac is used as dependencies registrar.

Introduction

Here I will show you how we can register and use multiple classes implementing same interface when Autofac is used as dependencies registrar.

Background

If multiple classes implementing same interface, then how we can get the desired implementation in our business class while using Autofac as dependencies registrar? This is issue which needs to be handle.

Lets say we have one interface:

C#
public interface ICake
{
	decimal GetPrice();
}

And two classes implementing this interface.

First Class:

C#
public class PlainCake : ICake
{
	public PlainCake()
	{
	}
	public decimal GetPrice()
	{
		return 30.45M;
	}
}

Second Class:

C#
public class FruitCake : ICake
{
	public FruitCake()
	{
	}
	public decimal GetPrice()
	{
		return 50.8M;
	}
}

In dependencies registrar we register our classes with interface like this:

C#
builder.RegisterType<PlainCake>().As<ICake>().InstancePerRequest();
builder.RegisterType<FruitCake>().As<ICake>().InstancePerRequest();

Now in our business class we create an object like this:

C#
private readonly ICake _Cake;

Now, here how we can get the GetPrice method implementation of PlainCake class and FruitCake class with this object???

Using the Code

Here is a work around and we can handle such situations with little bit decorating our interface like this:

Now our interface will look like this:

C#
public interface ICake<T> where T : class
{
	decimal GetPrice();
}

and our classes will look like this:

First Class:

C#
public class PlainCake : ICake<PlainCake>
{
	public PlainCake()
	{
	}
	public decimal GetPrice()
	{
		return 30.45M;
	}
}

Second Class:

C#
public class FruitCake : ICake<FruitCake>
{
	public FruitCake()
	{
	}
	public decimal GetPrice()
	{
		return 50.8M;
	}
}

In dependencies registrar  we can register them like this:

C#
builder.RegisterType<PlainCake>().As<ICake<PlainCake>>().InstancePerRequest();
builder.RegisterType<FruitCake>().As<ICake<FruitCake>>().InstancePerRequest();

In our business class we can call them like this:

C#
private readonly ICake<PlainCake> _plainCake;
private readonly ICake<FruitCake> _fruitCake;

public EvaluationBusiness(ICake<PlainCake> plaincake, ICake<FruitCake> fruitCake)
{
_plainCake = plaincake;
_fruitCake = fruitCake;
}

and calling GetPrice method.

C#
var _plainCakePrice = _plainCake.GetPrice();

var _fruitCakePrice = _fruitCake.GetPrice();

It is my implementation to handle such situation. (Our goal was not to reduce the number of lines of code). There will be a lot of solutions to handle such situations and hope so you will be handling this situation too with some fixes.

I hope it will help someone or someone have more better solutions to handle such situations.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Pakistan Pakistan
This member doesn't quite have enough reputation to be able to display their biography and homepage.

Comments and Discussions

 
QuestionClean Architecture Pin
Member 1457250829-Aug-19 8:07
Member 1457250829-Aug-19 8:07 
QuestionDifferent appoach Pin
quybmt200322-Aug-16 23:12
quybmt200322-Aug-16 23:12 
Suggestion[My vote of 2] Different approach to solve multiple implementations using Autofac. Pin
Amila Prabandhika Palahepitiya Gamage30-Jan-15 14:39
Amila Prabandhika Palahepitiya Gamage30-Jan-15 14:39 
GeneralRe: [My vote of 2] Different approach to solve multiple implementations using Autofac. Pin
_Rizwan_Khalid_1-Feb-15 19:38
_Rizwan_Khalid_1-Feb-15 19:38 
GeneralRe: [My vote of 2] Different approach to solve multiple implementations using Autofac. Pin
Amila/172-Feb-15 9:49
Amila/172-Feb-15 9:49 
GeneralRe: [My vote of 2] Different approach to solve multiple implementations using Autofac. Pin
_Rizwan_Khalid_3-Feb-15 1:00
_Rizwan_Khalid_3-Feb-15 1:00 
GeneralRe: [My vote of 2] Different approach to solve multiple implementations using Autofac. Pin
Amila/173-Feb-15 4:40
Amila/173-Feb-15 4:40 
SuggestionThis solution is a bit limiting Pin
RobTeixeira29-Jan-15 11:02
RobTeixeira29-Jan-15 11:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.