This commit is contained in:
Holger Boerchers 2018-08-15 08:46:46 +02:00
parent e7ec2fc805
commit 1d2fca6ddc
5 changed files with 118 additions and 22 deletions

55
Helper.cs Normal file
View File

@ -0,0 +1,55 @@
using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
namespace SmallInjectorDemo
{
/// <summary>
/// Static helper class for generating random numbers.
/// </summary>
public static class Helper
{
/// <summary>
/// Return cryptographic stable random integer.
/// </summary>
/// <param name="min">Lower border of result.</param>
/// <param name="max">Upper border of result.</param>
/// <returns>Random integer.</returns>
public static int NewRandomInteger(int min, int max)
{
// The random number provider.
using (var rand = new RNGCryptoServiceProvider())
{
var 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));
}
}
/// <summary>
/// Write method string.
/// </summary>
/// <param name="id">id of the object</param>
/// <param name="memberName">member name</param>
/// <param name="filepath">file path</param>
/// <returns></returns>
public static string WriteMethodString(int id, [CallerMemberName] string memberName = null, [CallerFilePath] string filepath = null)
{
var classname = Path.GetFileNameWithoutExtension(filepath);
return $"{classname}.{memberName?.TrimStart('.')}".PadRight(30) + $"Id: {id}";
}
}
}

View File

@ -6,28 +6,28 @@ namespace SmallInjectorDemo
{ {
private static void Main() private static void Main()
{ {
Console.WriteLine("Small dependency injection example."); Console.WriteLine("Small dependency injection example");
Console.WriteLine();
var injector = new SmallInjector(); var injector = new SmallInjector();
Console.WriteLine("Register " + nameof(ServiceOne)); Console.WriteLine("Register " + nameof(ServiceOne));
injector.RegisterType<ServiceOne, IServiceOne>(true); injector.RegisterType<ServiceOne, IServiceOne>(true);
Console.WriteLine("Register " + nameof(ServiceTwo)); Console.WriteLine("Register " + nameof(ServiceTwo));
injector.RegisterType<ServiceTwo, IServiceTwo>(true); injector.RegisterType<ServiceTwo, IServiceTwo>(true);
Console.WriteLine("Register " + nameof(UsefulClass)); Console.WriteLine("Register " + nameof(ServiceConsumer));
injector.RegisterType<UsefulClass>(false); injector.RegisterType<ServiceConsumer>(false);
Console.WriteLine(); Console.WriteLine();
Console.WriteLine("Resolve class instances."); Console.WriteLine("Resolve class instances:");
var useful1 = injector.Resolve<UsefulClass>(); var useful1 = injector.Resolve<ServiceConsumer>();
var useful2 = injector.Resolve<UsefulClass>(); var useful2 = injector.Resolve<ServiceConsumer>();
var useful3 = injector.Resolve<UsefulClass>(); var useful3 = injector.Resolve<ServiceConsumer>();
Console.WriteLine("Run test methods."); Console.WriteLine();
Console.WriteLine("Run test methods:");
useful1.TestTheServices(); useful1.TestTheServices();
Console.WriteLine(); Console.WriteLine();
useful2.TestTheServices(); useful2.TestTheServices();
Console.WriteLine(); Console.WriteLine();
useful3.TestTheServices(); useful3.TestTheServices();
//Console.ReadLine(); Console.ReadLine();
} }
} }
} }

41
ServiceConsumer.cs Normal file
View File

@ -0,0 +1,41 @@
using System;
namespace SmallInjectorDemo
{
/// <summary>
/// A very useful class.
/// </summary>
public class ServiceConsumer
{
private readonly IServiceOne _service1;
private readonly IServiceTwo _service2;
private readonly int _id;
/// <summary>
/// Creates a new instance of <see cref="ServiceConsumer"/>.
/// </summary>
/// <param name="service1">injected service one.</param>
/// <param name="service2">injected service two.</param>
public ServiceConsumer(IServiceOne service1, IServiceTwo service2)
{
_service1 = service1;
_service2 = service2;
_id = Helper.NewRandomInteger(10, 99);
Console.WriteLine(Helper.WriteMethodString(_id));
}
/// <summary>
/// Test the injected services.
/// </summary>
public void TestTheServices()
{
Console.WriteLine(ToString());
Console.WriteLine(_service1.ToString());
Console.WriteLine(_service2.ToString());
}
/// <inheritdoc />
public override string ToString() => Helper.WriteMethodString(_id);
}
}

View File

@ -2,25 +2,25 @@ using System;
namespace SmallInjectorDemo namespace SmallInjectorDemo
{ {
/// <inheritdoc />
/// <summary> /// <summary>
/// Implementation of <see cref="IServiceOne"/>. /// Implementation of <see cref="T:SmallInjectorDemo.IServiceOne" />.
/// </summary> /// </summary>
public class ServiceOne : IServiceOne public class ServiceOne : IServiceOne
{ {
private const string MyName = nameof(ServiceOne); private readonly int _id;
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()
{ {
_rand = RandomHelper.NewRandomInteger(10, 99); _id = Helper.NewRandomInteger(10, 99);
Console.WriteLine(MyName + ".ctor\t\tId: " + _rand); Console.WriteLine(Helper.WriteMethodString(_id));
} }
/// <inheritdoc /> /// <inheritdoc />
public override string ToString() => MyName + ".ToString()\tId: " + _rand; public override string ToString() => Helper.WriteMethodString(_id);
} }
/// <summary> /// <summary>

View File

@ -2,25 +2,25 @@ using System;
namespace SmallInjectorDemo namespace SmallInjectorDemo
{ {
/// <inheritdoc />
/// <summary> /// <summary>
/// Implementation of <see cref="IServiceTwo"/>. /// Implementation of <see cref="T:SmallInjectorDemo.IServiceTwo" />.
/// </summary> /// </summary>
public class ServiceTwo : IServiceTwo public class ServiceTwo : IServiceTwo
{ {
private const string MyName = nameof(ServiceTwo); private readonly int _id;
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()
{ {
_rand = RandomHelper.NewRandomInteger(10, 99); _id = Helper.NewRandomInteger(10, 99);
Console.WriteLine(MyName + ".ctor\t\tId: " + _rand); Console.WriteLine(Helper.WriteMethodString(_id));
} }
/// <inheritdoc /> /// <inheritdoc />
public override string ToString() => MyName + ".ToString()\tId: " + _rand; public override string ToString() => Helper.WriteMethodString(_id);
} }
/// <summary> /// <summary>