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:
- What is Container in Unity?
- What is Resolve?
- What is RegisterType.
- What is meant by Type Mapping?
- What is IOC
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!