A lot of comments.
This commit is contained in:
parent
7326ff6684
commit
c381375dbd
@ -1,15 +1,19 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace Playground
|
namespace SmallInjectorDemo
|
||||||
{
|
{
|
||||||
internal static class Program
|
internal static class Program
|
||||||
{
|
{
|
||||||
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));
|
||||||
injector.RegisterType<ServiceOne, IServiceOne>(true);
|
injector.RegisterType<ServiceOne, IServiceOne>(true);
|
||||||
|
Console.WriteLine("Register " + nameof(ServiceTwo));
|
||||||
injector.RegisterType<ServiceTwo, IServiceTwo>(true);
|
injector.RegisterType<ServiceTwo, IServiceTwo>(true);
|
||||||
|
Console.WriteLine("Register " + nameof(UsefulClass));
|
||||||
injector.RegisterType<UsefulClass>(false);
|
injector.RegisterType<UsefulClass>(false);
|
||||||
|
|
||||||
Console.WriteLine();
|
Console.WriteLine();
|
||||||
|
@ -1,17 +0,0 @@
|
|||||||
using System;
|
|
||||||
|
|
||||||
namespace Playground
|
|
||||||
{
|
|
||||||
public class RegisteredType
|
|
||||||
{
|
|
||||||
public RegisteredType(Type concreteType, bool isSingleton)
|
|
||||||
{
|
|
||||||
ConcreteType = concreteType;
|
|
||||||
IsSingleton = isSingleton;
|
|
||||||
}
|
|
||||||
|
|
||||||
public readonly bool IsSingleton;
|
|
||||||
public readonly Type ConcreteType;
|
|
||||||
public object SingletonInstance { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,20 +1,31 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace Playground
|
namespace SmallInjectorDemo
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Implementation of <see cref="IServiceOne"/>.
|
||||||
|
/// </summary>
|
||||||
public class ServiceOne : IServiceOne
|
public class ServiceOne : IServiceOne
|
||||||
{
|
{
|
||||||
|
private const string MyName = nameof(ServiceOne);
|
||||||
private readonly Guid _guid;
|
private readonly Guid _guid;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new instance of <see cref="ServiceOne"/>.
|
||||||
|
/// </summary>
|
||||||
public ServiceOne()
|
public ServiceOne()
|
||||||
{
|
{
|
||||||
_guid = Guid.NewGuid();
|
_guid = Guid.NewGuid();
|
||||||
Console.WriteLine(nameof(ServiceOne) + ".ctor\t\tId: " + _guid);
|
Console.WriteLine(MyName + ".ctor\t\tId: " + _guid);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string ToString() => nameof(ServiceOne) + ".ToString()\tId: " + _guid;
|
/// <inheritdoc />
|
||||||
|
public override string ToString() => MyName + ".ToString()\tId: " + _guid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Interface for service one.
|
||||||
|
/// </summary>
|
||||||
public interface IServiceOne
|
public interface IServiceOne
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -1,22 +1,33 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace Playground
|
namespace SmallInjectorDemo
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Implementation of <see cref="IServiceTwo"/>.
|
||||||
|
/// </summary>
|
||||||
public class ServiceTwo : IServiceTwo
|
public class ServiceTwo : IServiceTwo
|
||||||
{
|
{
|
||||||
|
private const string MyName = nameof(ServiceTwo);
|
||||||
private readonly Guid _guid;
|
private readonly Guid _guid;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new instance of <see cref="ServiceTwo"/>.
|
||||||
|
/// </summary>
|
||||||
public ServiceTwo()
|
public ServiceTwo()
|
||||||
{
|
{
|
||||||
_guid = Guid.NewGuid();
|
_guid = Guid.NewGuid();
|
||||||
Console.WriteLine(nameof(ServiceTwo) + ".ctor\t\tId: " + _guid);
|
Console.WriteLine(MyName + ".ctor\t\tId: " + _guid);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string ToString() => nameof(ServiceTwo) + ".ToString()\tId: " + _guid;
|
/// <inheritdoc />
|
||||||
|
public override string ToString() => MyName + ".ToString()\tId: " + _guid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Interface for service two.
|
||||||
|
/// </summary>
|
||||||
public interface IServiceTwo
|
public interface IServiceTwo
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,24 +1,44 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
|
||||||
using System.Linq.Expressions;
|
namespace SmallInjectorDemo
|
||||||
using System.Reflection;
|
|
||||||
namespace Playground
|
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// A small dependency injector to demonstrate the pattern.
|
||||||
|
/// </summary>
|
||||||
public class SmallInjector
|
public class SmallInjector
|
||||||
{
|
{
|
||||||
private readonly Dictionary<Type, RegisteredType> _container = new Dictionary<Type, RegisteredType>();
|
private readonly Dictionary<Type, RegisteredType> _container = new Dictionary<Type, RegisteredType>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Register types in the resolve container.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TService">Type of the service.</typeparam>
|
||||||
|
/// <typeparam name="TInterface">Type of the interface of the service.</typeparam>
|
||||||
|
/// <param name="isSingleton">True if the service should be singleton. False otherwise.</param>
|
||||||
public void RegisterType<TService, TInterface>(bool isSingleton)
|
public void RegisterType<TService, TInterface>(bool isSingleton)
|
||||||
{
|
{
|
||||||
_container.Add(typeof(TInterface), new RegisteredType(typeof(TService), isSingleton));
|
_container.Add(typeof(TInterface), new RegisteredType(typeof(TService), isSingleton));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Register types in the resolve container.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TService">Type of the service.</typeparam>
|
||||||
public void RegisterType<TService>(bool isSingleton) => RegisterType<TService, TService>(isSingleton);
|
public void RegisterType<TService>(bool isSingleton) => RegisterType<TService, TService>(isSingleton);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Resolve service of specified type.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TService">Type of the service.</typeparam>
|
||||||
|
/// <returns>A instance of the service.</returns>
|
||||||
public TService Resolve<TService>() => (TService)Resolve(typeof(TService));
|
public TService Resolve<TService>() => (TService)Resolve(typeof(TService));
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Resolve service of specified type.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="serviceInterface">Type of the service.</param>
|
||||||
|
/// <returns>A instance of the service.</returns>
|
||||||
public object Resolve(Type serviceInterface)
|
public object Resolve(Type serviceInterface)
|
||||||
{
|
{
|
||||||
if (!_container.TryGetValue(serviceInterface, out var registeredType))
|
if (!_container.TryGetValue(serviceInterface, out var registeredType))
|
||||||
@ -41,7 +61,19 @@ namespace Playground
|
|||||||
return registeredType.SingletonInstance ?? (registeredType.SingletonInstance = constructor.Invoke(constructorParameters));
|
return registeredType.SingletonInstance ?? (registeredType.SingletonInstance = constructor.Invoke(constructorParameters));
|
||||||
}
|
}
|
||||||
return constructor.Invoke(constructorParameters);
|
return constructor.Invoke(constructorParameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
private class RegisteredType
|
||||||
|
{
|
||||||
|
public RegisteredType(Type concreteType, bool isSingleton)
|
||||||
|
{
|
||||||
|
ConcreteType = concreteType;
|
||||||
|
IsSingleton = isSingleton;
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly bool IsSingleton;
|
||||||
|
public readonly Type ConcreteType;
|
||||||
|
public object SingletonInstance { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -5,4 +5,10 @@
|
|||||||
<AssemblyName>SmallInjectorDemo</AssemblyName>
|
<AssemblyName>SmallInjectorDemo</AssemblyName>
|
||||||
<RootNamespace>SmallInjectorDemo</RootNamespace>
|
<RootNamespace>SmallInjectorDemo</RootNamespace>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||||
|
<DocumentationFile>bin\Debug\netcoreapp2.1\SmallInjectorDemo.xml</DocumentationFile>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
|
||||||
|
<DocumentationFile>bin\Release\netcoreapp2.1\SmallInjectorDemo.xml</DocumentationFile>
|
||||||
|
</PropertyGroup>
|
||||||
</Project>
|
</Project>
|
@ -1,28 +1,40 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace Playground
|
namespace SmallInjectorDemo
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// A very useful class.
|
||||||
|
/// </summary>
|
||||||
public class UsefulClass
|
public class UsefulClass
|
||||||
{
|
{
|
||||||
private readonly IServiceOne _service;
|
private readonly IServiceOne _service1;
|
||||||
private readonly IServiceTwo _service2;
|
private readonly IServiceTwo _service2;
|
||||||
private readonly Guid _guid;
|
private readonly Guid _guid;
|
||||||
|
|
||||||
public UsefulClass(IServiceOne service, IServiceTwo service2)
|
/// <summary>
|
||||||
|
/// Creates a new instance of <see cref="UsefulClass"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="service1">injected service one.</param>
|
||||||
|
/// <param name="service2">injected service two.</param>
|
||||||
|
public UsefulClass(IServiceOne service1, IServiceTwo service2)
|
||||||
{
|
{
|
||||||
_service = service;
|
_service1 = service1;
|
||||||
_service2 = service2;
|
_service2 = service2;
|
||||||
_guid = Guid.NewGuid();
|
_guid = Guid.NewGuid();
|
||||||
Console.WriteLine(nameof(UsefulClass) + ".ctor\tId: " + _guid);
|
Console.WriteLine(nameof(UsefulClass) + ".ctor\tId: " + _guid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Test the injected services.
|
||||||
|
/// </summary>
|
||||||
public void TestTheServices()
|
public void TestTheServices()
|
||||||
{
|
{
|
||||||
Console.WriteLine(ToString());
|
Console.WriteLine(ToString());
|
||||||
Console.WriteLine(_service.ToString());
|
Console.WriteLine(_service1.ToString());
|
||||||
Console.WriteLine(_service2.ToString());
|
Console.WriteLine(_service2.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
public override string ToString() => nameof(UsefulClass) + ".ToString()\tId: " + _guid;
|
public override string ToString() => nameof(UsefulClass) + ".ToString()\tId: " + _guid;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user