Could someone please help me to understand what Unity is and how it's simplifying the coding on example below:
Normal Code
FileLogger myLogger = new FileLogger();
FileLogger myLogger = new FastLogger();
Unity Container Code
// Create container and register types
IUnityContainer myContainer = new UnityContainer();
myContainer.RegisterType<ILogger, FileLogger>(); // default instance
myContainer.RegisterType<ILogger, FastFileLogger>("FastLogger");
ILogger myLogger = myContainer.Resolve<ILogger>();
Also:
Also what happens if two classes implement the same interface and we do something like below on Unity:
container.RegisterType<IInvoicingService, InvoicingService>()
.RegisterType<IInvoicingService, ManagerService>();
IInvoicingService service = container.Resolve<IInvoicingService>();
service.GetCount();
Looks like it's going to invoke the getCount
method on ManagerService
. What should I do to invoke GetCount
on InvoicingService
and ManagerService
?
Yes I have read the documentatation on CodePlex, it just confused me a lot!
For IOC and / or Dependency Injection i can recommend this read:
http://martinfowler.com/articles/injection.html
This should help you get a better understanding of what you can achieve by using this patterns. Unity helps you implement this patterns in .NET.
I will try to answer your bulleted questions with best of my knowledge: