From b469188f2842618a2ba093542d225a3aaeb2adb6 Mon Sep 17 00:00:00 2001 From: syneffort Date: Thu, 11 May 2023 13:56:53 +0900 Subject: [PATCH] di injection --- MySolution/ConsoleApp/Program.cs | 17 +++++- .../ConsoleApp/Samples/CalculateSample.cs | 2 +- MySolution/ConsoleApp/Samples/DISample.cs | 58 +++++++++++++++++++ MySolution/ConsoleApp/Samples/EventSample.cs | 2 +- .../Samples/MessagePrinterSample.cs | 2 +- ...{StringLength.cs => StringLengthSample.cs} | 4 +- 6 files changed, 77 insertions(+), 8 deletions(-) create mode 100644 MySolution/ConsoleApp/Samples/DISample.cs rename MySolution/ConsoleApp/Samples/{StringLength.cs => StringLengthSample.cs} (93%) diff --git a/MySolution/ConsoleApp/Program.cs b/MySolution/ConsoleApp/Program.cs index 1e4a126..53dc347 100644 --- a/MySolution/ConsoleApp/Program.cs +++ b/MySolution/ConsoleApp/Program.cs @@ -1,4 +1,8 @@ -using Samples; +using Samples.Calculate; +using Samples.DI; +using Samples.Event; +using Samples.MessagePrinter; +using Samples.StringLength; namespace ConsoleApp; class Program @@ -15,16 +19,23 @@ class Program #region Callback - // StringLength.Sample(); + // StringLengthSample.Sample(); #endregion #region Event - EventSample.Sample(); + // EventSample.Sample(); #endregion + + #region Dependency Injection + + DISample.Sample(); + + #endregion + } } diff --git a/MySolution/ConsoleApp/Samples/CalculateSample.cs b/MySolution/ConsoleApp/Samples/CalculateSample.cs index c9e69ca..86055bb 100644 --- a/MySolution/ConsoleApp/Samples/CalculateSample.cs +++ b/MySolution/ConsoleApp/Samples/CalculateSample.cs @@ -1,6 +1,6 @@ using System; -namespace Samples +namespace Samples.Calculate { class CalculateSample { diff --git a/MySolution/ConsoleApp/Samples/DISample.cs b/MySolution/ConsoleApp/Samples/DISample.cs new file mode 100644 index 0000000..d71278c --- /dev/null +++ b/MySolution/ConsoleApp/Samples/DISample.cs @@ -0,0 +1,58 @@ +namespace Samples.DI +{ + class DISample + { + public static void Sample() + { + DIContainer container = new DIContainer(); + container.Register(); + + Client client = new Client(container.Resolve()); + client.Run(); + } + } + + public interface IService + { + void DoSomething(); + } + + public class Service : IService + { + public void DoSomething() + { + Console.WriteLine("Service is doing something..."); + } + } + + public class Client + { + private readonly IService _service; + + public Client(IService service) + { + _service = service; + } + + public void Run() + { + _service.DoSomething(); + } + } + + public class DIContainer + { + private readonly Dictionary _typemappings = new Dictionary(); + + public void Register() + { + _typemappings[typeof(TInterface)] = typeof(TImplementation); + } + + public TInterface Resolve() + { + Type implementationType = _typemappings[typeof(TInterface)]; + return (TInterface)Activator.CreateInstance(implementationType); + } + } +} \ No newline at end of file diff --git a/MySolution/ConsoleApp/Samples/EventSample.cs b/MySolution/ConsoleApp/Samples/EventSample.cs index 38df234..8539f7d 100644 --- a/MySolution/ConsoleApp/Samples/EventSample.cs +++ b/MySolution/ConsoleApp/Samples/EventSample.cs @@ -1,6 +1,6 @@ using System; -namespace Samples +namespace Samples.Event { public delegate void CustomEventHandler(object sender, EventArgs e); diff --git a/MySolution/ConsoleApp/Samples/MessagePrinterSample.cs b/MySolution/ConsoleApp/Samples/MessagePrinterSample.cs index 031934c..524ab5b 100644 --- a/MySolution/ConsoleApp/Samples/MessagePrinterSample.cs +++ b/MySolution/ConsoleApp/Samples/MessagePrinterSample.cs @@ -2,7 +2,7 @@ using System; using System.Diagnostics; using System.Threading.Tasks; -namespace Samples +namespace Samples.MessagePrinter { class MessagePrinterSample { diff --git a/MySolution/ConsoleApp/Samples/StringLength.cs b/MySolution/ConsoleApp/Samples/StringLengthSample.cs similarity index 93% rename from MySolution/ConsoleApp/Samples/StringLength.cs rename to MySolution/ConsoleApp/Samples/StringLengthSample.cs index 03e1ea5..cb5cc3f 100644 --- a/MySolution/ConsoleApp/Samples/StringLength.cs +++ b/MySolution/ConsoleApp/Samples/StringLengthSample.cs @@ -1,8 +1,8 @@ using System; -namespace Samples +namespace Samples.StringLength { - class StringLength + class StringLengthSample { public static void Sample() {