implement multiple expotd

This commit is contained in:
Holger Börchers 2019-07-29 00:21:09 +02:00
parent db8cb3e289
commit 56691b3145
14 changed files with 86 additions and 24 deletions

View File

@ -1,5 +1,7 @@
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Linq; using System.Linq;
namespace SmallInjector namespace SmallInjector
@ -15,7 +17,7 @@ namespace SmallInjector
public void RegisterType<TService, TInterface>(bool isSingleton, TService instance = default) public void RegisterType<TService, TInterface>(bool isSingleton, TService instance = default)
where TService : TInterface where TService : TInterface
{ {
if (!IsRegistered<TService>()) if (!IsRegistered<TInterface>())
{ {
_container[typeof(TInterface)] = new List<RegisteredType> _container[typeof(TInterface)] = new List<RegisteredType>
{new RegisteredType(typeof(TService), isSingleton, null)}; {new RegisteredType(typeof(TService), isSingleton, null)};
@ -43,7 +45,7 @@ namespace SmallInjector
public IEnumerable<object> ResolveAny(Type serviceType) public IEnumerable<object> ResolveAny(Type serviceType)
{ {
if (!_container.TryGetValue(serviceType, out var registeredTypes)) if (!_container.TryGetValue(serviceType, out var registeredTypes))
throw new Exception(@"Dependency {serviceType} is not registered"); throw new Exception($"Dependency {serviceType.FullName} is not registered");
foreach (var registeredType in registeredTypes) foreach (var registeredType in registeredTypes)
{ {
if (registeredType.IsSingleton && registeredType.Instance != null) if (registeredType.IsSingleton && registeredType.Instance != null)
@ -56,9 +58,17 @@ namespace SmallInjector
var parameters = constructor.GetParameters(); var parameters = constructor.GetParameters();
var constructorParameters = new object[parameters.Length]; var constructorParameters = new object[parameters.Length];
for (var i = 0; i < parameters.Length; i++) for (var i = 0; i < parameters.Length; i++)
{
var parameterType = parameters[i].ParameterType;
if (typeof(Array).IsAssignableFrom(parameterType))
{
constructorParameters[i] = ResolveAny(parameterType.GetElementType()).ToArray();
}
else
{ {
constructorParameters[i] = Resolve(parameters[i].ParameterType); constructorParameters[i] = Resolve(parameters[i].ParameterType);
} }
}
var instance = constructor.Invoke(constructorParameters); var instance = constructor.Invoke(constructorParameters);
yield return registeredType.IsSingleton ? (registeredType.Instance = instance) : instance; yield return registeredType.IsSingleton ? (registeredType.Instance = instance) : instance;
@ -83,6 +93,8 @@ namespace SmallInjector
IsSingleton = isSingleton; IsSingleton = isSingleton;
Instance = instance; Instance = instance;
} }
public override string ToString() => ServiceType.ToString();
} }
} }
} }

View File

