From c381375dbd4301ed1d4e0577a8a44f1b1cabfa91 Mon Sep 17 00:00:00 2001 From: Holger Boerchers Date: Sun, 12 Aug 2018 13:44:42 +0200 Subject: [PATCH] A lot of comments. --- Program.cs | 6 +++++- RegisteredType.cs | 17 ---------------- ServiceOne.cs | 17 +++++++++++++--- ServiceTwo.cs | 19 ++++++++++++++---- SmallInjector.cs | 42 +++++++++++++++++++++++++++++++++++----- SmallInjectorDemo.csproj | 6 ++++++ UsefulClass.cs | 22 ++++++++++++++++----- 7 files changed, 94 insertions(+), 35 deletions(-) delete mode 100644 RegisteredType.cs diff --git a/Program.cs b/Program.cs index 3490af5..1e1c944 100644 --- a/Program.cs +++ b/Program.cs @@ -1,15 +1,19 @@ using System; -namespace Playground +namespace SmallInjectorDemo { internal static class Program { private static void Main() { Console.WriteLine("Small dependency injection example."); + Console.WriteLine(); var injector = new SmallInjector(); + Console.WriteLine("Register " + nameof(ServiceOne)); injector.RegisterType(true); + Console.WriteLine("Register " + nameof(ServiceTwo)); injector.RegisterType(true); + Console.WriteLine("Register " + nameof(UsefulClass)); injector.RegisterType(false); Console.WriteLine(); diff --git a/RegisteredType.cs b/RegisteredType.cs deleted file mode 100644 index b5cfbc6..0000000 --- a/RegisteredType.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; - -namespace Playground -{ - public class RegisteredType - { - public RegisteredType(Type concreteType, bool isSingleton) - { - ConcreteType = concreteType; - IsSingleton = isSingleton; - } - - public readonly bool IsSingleton; - public readonly Type ConcreteType; - public object SingletonInstance { get; set; } - } -} \ No newline at end of file diff --git a/ServiceOne.cs b/ServiceOne.cs index 547f16c..4a4637d 100644 --- a/ServiceOne.cs +++ b/ServiceOne.cs @@ -1,20 +1,31 @@ using System; -namespace Playground +namespace SmallInjectorDemo { + /// + /// Implementation of . + /// public class ServiceOne : IServiceOne { + private const string MyName = nameof(ServiceOne); private readonly Guid _guid; + /// + /// Creates a new instance of . + /// public ServiceOne() { _guid = Guid.NewGuid(); - Console.WriteLine(nameof(ServiceOne) + ".ctor\t\tId: " + _guid); + Console.WriteLine(MyName + ".ctor\t\tId: " + _guid); } - public override string ToString() => nameof(ServiceOne) + ".ToString()\tId: " + _guid; + /// + public override string ToString() => MyName + ".ToString()\tId: " + _guid; } + /// + /// Interface for service one. + /// public interface IServiceOne { diff --git a/ServiceTwo.cs b/ServiceTwo.cs index 19f1166..52e1d2b 100644 --- a/ServiceTwo.cs +++ b/ServiceTwo.cs @@ -1,22 +1,33 @@ using System; -namespace Playground +namespace SmallInjectorDemo { + /// + /// Implementation of . + /// public class ServiceTwo : IServiceTwo { + private const string MyName = nameof(ServiceTwo); private readonly Guid _guid; + /// + /// Creates a new instance of . + /// public ServiceTwo() { _guid = Guid.NewGuid(); - Console.WriteLine(nameof(ServiceTwo) + ".ctor\t\tId: " + _guid); + Console.WriteLine(MyName + ".ctor\t\tId: " + _guid); } - public override string ToString() => nameof(ServiceTwo) + ".ToString()\tId: " + _guid; - + /// + public override string ToString() => MyName + ".ToString()\tId: " + _guid; } + /// + /// Interface for service two. + /// public interface IServiceTwo { + } } \ No newline at end of file diff --git a/SmallInjector.cs b/SmallInjector.cs index b1efa84..98a2dee 100644 --- a/SmallInjector.cs +++ b/SmallInjector.cs @@ -1,24 +1,44 @@ using System; -using System.Collections; using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; -using System.Reflection; -namespace Playground + +namespace SmallInjectorDemo { + /// + /// A small dependency injector to demonstrate the pattern. + /// public class SmallInjector { private readonly Dictionary _container = new Dictionary(); + /// + /// Register types in the resolve container. + /// + /// Type of the service. + /// Type of the interface of the service. + /// True if the service should be singleton. False otherwise. public void RegisterType(bool isSingleton) { _container.Add(typeof(TInterface), new RegisteredType(typeof(TService), isSingleton)); } + /// + /// Register types in the resolve container. + /// + /// Type of the service. public void RegisterType(bool isSingleton) => RegisterType(isSingleton); + /// + /// Resolve service of specified type. + /// + /// Type of the service. + /// A instance of the service. public TService Resolve() => (TService)Resolve(typeof(TService)); + /// + /// Resolve service of specified type. + /// + /// Type of the service. + /// A instance of the service. public object Resolve(Type serviceInterface) { if (!_container.TryGetValue(serviceInterface, out var registeredType)) @@ -41,7 +61,19 @@ namespace Playground return registeredType.SingletonInstance ?? (registeredType.SingletonInstance = constructor.Invoke(constructorParameters)); } return constructor.Invoke(constructorParameters); + } + private class RegisteredType + { + public RegisteredType(Type concreteType, bool isSingleton) + { + ConcreteType = concreteType; + IsSingleton = isSingleton; + } + + public readonly bool IsSingleton; + public readonly Type ConcreteType; + public object SingletonInstance { get; set; } } } } \ No newline at end of file diff --git a/SmallInjectorDemo.csproj b/SmallInjectorDemo.csproj index b30d547..c4c4003 100644 --- a/SmallInjectorDemo.csproj +++ b/SmallInjectorDemo.csproj @@ -5,4 +5,10 @@ SmallInjectorDemo SmallInjectorDemo + + bin\Debug\netcoreapp2.1\SmallInjectorDemo.xml + + + bin\Release\netcoreapp2.1\SmallInjectorDemo.xml + \ No newline at end of file diff --git a/UsefulClass.cs b/UsefulClass.cs index b5520a6..f11cc3b 100644 --- a/UsefulClass.cs +++ b/UsefulClass.cs @@ -1,28 +1,40 @@ using System; -namespace Playground +namespace SmallInjectorDemo { + /// + /// A very useful class. + /// public class UsefulClass { - private readonly IServiceOne _service; + private readonly IServiceOne _service1; private readonly IServiceTwo _service2; private readonly Guid _guid; - public UsefulClass(IServiceOne service, IServiceTwo service2) + /// + /// Creates a new instance of . + /// + /// injected service one. + /// injected service two. + public UsefulClass(IServiceOne service1, IServiceTwo service2) { - _service = service; + _service1 = service1; _service2 = service2; _guid = Guid.NewGuid(); Console.WriteLine(nameof(UsefulClass) + ".ctor\tId: " + _guid); } + /// + /// Test the injected services. + /// public void TestTheServices() { Console.WriteLine(ToString()); - Console.WriteLine(_service.ToString()); + Console.WriteLine(_service1.ToString()); Console.WriteLine(_service2.ToString()); } + /// public override string ToString() => nameof(UsefulClass) + ".ToString()\tId: " + _guid; }