How to resolve multiple dependencies for the same interface in a constructor using Unity

QU:-How to register and use Multiple Classes Implementing Same Interface in Unity?

Qu:- How to resolve multiple dependencies for the same interface in a constructor using Unity

While discussing with one of my friend we have came across this question –  how to register and use multiple classes implementing same interface in unity. For that  reason i am writing this article, hope this will help to understand the concepts.

Here we will see this concepts can be implemented using

1) Using Generic Class  — Part I

2) Using ‘Named registration’ & ‘[Dependency]’ attribute  — Part II

Prerequisites: MVC 5 and Unity libraries

Part I :- Using Generic class

Interface:-  ITest


public interface ITest<T> where T : class
// public interface ITest
{
int MyProperty1 { get; set; }
int MyProperty2 { get; set; }
}

Class:- ClsTest


public class ClsTest : ITest<ClsTest>
{
public int _myproperty1 = 20;
public int _myproperty2 = 20;

public int MyProperty1
{
get
{
return _myproperty1;
}
set
{
_myproperty1 = value;
}
}

public int MyProperty2
{
get
{
return _myproperty2;
}
set
{
_myproperty2 = value;
}
}
}

Class:- ClsDemo


public class Clsdemo :ITest<Clsdemo>
{
public int _myproperty1 = 10;
public int _myproperty2 = 10;

public int MyProperty1
{
get
{
return _myproperty1;
}
set
{
_myproperty1 = value;
}
}

public int MyProperty2
{
get
{
return _myproperty2;
}
set
{
_myproperty2 = value;
}
}

}

Register unity in Global.asax file:-


public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();

//Register Unity
UnityConfig.RegisterComponents();

FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}

Unity Class:-  UnityConfig


public static class UnityConfig
{
public static void RegisterComponents()
{
var container = new UnityContainer();

// register all your components with the container here
// it is NOT necessary to register your controllers
// e.g. container.RegisterType<ITestService, TestService>();

container.RegisterType<ITest, ClsTest>();
container.RegisterType<ITest, Clsdemo>();

DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}
}

Controller :-TestController


public class TestController : Controller
{

//ITest iTobj;
ITest<ClsTest> iTobj;
ITest<Clsdemo> iTobj1;

// public TestController(ITest iT)
//public TestController(ITest<ClsTest> iT)
public TestController(ITest<ClsTest> iT, ITest<Clsdemo> Obj1)
{
iTobj = iT;
iTobj1 = Obj1;

}
//
// GET: /Test/
public ActionResult Index()
{
return View();
}

}

Controller:- HomeController


public class HomeController : Controller
{
ITest<Clsdemo> obj1;
// ITest obj1;

//public HomeController(ITest<Clsdemo> Obj1)
public HomeController(ITest Obj1)
{

}
public ActionResult Index()
{
return View();
}

}

After running the above code you will able to get the class object as per the controller classes.

Note:- If you used MVC4 and added mvc4.unity from Nu-get package you will get Bootstraper.cs class file. In my case i am using MVC5 so after adding unity library i am getting  UnityConfig.cs class in App_start folder but both the unity class have the same implementation.

————————————————————————————————————————–

Part II  — Using ‘Named registration’ & ‘[Dependency]’ attribute 

UnityConfig.cs:


// "<strong>NormalAccount"</strong> is assigned as a name for RegisterType

container.RegisterType<IAccountService, AccountService>("NormalAccount");

// "<strong>SpecialAccount"</strong> is assigned as a name for RegisterType

container.RegisterType<IAccountService, SplAccountService>("SpecialAccount");

AccountInfoController.cs:


private IAccountService normalAcc;
private IAccountService specialAcc;

//  Dependency attribute is used with Named RegisterType as per requirement.

public SplAccountInfoController([Dependency("NormalAccount")]IAccountService accountService1, [Dependency("SpecialAccount")]IAccountService accountService2)
{
this.normalAcc = accountService1;
this.specialAcc = accountService2;
}

 

 

Project Estimation Technique Topics and Links

Risk Analysis

Ref:- https://www.mindtools.com/pages/article/newTMC_07.htm

Risk Mitigation Planning, Implementation, and Progress Monitoring

Ref:- https://www.mitre.org/publications/systems-engineering-guide/acquisition-systems-engineering/risk-management/risk-mitigation-planning-implementation-and-progress-monitoring

What is the difference between mitigation and contingency plan?

Ref:- https://www.izenbridge.com/blog/know-the-difference-between-mitigation-plan-and-contingency-plan/

Estimation Techniques

Ref:- https://www.tutorialspoint.com/estimation_techniques/index.htm