A lot of comments.
This commit is contained in:
		@@ -1,15 +1,19 @@
 | 
			
		||||
using System;
 | 
			
		||||
 | 
			
		||||
namespace Playground
 | 
			
		||||
namespace SmallInjectorDemo
 | 
			
		||||
{
 | 
			
		||||
    internal static class Program
 | 
			
		||||
    {
 | 
			
		||||
        private static void Main()
 | 
			
		||||
        {
 | 
			
		||||
            Console.WriteLine("Small dependency injection example.");
 | 
			
		||||
            Console.WriteLine();
 | 
			
		||||
            var injector = new SmallInjector();
 | 
			
		||||
            Console.WriteLine("Register " + nameof(ServiceOne));
 | 
			
		||||
            injector.RegisterType<ServiceOne, IServiceOne>(true);
 | 
			
		||||
            Console.WriteLine("Register " + nameof(ServiceTwo));
 | 
			
		||||
            injector.RegisterType<ServiceTwo, IServiceTwo>(true);
 | 
			
		||||
            Console.WriteLine("Register " + nameof(UsefulClass));
 | 
			
		||||
            injector.RegisterType<UsefulClass>(false);
 | 
			
		||||
 | 
			
		||||
            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;
 | 
			
		||||
 | 
			
		||||
namespace Playground
 | 
			
		||||
namespace SmallInjectorDemo
 | 
			
		||||
{
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Implementation of <see cref="IServiceOne"/>.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public class ServiceOne : IServiceOne
 | 
			
		||||
    {
 | 
			
		||||
        private const string MyName = nameof(ServiceOne);
 | 
			
		||||
        private readonly Guid _guid;
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Creates a new instance of <see cref="ServiceOne"/>.
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        public ServiceOne()
 | 
			
		||||
        {
 | 
			
		||||
            _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
 | 
			
		||||
    {
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,22 +1,33 @@
 | 
			
		||||
using System;
 | 
			
		||||
 | 
			
		||||
namespace Playground
 | 
			
		||||
namespace SmallInjectorDemo
 | 
			
		||||
{
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Implementation of <see cref="IServiceTwo"/>.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public class ServiceTwo : IServiceTwo
 | 
			
		||||
    {
 | 
			
		||||
        private const string MyName = nameof(ServiceTwo);
 | 
			
		||||
        private readonly Guid _guid;
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Creates a new instance of <see cref="ServiceTwo"/>.
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        public ServiceTwo()
 | 
			
		||||
        {
 | 
			
		||||
            _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
 | 
			
		||||
    {
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,24 +1,44 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Linq.Expressions;
 | 
			
		||||
using System.Reflection;
 | 
			
		||||
namespace Playground
 | 
			
		||||
 | 
			
		||||
namespace SmallInjectorDemo
 | 
			
		||||
{
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// A small dependency injector to demonstrate the pattern.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public class SmallInjector
 | 
			
		||||
    {
 | 
			
		||||
        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)
 | 
			
		||||
        {
 | 
			
		||||
            _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);
 | 
			
		||||
 | 
			
		||||
        /// <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));
 | 
			
		||||
 | 
			
		||||
        /// <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)
 | 
			
		||||
        {
 | 
			
		||||
            if (!_container.TryGetValue(serviceInterface, out var registeredType))
 | 
			
		||||
@@ -41,7 +61,19 @@ namespace Playground
 | 
			
		||||
                return registeredType.SingletonInstance ?? (registeredType.SingletonInstance = 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>
 | 
			
		||||
    <RootNamespace>SmallInjectorDemo</RootNamespace>
 | 
			
		||||
  </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>
 | 
			
		||||
@@ -1,28 +1,40 @@
 | 
			
		||||
using System;
 | 
			
		||||
 | 
			
		||||
namespace Playground
 | 
			
		||||
namespace SmallInjectorDemo
 | 
			
		||||
{
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// A very useful class.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public class UsefulClass
 | 
			
		||||
    {
 | 
			
		||||
        private readonly IServiceOne _service;
 | 
			
		||||
        private readonly IServiceOne _service1;
 | 
			
		||||
        private readonly IServiceTwo _service2;
 | 
			
		||||
        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;
 | 
			
		||||
            _guid = Guid.NewGuid();
 | 
			
		||||
            Console.WriteLine(nameof(UsefulClass) + ".ctor\tId: " + _guid);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Test the injected services.
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        public void TestTheServices()
 | 
			
		||||
        {
 | 
			
		||||
            Console.WriteLine(ToString());
 | 
			
		||||
            Console.WriteLine(_service.ToString());
 | 
			
		||||
            Console.WriteLine(_service1.ToString());
 | 
			
		||||
            Console.WriteLine(_service2.ToString());
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <inheritdoc />
 | 
			
		||||
        public override string ToString() => nameof(UsefulClass) + ".ToString()\tId: " + _guid;
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user