Changed implementation of random identifier.

This commit is contained in:
Holger Boerchers 2018-08-12 20:11:01 +02:00
parent 9c5a1e7ab8
commit ae35676873
7 changed files with 48 additions and 15 deletions

2
.vscode/launch.json vendored
View File

@ -10,7 +10,7 @@
"request": "launch", "request": "launch",
"preLaunchTask": "build", "preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path. // 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": [], "args": [],
"cwd": "${workspaceFolder}", "cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window // For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window

2
.vscode/tasks.json vendored
View File

@ -7,7 +7,7 @@
"type": "process", "type": "process",
"args": [ "args": [
"build", "build",
"${workspaceFolder}/Playground.csproj" "${workspaceFolder}/SmallInjectorDemo.csproj"
], ],
"problemMatcher": "$msCompile" "problemMatcher": "$msCompile"
} }

View File

@ -26,7 +26,7 @@ namespace SmallInjectorDemo
useful1.TestTheServices(); useful1.TestTheServices();
useful2.TestTheServices(); useful2.TestTheServices();
useful3.TestTheServices(); useful3.TestTheServices();
Console.ReadLine(); //Console.ReadLine();
} }
} }
} }

33
RandomHelper.cs Normal file
View File

@ -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));
}
}
}
}

View File

@ -8,19 +8,19 @@ namespace SmallInjectorDemo
public class ServiceOne : IServiceOne public class ServiceOne : IServiceOne
{ {
private const string MyName = nameof(ServiceOne); private const string MyName = nameof(ServiceOne);
private readonly Guid _guid; private readonly int _rand;
/// <summary> /// <summary>
/// Creates a new instance of <see cref="ServiceOne"/>. /// Creates a new instance of <see cref="ServiceOne"/>.
/// </summary> /// </summary>
public ServiceOne() public ServiceOne()
{ {
_guid = Guid.NewGuid(); _rand = RandomHelper.NewRandomInteger(10, 99);
Console.WriteLine(MyName + ".ctor\t\tId: " + _guid); Console.WriteLine(MyName + ".ctor\t\tId: " + _rand);
} }
/// <inheritdoc /> /// <inheritdoc />
public override string ToString() => MyName + ".ToString()\tId: " + _guid; public override string ToString() => MyName + ".ToString()\tId: " + _rand;
} }
/// <summary> /// <summary>

View File

@ -8,19 +8,19 @@ namespace SmallInjectorDemo
public class ServiceTwo : IServiceTwo public class ServiceTwo : IServiceTwo
{ {
private const string MyName = nameof(ServiceTwo); private const string MyName = nameof(ServiceTwo);
private readonly Guid _guid; private readonly int _rand;
/// <summary> /// <summary>
/// Creates a new instance of <see cref="ServiceTwo"/>. /// Creates a new instance of <see cref="ServiceTwo"/>.
/// </summary> /// </summary>
public ServiceTwo() public ServiceTwo()
{ {
_guid = Guid.NewGuid(); _rand = RandomHelper.NewRandomInteger(10, 99);
Console.WriteLine(MyName + ".ctor\t\tId: " + _guid); Console.WriteLine(MyName + ".ctor\t\tId: " + _rand);
} }
/// <inheritdoc /> /// <inheritdoc />
public override string ToString() => MyName + ".ToString()\tId: " + _guid; public override string ToString() => MyName + ".ToString()\tId: " + _rand;
} }
/// <summary> /// <summary>

View File

@ -9,7 +9,7 @@ namespace SmallInjectorDemo
{ {
private readonly IServiceOne _service1; private readonly IServiceOne _service1;
private readonly IServiceTwo _service2; private readonly IServiceTwo _service2;
private readonly Guid _guid; private readonly int _rand;
/// <summary> /// <summary>
/// Creates a new instance of <see cref="UsefulClass"/>. /// Creates a new instance of <see cref="UsefulClass"/>.
@ -20,8 +20,8 @@ namespace SmallInjectorDemo
{ {
_service1 = service1; _service1 = service1;
_service2 = service2; _service2 = service2;
_guid = Guid.NewGuid(); _rand = RandomHelper.NewRandomInteger(10, 99);
Console.WriteLine(nameof(UsefulClass) + ".ctor\tId: " + _guid); Console.WriteLine(nameof(UsefulClass) + ".ctor\tId: " + _rand);
} }
/// <summary> /// <summary>
@ -35,7 +35,7 @@ namespace SmallInjectorDemo
} }
/// <inheritdoc /> /// <inheritdoc />
public override string ToString() => nameof(UsefulClass) + ".ToString()\tId: " + _guid; public override string ToString() => nameof(UsefulClass) + ".ToString()\tId: " + _rand;
} }
} }