Last active
September 27, 2021 12:32
-
-
Save ruselknow/79abb817e70b919c33fcb8c86ce3dfa8 to your computer and use it in GitHub Desktop.
Property Injection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| namespace DependencyInjection | |
| { | |
| public interface IDependent | |
| { | |
| void DoSomething(); | |
| } | |
| public class Dependent1 : IDependent | |
| { | |
| public void DoSomething() | |
| { | |
| Console.WriteLine("Hello from Dependent1"); | |
| } | |
| } | |
| public class Dependent2 : IDependent | |
| { | |
| public void DoSomething() | |
| { | |
| Console.WriteLine("Hello from Dependent2"); | |
| } | |
| } | |
| public class MainClass | |
| { | |
| public IDependent dependent { get; set; } | |
| public void DoSomething() | |
| { | |
| dependent.DoSomething(); | |
| } | |
| } | |
| internal static class Program | |
| { | |
| private static void Main() | |
| { | |
| // Get the correct dependency | |
| var dependency = GetCorrectDependency(); | |
| // Create class and inject the dependency | |
| var mainClass = new MainClass | |
| { | |
| dependent = dependency | |
| }; | |
| mainClass.DoSomething(); | |
| Console.ReadLine(); | |
| } | |
| private static IDependent GetCorrectDependency() | |
| { | |
| return new Dependent1(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment