From ae35676873f44331538732f8a7b86859ca43009d Mon Sep 17 00:00:00 2001 From: Holger Boerchers Date: Sun, 12 Aug 2018 20:11:01 +0200 Subject: [PATCH] Changed implementation of random identifier. --- .vscode/launch.json | 2 +- .vscode/tasks.json | 2 +- Program.cs | 2 +- RandomHelper.cs | 33 +++++++++++++++++++++++++++++++++ ServiceOne.cs | 8 ++++---- ServiceTwo.cs | 8 ++++---- UsefulClass.cs | 8 ++++---- 7 files changed, 48 insertions(+), 15 deletions(-) create mode 100644 RandomHelper.cs diff --git a/.vscode/launch.json b/.vscode/launch.json index 9724023..cc84667 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,7 @@ "request": "launch", "preLaunchTask": "build", // If you have changed target frameworks, make sure to update the program path. - "program": "${workspaceFolder}/bin/Debug/netcoreapp2.1/Playground.dll", + "program": "${workspaceFolder}/bin/Debug/netcoreapp2.1/SmallInjectorDemo.dll", "args": [], "cwd": "${workspaceFolder}", // For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 2644cb2..d304b79 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -7,7 +7,7 @@ "type": "process", "args": [ "build", - "${workspaceFolder}/Playground.csproj" + "${workspaceFolder}/SmallInjectorDemo.csproj" ], "problemMatcher": "$msCompile" } diff --git a/Program.cs b/Program.cs index 1e1c944..65131bd 100644 --- a/Program.cs +++ b/Program.cs @@ -26,7 +26,7 @@ namespace SmallInjectorDemo useful1.TestTheServices(); useful2.TestTheServices(); useful3.TestTheServices(); - Console.ReadLine(); + //Console.ReadLine(); } } } \ No newline at end of file diff --git a/RandomHelper.cs b/RandomHelper.cs new file mode 100644 index 0000000..25fa7a8 --- /dev/null +++ b/RandomHelper.cs @@ -0,0 +1,33 @@ +using System; +using System.Security.Cryptography; + +namespace SmallInjectorDemo +{ + public static class RandomHelper + { + + 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/ServiceOne.cs b/ServiceOne.cs index 4a4637d..8ea7994 100644 --- a/ServiceOne.cs +++ b/ServiceOne.cs @@ -8,19 +8,19 @@ namespace SmallInjectorDemo public class ServiceOne : IServiceOne { private const string MyName = nameof(ServiceOne); - private readonly Guid _guid; + private readonly int _rand; /// /// Creates a new instance of . /// public ServiceOne() { - _guid = Guid.NewGuid(); - Console.WriteLine(MyName + ".ctor\t\tId: " + _guid); + _rand = RandomHelper.NewRandomInteger(10, 99); + Console.WriteLine(MyName + ".ctor\t\tId: " + _rand); } /// - public override string ToString() => MyName + ".ToString()\tId: " + _guid; + public override string ToString() => MyName + ".ToString()\tId: " + _rand; } /// diff --git a/ServiceTwo.cs b/ServiceTwo.cs index 52e1d2b..f2044e8 100644 --- a/ServiceTwo.cs +++ b/ServiceTwo.cs @@ -8,19 +8,19 @@ namespace SmallInjectorDemo public class ServiceTwo : IServiceTwo { private const string MyName = nameof(ServiceTwo); - private readonly Guid _guid; + private readonly int _rand; /// /// Creates a new instance of . /// public ServiceTwo() { - _guid = Guid.NewGuid(); - Console.WriteLine(MyName + ".ctor\t\tId: " + _guid); + _rand = RandomHelper.NewRandomInteger(10, 99); + Console.WriteLine(MyName + ".ctor\t\tId: " + _rand); } /// - public override string ToString() => MyName + ".ToString()\tId: " + _guid; + public override string ToString() => MyName + ".ToString()\tId: " + _rand; } /// diff --git a/UsefulClass.cs b/UsefulClass.cs index f11cc3b..23ce77b 100644 --- a/UsefulClass.cs +++ b/UsefulClass.cs @@ -9,7 +9,7 @@ namespace SmallInjectorDemo { private readonly IServiceOne _service1; private readonly IServiceTwo _service2; - private readonly Guid _guid; + private readonly int _rand; /// /// Creates a new instance of . @@ -20,8 +20,8 @@ namespace SmallInjectorDemo { _service1 = service1; _service2 = service2; - _guid = Guid.NewGuid(); - Console.WriteLine(nameof(UsefulClass) + ".ctor\tId: " + _guid); + _rand = RandomHelper.NewRandomInteger(10, 99); + Console.WriteLine(nameof(UsefulClass) + ".ctor\tId: " + _rand); } /// @@ -35,7 +35,7 @@ namespace SmallInjectorDemo } /// - public override string ToString() => nameof(UsefulClass) + ".ToString()\tId: " + _guid; + public override string ToString() => nameof(UsefulClass) + ".ToString()\tId: " + _rand; } } \ No newline at end of file