Simple example 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); } } Employee employee1 = new Employee(new LoggerOne()); Employee employee2 = new Employee(new LoggerTwo()); // OUTPUT // New employee created // // ******************* // New employee created // *******************

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.