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",
"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

2
.vscode/tasks.json vendored
View File

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

View File

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

View File

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

View File

@ -9,7 +9,7 @@ namespace SmallInjectorDemo
{
private readonly IServiceOne _service1;
private readonly IServiceTwo _service2;
private readonly Guid _guid;
private readonly int _rand;
/// <summary>
/// Creates a new instance of <see cref="UsefulClass"/>.
@ -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);
}
/// <summary>
@ -35,7 +35,7 @@ namespace SmallInjectorDemo
}
/// <inheritdoc />
public override string ToString() => nameof(UsefulClass) + ".ToString()\tId: " + _guid;
public override string ToString() => nameof(UsefulClass) + ".ToString()\tId: " + _rand;
}
}