@ -4,6 +4,7 @@ using System.IO;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
using SmallInjectorDemo.Interfaces;
namespace SmallInjectorDemo namespace SmallInjectorDemo
{ {

View File

@ -1,4 +1,4 @@
namespace SmallInjectorDemo namespace SmallInjectorDemo.Interfaces
{ {
/// <summary> /// <summary>
/// Interface of the clock. /// Interface of the clock.

View File

@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace SmallInjectorDemo.Interfaces
{
public interface IModule
{
}
}

View File

@ -0,0 +1,10 @@
namespace SmallInjectorDemo.Interfaces
{
/// <summary>
/// Interface for service one.
/// </summary>
public interface IServiceOne
{
}
}

View File

@ -0,0 +1,10 @@
namespace SmallInjectorDemo.Interfaces
{
/// <summary>
/// Interface for service two.
/// </summary>
public interface IServiceTwo
{
}
}

View File

@ -0,0 +1,9 @@
using SmallInjectorDemo.Interfaces;
namespace SmallInjectorDemo.Items
{
public class ModuleA : IModule
{
}
}

View File

@ -0,0 +1,9 @@
using SmallInjectorDemo.Interfaces;
namespace SmallInjectorDemo.Items
{
public class ModuleB : IModule
{
}
}

View File

@ -0,0 +1,9 @@
using SmallInjectorDemo.Interfaces;
namespace SmallInjectorDemo.Items
{
public class ModuleC : IModule
{
}
}

View File

@ -1,6 +1,8 @@
using System; using System;
using CommonServiceLocator; using CommonServiceLocator;
using SmallInjector; using SmallInjector;
using SmallInjectorDemo.Interfaces;
using SmallInjectorDemo.Items;
namespace SmallInjectorDemo namespace SmallInjectorDemo
{ {
@ -22,6 +24,10 @@ namespace SmallInjectorDemo
container.RegisterType<ServiceTwo, IServiceTwo>(true); container.RegisterType<ServiceTwo, IServiceTwo>(true);
Console.WriteLine("Register " + nameof(ServiceConsumer)); Console.WriteLine("Register " + nameof(ServiceConsumer));
container.RegisterType<ServiceConsumer>(false); container.RegisterType<ServiceConsumer>(false);
container.RegisterType<ModuleA, IModule>(true);
container.RegisterType<ModuleB, IModule>(true);
container.RegisterType<ModuleC, IModule>(true);
Console.WriteLine(); Console.WriteLine();
Console.WriteLine("Check registrations:"); Console.WriteLine("Check registrations:");
@ -32,9 +38,6 @@ namespace SmallInjectorDemo
Console.WriteLine(); Console.WriteLine();
Console.WriteLine("Resolve class instances:"); Console.WriteLine("Resolve class instances:");
//var useful1 = container.Resolve<ServiceConsumer>();
//var useful2 = container.Resolve<ServiceConsumer>();
//var useful3 = container.Resolve<ServiceConsumer>();
var useful1 = ServiceLocator.Current.GetInstance<ServiceConsumer>(); var useful1 = ServiceLocator.Current.GetInstance<ServiceConsumer>();
var useful2 = ServiceLocator.Current.GetInstance<ServiceConsumer>(); var useful2 = ServiceLocator.Current.GetInstance<ServiceConsumer>();
var useful3 = ServiceLocator.Current.GetInstance<ServiceConsumer>(); var useful3 = ServiceLocator.Current.GetInstance<ServiceConsumer>();

View File

@ -1,4 +1,5 @@
using System; using System;
using SmallInjectorDemo.Interfaces;
namespace SmallInjectorDemo namespace SmallInjectorDemo
{ {

View File

@ -1,4 +1,6 @@
using System; using System;
using System.Collections.Generic;
using SmallInjectorDemo.Interfaces;
namespace SmallInjectorDemo namespace SmallInjectorDemo
{ {
@ -14,7 +16,7 @@ namespace SmallInjectorDemo
/// <summary> /// <summary>
/// Creates a new instance of <see cref="ServiceOne"/>. /// Creates a new instance of <see cref="ServiceOne"/>.
/// </summary> /// </summary>
public ServiceOne(IClock clock) public ServiceOne(IClock clock, IModule[] modules)
{ {
_clock = clock; _clock = clock;
_id = Guid.NewGuid().EncodeBase64String(); _id = Guid.NewGuid().EncodeBase64String();
@ -24,12 +26,4 @@ namespace SmallInjectorDemo
/// <inheritdoc /> /// <inheritdoc />
public override string ToString() => Helper.WriteMethodString(_clock, _id); public override string ToString() => Helper.WriteMethodString(_clock, _id);
} }
/// <summary>
/// Interface for service one.
/// </summary>
public interface IServiceOne
{
}
} }

View File

@ -1,4 +1,5 @@
using System; using System;
using SmallInjectorDemo.Interfaces;
namespace SmallInjectorDemo namespace SmallInjectorDemo
{ {
@ -24,12 +25,4 @@ namespace SmallInjectorDemo
/// <inheritdoc /> /// <inheritdoc />
public override string ToString() => Helper.WriteMethodString(_clock, _id); public override string ToString() => Helper.WriteMethodString(_clock, _id);
} }
/// <summary>
/// Interface for service two.
/// </summary>
public interface IServiceTwo
{
}
} }

View File

@ -1,4 +1,5 @@
using System; using System;
using SmallInjectorDemo.Interfaces;
namespace SmallInjectorDemo namespace SmallInjectorDemo
{ {