diff --git a/InjectorTest/HelperTest.cs b/InjectorTest/HelperTest.cs new file mode 100644 index 0000000..49c64bf --- /dev/null +++ b/InjectorTest/HelperTest.cs @@ -0,0 +1,18 @@ +using SmallInjectorDemo; +using Xunit; + +namespace InjectorTest +{ + public class HelperTest + { + [Fact] + public void Test() + { + const string expected = "[-] HelperTest.Test Id: 0"; + var actual = Helper.WriteMethodString(new TestClock(), 0); + Assert.Equal(expected, actual); + } + + + } +} diff --git a/InjectorTest/InjectorTest.csproj b/InjectorTest/InjectorTest.csproj new file mode 100644 index 0000000..93db71c --- /dev/null +++ b/InjectorTest/InjectorTest.csproj @@ -0,0 +1,22 @@ + + + + netcoreapp2.1 + + false + + + + + + + all + runtime; build; native; contentfiles; analyzers + + + + + + + + diff --git a/InjectorTest/TestClock.cs b/InjectorTest/TestClock.cs new file mode 100644 index 0000000..27756e4 --- /dev/null +++ b/InjectorTest/TestClock.cs @@ -0,0 +1,9 @@ +using SmallInjectorDemo; + +namespace InjectorTest +{ + public class TestClock : IClock + { + public string CurrentDateTime => "-"; + } +} \ No newline at end of file diff --git a/RandomHelper.cs b/RandomHelper.cs deleted file mode 100644 index c43feeb..0000000 --- a/RandomHelper.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using System.Security.Cryptography; - -namespace SmallInjectorDemo -{ - /// - /// Static helper class for generating random numbers. - /// - public static class RandomHelper - { - /// - /// Return cryptographic stable random integer. - /// - /// Lower border of result. - /// Upper border of result. - /// Random integer. - public static int NewRandomInteger(int min, int max) - { - // The random number provider. - using (var rand = new RNGCryptoServiceProvider()) - { - uint scale = uint.MaxValue; - while (scale == uint.MaxValue) - { - // Get four random bytes. - var four_bytes = new byte[4]; - rand.GetBytes(four_bytes); - - // Convert that into an uint. - scale = BitConverter.ToUInt32(four_bytes, 0); - } - - // Add min to the scaled difference between max and min. - return (int)(min + (max - min) * - (scale / (double)uint.MaxValue)); - - } - } - } -} \ No newline at end of file diff --git a/SmallInjectorDemo.sln b/SmallInjectorDemo.sln index c2d049d..93dd653 100644 --- a/SmallInjectorDemo.sln +++ b/SmallInjectorDemo.sln @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 VisualStudioVersion = 15.0.26124.0 MinimumVisualStudioVersion = 15.0.26124.0 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmallInjectorDemo", "SmallInjectorDemo.csproj", "{620CC001-7DF9-4233-AFC2-187FD9144835}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmallInjectorDemo", "SmallInjectorDemo\SmallInjectorDemo.csproj", "{620CC001-7DF9-4233-AFC2-187FD9144835}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InjectorTest", "InjectorTest\InjectorTest.csproj", "{0648782E-EB73-4326-9471-CE386C1FBC4D}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -27,6 +29,18 @@ Global {620CC001-7DF9-4233-AFC2-187FD9144835}.Release|x64.Build.0 = Release|Any CPU {620CC001-7DF9-4233-AFC2-187FD9144835}.Release|x86.ActiveCfg = Release|Any CPU {620CC001-7DF9-4233-AFC2-187FD9144835}.Release|x86.Build.0 = Release|Any CPU + {0648782E-EB73-4326-9471-CE386C1FBC4D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0648782E-EB73-4326-9471-CE386C1FBC4D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0648782E-EB73-4326-9471-CE386C1FBC4D}.Debug|x64.ActiveCfg = Debug|Any CPU + {0648782E-EB73-4326-9471-CE386C1FBC4D}.Debug|x64.Build.0 = Debug|Any CPU + {0648782E-EB73-4326-9471-CE386C1FBC4D}.Debug|x86.ActiveCfg = Debug|Any CPU + {0648782E-EB73-4326-9471-CE386C1FBC4D}.Debug|x86.Build.0 = Debug|Any CPU + {0648782E-EB73-4326-9471-CE386C1FBC4D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0648782E-EB73-4326-9471-CE386C1FBC4D}.Release|Any CPU.Build.0 = Release|Any CPU + {0648782E-EB73-4326-9471-CE386C1FBC4D}.Release|x64.ActiveCfg = Release|Any CPU + {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 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/.vscode/launch.json b/SmallInjectorDemo/.vscode/launch.json similarity index 100% rename from .vscode/launch.json rename to SmallInjectorDemo/.vscode/launch.json diff --git a/.vscode/tasks.json b/SmallInjectorDemo/.vscode/tasks.json similarity index 100% rename from .vscode/tasks.json rename to SmallInjectorDemo/.vscode/tasks.json diff --git a/Helper.cs b/SmallInjectorDemo/Helper.cs similarity index 71% rename from Helper.cs rename to SmallInjectorDemo/Helper.cs index f593229..92f02a8 100644 --- a/Helper.cs +++ b/SmallInjectorDemo/Helper.cs @@ -25,31 +25,31 @@ namespace SmallInjectorDemo while (scale == uint.MaxValue) { // Get four random bytes. - var four_bytes = new byte[4]; - rand.GetBytes(four_bytes); + var fourBytes = new byte[4]; + rand.GetBytes(fourBytes); // Convert that into an uint. - scale = BitConverter.ToUInt32(four_bytes, 0); + scale = BitConverter.ToUInt32(fourBytes, 0); } // Add min to the scaled difference between max and min. - return (int) (min + (max - min) * (scale / (double) uint.MaxValue)); + return (int)(min + (max - min) * (scale / (double)uint.MaxValue)); } } - /// /// Write method string. /// + /// The current clock /// id of the object /// member name /// file path /// - public static string WriteMethodString(int id, [CallerMemberName] string memberName = null, [CallerFilePath] string filepath = null) + public static string WriteMethodString(IClock clock, int id, [CallerMemberName] string memberName = null, [CallerFilePath] string filepath = null) { var classname = Path.GetFileNameWithoutExtension(filepath); - return $"{classname}.{memberName?.TrimStart('.')}".PadRight(30) + $"Id: {id}"; + return $"[{clock.CurrentDateTime}] {classname}.{memberName?.TrimStart('.')}".PadRight(30) + $"Id: {id}"; } } } \ No newline at end of file diff --git a/SmallInjectorDemo/IClock.cs b/SmallInjectorDemo/IClock.cs new file mode 100644 index 0000000..d2da1da --- /dev/null +++ b/SmallInjectorDemo/IClock.cs @@ -0,0 +1,13 @@ +namespace SmallInjectorDemo +{ + /// + /// Interface of the clock. + /// + public interface IClock + { + /// + /// Current Date-Time. + /// + string CurrentDateTime { get; } + } +} \ No newline at end of file diff --git a/Program.cs b/SmallInjectorDemo/Program.cs similarity index 88% rename from Program.cs rename to SmallInjectorDemo/Program.cs index 1483204..2102cc6 100644 --- a/Program.cs +++ b/SmallInjectorDemo/Program.cs @@ -8,6 +8,8 @@ namespace SmallInjectorDemo { Console.WriteLine("Small dependency injection example"); var injector = new SmallInjector(); + Console.WriteLine("Register " + nameof(SystemClock)); + injector.RegisterType(true); Console.WriteLine("Register " + nameof(ServiceOne)); injector.RegisterType(true); Console.WriteLine("Register " + nameof(ServiceTwo)); @@ -27,7 +29,7 @@ namespace SmallInjectorDemo useful2.TestTheServices(); Console.WriteLine(); useful3.TestTheServices(); - //Console.ReadLine(); + Console.ReadLine(); } } } \ No newline at end of file diff --git a/ServiceConsumer.cs b/SmallInjectorDemo/ServiceConsumer.cs similarity index 77% rename from ServiceConsumer.cs rename to SmallInjectorDemo/ServiceConsumer.cs index a3aa47c..fdced72 100644 --- a/ServiceConsumer.cs +++ b/SmallInjectorDemo/ServiceConsumer.cs @@ -7,6 +7,7 @@ namespace SmallInjectorDemo /// public class ServiceConsumer { + private readonly IClock _clock; private readonly IServiceOne _service1; private readonly IServiceTwo _service2; private readonly int _id; @@ -14,14 +15,16 @@ namespace SmallInjectorDemo /// /// Creates a new instance of . /// + /// The current clock /// injected service one. /// injected service two. - public ServiceConsumer(IServiceOne service1, IServiceTwo service2) + public ServiceConsumer(IClock clock, IServiceOne service1, IServiceTwo service2) { + _clock = clock; _service1 = service1; _service2 = service2; _id = Helper.NewRandomInteger(10, 99); - Console.WriteLine(Helper.WriteMethodString(_id)); + Console.WriteLine(Helper.WriteMethodString(clock, _id)); } /// @@ -35,7 +38,7 @@ namespace SmallInjectorDemo } /// - public override string ToString() => Helper.WriteMethodString(_id); + public override string ToString() => Helper.WriteMethodString(_clock, _id); } } \ No newline at end of file diff --git a/ServiceOne.cs b/SmallInjectorDemo/ServiceOne.cs similarity index 77% rename from ServiceOne.cs rename to SmallInjectorDemo/ServiceOne.cs index 17ce417..1ae6dc9 100644 --- a/ServiceOne.cs +++ b/SmallInjectorDemo/ServiceOne.cs @@ -8,19 +8,21 @@ namespace SmallInjectorDemo /// public class ServiceOne : IServiceOne { + private readonly IClock _clock; private readonly int _id; /// /// Creates a new instance of . /// - public ServiceOne() + public ServiceOne(IClock clock) { + _clock = clock; _id = Helper.NewRandomInteger(10, 99); - Console.WriteLine(Helper.WriteMethodString(_id)); + Console.WriteLine(Helper.WriteMethodString(clock, _id)); } /// - public override string ToString() => Helper.WriteMethodString(_id); + public override string ToString() => Helper.WriteMethodString(_clock, _id); } /// diff --git a/ServiceTwo.cs b/SmallInjectorDemo/ServiceTwo.cs similarity index 77% rename from ServiceTwo.cs rename to SmallInjectorDemo/ServiceTwo.cs index 2200d8f..56ac88e 100644 --- a/ServiceTwo.cs +++ b/SmallInjectorDemo/ServiceTwo.cs @@ -8,19 +8,21 @@ namespace SmallInjectorDemo /// public class ServiceTwo : IServiceTwo { + private readonly IClock _clock; private readonly int _id; /// /// Creates a new instance of . /// - public ServiceTwo() + public ServiceTwo(IClock clock) { + _clock = clock; _id = Helper.NewRandomInteger(10, 99); - Console.WriteLine(Helper.WriteMethodString(_id)); + Console.WriteLine(Helper.WriteMethodString(clock, _id)); } /// - public override string ToString() => Helper.WriteMethodString(_id); + public override string ToString() => Helper.WriteMethodString(_clock, _id); } /// diff --git a/SmallInjector.cs b/SmallInjectorDemo/SmallInjector.cs similarity index 100% rename from SmallInjector.cs rename to SmallInjectorDemo/SmallInjector.cs diff --git a/SmallInjectorDemo.csproj b/SmallInjectorDemo/SmallInjectorDemo.csproj similarity index 100% rename from SmallInjectorDemo.csproj rename to SmallInjectorDemo/SmallInjectorDemo.csproj diff --git a/SmallInjectorDemo/SystemClock.cs b/SmallInjectorDemo/SystemClock.cs new file mode 100644 index 0000000..b319c20 --- /dev/null +++ b/SmallInjectorDemo/SystemClock.cs @@ -0,0 +1,11 @@ +using System; + +namespace SmallInjectorDemo +{ + /// + public class SystemClock : IClock + { + /// + public string CurrentDateTime => DateTime.Now.ToString("O"); + } +} \ No newline at end of file diff --git a/UsefulClass.cs b/SmallInjectorDemo/UsefulClass.cs similarity index 95% rename from UsefulClass.cs rename to SmallInjectorDemo/UsefulClass.cs index 23ce77b..6eadd4e 100644 --- a/UsefulClass.cs +++ b/SmallInjectorDemo/UsefulClass.cs @@ -20,7 +20,7 @@ namespace SmallInjectorDemo { _service1 = service1; _service2 = service2; - _rand = RandomHelper.NewRandomInteger(10, 99); + _rand = Helper.NewRandomInteger(10, 99); Console.WriteLine(nameof(UsefulClass) + ".ctor\tId: " + _rand); }