People are often confused about what Dependency Injection is and when they might need or want to use it. Some time ago I wrote an article Managing Dependency Injection in C# with Autofac which explains how to manage DI in C#, but today I want to show by simple code sample what actually Dependency Injection is.
Imaging situation where you have a class, let's say Employee, and two or more different loggers for that class. Each logger prints messages in his own particular way and you want to have control of which logger to use for Employee during its instantiation.
Just take a look at this code and I'm sure you will get the idea of Dependency Injection:
Imaging situation where you have a class, let's say Employee, and two or more different loggers for that class. Each logger prints messages in his own particular way and you want to have control of which logger to use for Employee during its instantiation.
Just take a look at this code and I'm sure you will get the idea of Dependency Injection:
public class Employee { public Employee(ILogger logger) { logger.WriteToLog("New employee created"); } } public interface ILogger { void WriteToLog(string text); } public class LoggerOne : ILogger { public void WriteToLog(string text) { Console.WriteLine(text); } } public class LoggerTwo : ILogger { public void WriteToLog(string text) { Console.WriteLine("***********\n {0}\n***********", text); } }
Now let's instantiate an Employee with two different loggers:
Employee employee1 = new Employee(new LoggerOne()); Employee employee2 = new Employee(new LoggerTwo());
And the output:
New employee created ******************* New employee created *******************
Simply awesome best example i have ever searched. Keep up
ReplyDeleteBest and to the point. Simplicity at its best. It took me 5 minutes to understand Constructor DI.
ReplyDeleteshort and sweet example!
ReplyDeleteThanks!!!! Very best example
ReplyDeleteSimply the best. Thank you very much!
ReplyDeleteYou can Change Title to 'How to learn DI in 5mins'. Simply great...
ReplyDeleteAwesome..
ReplyDeleteCooooool! Thank you!
ReplyDeleteReally easy to understand. Thanks!
ReplyDeleteThanks for this crispy article. Helpful !
ReplyDeleteNice Explanation. But can you explain this using Unity Application Block?
ReplyDeleteThank you, simply genius.
ReplyDeleteLost a lot of time around the net and did not understand how DI works, and then you manage to explain it in such a simple way...
ReplyDeletethank you very much!
very good :) thanks
ReplyDeletenice :)
ReplyDeletethank you! it's very simple
ReplyDeleteThank you.it very simple to understand DI
ReplyDeletegreat
ReplyDeleteThats what I was looking for. Simple example and easy to understand. Once I know what DI is made for I can go forward with some more details. Thanks a lot!
ReplyDeleteThats what I was looking for. Simple example and easy to understand. Once I know what DI is made for I can go forward with some more details. Thanks a lot!
ReplyDeleteEasy And Best Way.
ReplyDeleteThanks alot...
Good.. Many post i showed but from this blog i am able to understand how we can implement DI concept in development enviroment
ReplyDeleteBest
ReplyDelete