diff --git a/InjectorTest/HelperTest.cs b/InjectorTest/HelperTest.cs index 4f524e2..8b7d026 100644 --- a/InjectorTest/HelperTest.cs +++ b/InjectorTest/HelperTest.cs @@ -12,7 +12,5 @@ namespace InjectorTest var actual = Helper.WriteMethodString(new TestClock(), ""); Assert.Equal(expected, actual); } - - } } diff --git a/InjectorTest/InjectorTest.csproj b/InjectorTest/InjectorTest.csproj index 8e0ee52..db6d839 100644 --- a/InjectorTest/InjectorTest.csproj +++ b/InjectorTest/InjectorTest.csproj @@ -1,7 +1,7 @@ - netcoreapp2.2 + netcoreapp2.1 false diff --git a/SmallInjectorDemo/SmallInjector.cs b/SmallInjector/Container.cs similarity index 65% rename from SmallInjectorDemo/SmallInjector.cs rename to SmallInjector/Container.cs index 84f9802..1a3e29c 100644 --- a/SmallInjectorDemo/SmallInjector.cs +++ b/SmallInjector/Container.cs @@ -1,45 +1,28 @@ using System; using System.Collections.Generic; -namespace SmallInjectorDemo +namespace SmallInjector { /// /// A small dependency injector to demonstrate the pattern. /// - public class SmallInjector + public class Container : IContainer { 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. - /// instance of the service + /// public void RegisterType(bool isSingleton, TService instance = default) where TService : TInterface { _container.Add(typeof(TInterface), new RegisteredType(typeof(TService), isSingleton, null)); } - /// - /// Register types in the resolve container. - /// - /// Type of the service. + /// public void RegisterType(bool isSingleton, TService instance = default) => RegisterType(isSingleton, instance); - /// - /// 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)) diff --git a/SmallInjector/IContainer.cs b/SmallInjector/IContainer.cs new file mode 100644 index 0000000..0c21f9a --- /dev/null +++ b/SmallInjector/IContainer.cs @@ -0,0 +1,34 @@ +using System; + +namespace SmallInjector +{ + /// + /// DI Container + /// + public interface IContainer + { + /// + /// 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. + /// instance of the service + void RegisterType(bool isSingleton, TService instance = default) where TService : TInterface; + + /// + /// Register types in the resolve container. + /// + void RegisterType(bool isSingleton, TService instance = default); + + /// + /// Resolve service of specified type. + /// + object Resolve(Type serviceInterface); + + /// + /// Resolve service of specified type. + /// + TService Resolve(); + } +} \ No newline at end of file diff --git a/SmallInjector/SmallInjector.csproj b/SmallInjector/SmallInjector.csproj new file mode 100644 index 0000000..ef6f447 --- /dev/null +++ b/SmallInjector/SmallInjector.csproj @@ -0,0 +1,15 @@ + + + + netstandard2.0 + + + + C:\Users\Holger\Desktop\SmallInjectorDemo\SmallInjector\SmallInjector.xml + + + + C:\Users\Holger\Desktop\SmallInjectorDemo\SmallInjector\SmallInjector.xml + + + diff --git a/SmallInjectorDemo.sln b/SmallInjectorDemo.sln index 93dd653..2615959 100644 --- a/SmallInjectorDemo.sln +++ b/SmallInjectorDemo.sln @@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmallInjectorDemo", "SmallI EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InjectorTest", "InjectorTest\InjectorTest.csproj", "{0648782E-EB73-4326-9471-CE386C1FBC4D}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SmallInjector", "SmallInjector\SmallInjector.csproj", "{8D52C856-A71D-4C50-832B-8679CDD030B4}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -41,6 +43,18 @@ Global {0648782E-EB73-4326-9471-CE386C1FBC4D}.Release|x64.Build.0 = Release|Any CPU {0648782E-EB73-4326-9471-CE386C1FBC4D}.Release|x86.ActiveCfg = Release|Any CPU {0648782E-EB73-4326-9471-CE386C1FBC4D}.Release|x86.Build.0 = Release|Any CPU + {8D52C856-A71D-4C50-832B-8679CDD030B4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8D52C856-A71D-4C50-832B-8679CDD030B4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8D52C856-A71D-4C50-832B-8679CDD030B4}.Debug|x64.ActiveCfg = Debug|Any CPU + {8D52C856-A71D-4C50-832B-8679CDD030B4}.Debug|x64.Build.0 = Debug|Any CPU + {8D52C856-A71D-4C50-832B-8679CDD030B4}.Debug|x86.ActiveCfg = Debug|Any CPU + {8D52C856-A71D-4C50-832B-8679CDD030B4}.Debug|x86.Build.0 = Debug|Any CPU + {8D52C856-A71D-4C50-832B-8679CDD030B4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8D52C856-A71D-4C50-832B-8679CDD030B4}.Release|Any CPU.Build.0 = Release|Any CPU + {8D52C856-A71D-4C50-832B-8679CDD030B4}.Release|x64.ActiveCfg = Release|Any CPU + {8D52C856-A71D-4C50-832B-8679CDD030B4}.Release|x64.Build.0 = Release|Any CPU + {8D52C856-A71D-4C50-832B-8679CDD030B4}.Release|x86.ActiveCfg = Release|Any CPU + {8D52C856-A71D-4C50-832B-8679CDD030B4}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SmallInjectorDemo/Helper.cs b/SmallInjectorDemo/Helper.cs index 972d853..64ecbf1 100644 --- a/SmallInjectorDemo/Helper.cs +++ b/SmallInjectorDemo/Helper.cs @@ -12,11 +12,6 @@ namespace SmallInjectorDemo /// public static class Helper { - private const byte ForwardSlashByte = (byte)'/'; - private const byte DashByte = (byte)'-'; - private const byte PlusByte = (byte)'+'; - private const byte UnderscoreByte = (byte)'_'; - /// /// Encode Base64 string /// @@ -28,16 +23,6 @@ namespace SmallInjectorDemo MemoryMarshal.TryWrite(guidBytes, ref guid); // write bytes from the Guid Base64.EncodeToUtf8(guidBytes, encodedBytes, out _, out _); - // replace any characters which are not URL safe - for (var i = 0; i < 22; i++) - { - if (encodedBytes[i] == ForwardSlashByte) - encodedBytes[i] = DashByte; - - if (encodedBytes[i] == PlusByte) - encodedBytes[i] = UnderscoreByte; - } - // skip the last two bytes as these will be '==' padding var final = Encoding.UTF8.GetString(encodedBytes.Slice(0, 22)); diff --git a/SmallInjectorDemo/Program.cs b/SmallInjectorDemo/Program.cs index 3bf9ee3..cc24aec 100644 --- a/SmallInjectorDemo/Program.cs +++ b/SmallInjectorDemo/Program.cs @@ -1,4 +1,5 @@ using System; +using SmallInjector; namespace SmallInjectorDemo { @@ -7,21 +8,22 @@ namespace SmallInjectorDemo private static void Main() { Console.WriteLine("Small dependency injection example"); - var injector = new SmallInjector(); + IContainer container = new Container(); + container.RegisterType(true, container); Console.WriteLine("Register " + nameof(SystemClock)); - injector.RegisterType(true); + container.RegisterType(true); Console.WriteLine("Register " + nameof(ServiceOne)); - injector.RegisterType(true); + container.RegisterType(true); Console.WriteLine("Register " + nameof(ServiceTwo)); - injector.RegisterType(true); + container.RegisterType(true); Console.WriteLine("Register " + nameof(ServiceConsumer)); - injector.RegisterType(false); + container.RegisterType(false); Console.WriteLine(); Console.WriteLine("Resolve class instances:"); - var useful1 = injector.Resolve(); - var useful2 = injector.Resolve(); - var useful3 = injector.Resolve(); + var useful1 = container.Resolve(); + var useful2 = container.Resolve(); + var useful3 = container.Resolve(); Console.WriteLine(); Console.WriteLine("Run test methods:"); useful1.TestTheServices(); diff --git a/SmallInjectorDemo/SmallInjectorDemo.csproj b/SmallInjectorDemo/SmallInjectorDemo.csproj index 9627fca..63e1361 100644 --- a/SmallInjectorDemo/SmallInjectorDemo.csproj +++ b/SmallInjectorDemo/SmallInjectorDemo.csproj @@ -1,7 +1,7 @@  Exe - netcoreapp2.2 + netcoreapp2.1 SmallInjectorDemo SmallInjectorDemo latest @@ -12,4 +12,7 @@ bin\Release\netcoreapp2.1\SmallInjectorDemo.xml + + + \ No newline at end of file