can you help me with theory of some patterns. I have tried to describe them, I have tried my best, but I think my statements are wrong, so help )).
1) "DI" and "IOC" - the same.
2) "IOC Container" - it is an instance of an object that can resolve dependences like:
void Test()
{
// create IOC Container to resolve
// dependences for SomeMethod method
var container = new SomeContainer();
container.For(typeof(IEmaleSender), typeof(SuperEmaleSender));
// Pass IOC Container to resolve dependences in SomeMethod
SomeMethod(container);
}
void SomeMethod(SomeContainer container)
{
IEmaleSender emailSender = container.Resolve(IEmaleSender);
emailSender.SendEmail();
}
3) "Service Locator" - It is something like static object that contains Dictionary<Type, object>
where value is an instance of key type. And this static object have 2 methods: Add
and Get
. So I can add object on start of my application and request it from everywhere:
void Test()
{
// Assign instanse of SuperEmaleSender to Locator
SuperEmaleSender emailSender = new SuperEmaleSender()
SomeLocator.Add(typeof(SuperEmaleSender), emailSender);
SomeMethod();
}
void SomeMethod()
{
SuperEmaleSender emailSender = SomeLocator.Get(typeof(SuperEmaleSender));
emailSender.SendEmail();
}
4) It is a good practice to combine "Service Locator" and "IOC Container". So you can instantiate "IOC Container" on application start and request it through "Service Locator" from everywhere.
5) In ASP MVC5, "Service Locator" already included. I'm talking about DependencyResolver
.
Thank you for your help.