diff --git a/SmallInjector/Container.cs b/SmallInjector/Container.cs index 0fdc9b1..7f5205a 100644 --- a/SmallInjector/Container.cs +++ b/SmallInjector/Container.cs @@ -1,5 +1,7 @@ using System; +using System.Collections; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; namespace SmallInjector @@ -15,7 +17,7 @@ namespace SmallInjector public void RegisterType(bool isSingleton, TService instance = default) where TService : TInterface { - if (!IsRegistered()) + if (!IsRegistered()) { _container[typeof(TInterface)] = new List {new RegisteredType(typeof(TService), isSingleton, null)}; @@ -43,7 +45,7 @@ namespace SmallInjector public IEnumerable ResolveAny(Type serviceType) { 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) { if (registeredType.IsSingleton && registeredType.Instance != null) @@ -57,7 +59,15 @@ namespace SmallInjector var constructorParameters = new object[parameters.Length]; for (var i = 0; i < parameters.Length; i++) { - constructorParameters[i] = Resolve(parameters[i].ParameterType); + var parameterType = parameters[i].ParameterType; + if (typeof(Array).IsAssignableFrom(parameterType)) + { + constructorParameters[i] = ResolveAny(parameterType.GetElementType()).ToArray(); + } + else + { + constructorParameters[i] = Resolve(parameters[i].ParameterType); + } } var instance = constructor.Invoke(constructorParameters); @@ -83,6 +93,8 @@ namespace SmallInjector IsSingleton = isSingleton; Instance = instance; } + + public override string ToString() => ServiceType.ToString(); } } } \ No newline at end of file diff --git a/SmallInjectorDemo/Helper.cs b/SmallInjectorDemo/Helper.cs index 64ecbf1..7f98043 100644 --- a/SmallInjectorDemo/Helper.cs +++ b/SmallInjectorDemo/Helper.cs @@ -4,6 +4,7 @@ using System.IO; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; +using SmallInjectorDemo.Interfaces; namespace SmallInjectorDemo { diff --git a/SmallInjectorDemo/IClock.cs b/SmallInjectorDemo/Interfaces/IClock.cs similarity index 85% rename from SmallInjectorDemo/IClock.cs rename to SmallInjectorDemo/Interfaces/IClock.cs index d2da1da..535fbc2 100644 --- a/SmallInjectorDemo/IClock.cs +++ b/SmallInjectorDemo/Interfaces/IClock.cs @@ -1,4 +1,4 @@ -namespace SmallInjectorDemo +namespace SmallInjectorDemo.Interfaces { /// /// Interface of the clock. diff --git a/SmallInjectorDemo/Interfaces/IModule.cs b/SmallInjectorDemo/Interfaces/IModule.cs new file mode 100644 index 0000000..f18b0e1 --- /dev/null +++ b/SmallInjectorDemo/Interfaces/IModule.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace SmallInjectorDemo.Interfaces +{ + public interface IModule + { + } +} diff --git a/SmallInjectorDemo/Interfaces/IServiceOne.cs b/SmallInjectorDemo/Interfaces/IServiceOne.cs new file mode 100644 index 0000000..eb1e4b8 --- /dev/null +++ b/SmallInjectorDemo/Interfaces/IServiceOne.cs @@ -0,0 +1,10 @@ +namespace SmallInjectorDemo.Interfaces +{ + /// + /// Interface for service one. + /// + public interface IServiceOne + { + + } +} \ No newline at end of file diff --git a/SmallInjectorDemo/Interfaces/IServiceTwo.cs b/SmallInjectorDemo/Interfaces/IServiceTwo.cs new file mode 100644 index 0000000..d333dea --- /dev/null +++ b/SmallInjectorDemo/Interfaces/IServiceTwo.cs @@ -0,0 +1,10 @@ +namespace SmallInjectorDemo.Interfaces +{ + /// + /// Interface for service two. + /// + public interface IServiceTwo + { + + } +} \ No newline at end of file diff --git a/SmallInjectorDemo/Items/ModuleA.cs b/SmallInjectorDemo/Items/ModuleA.cs new file mode 100644 index 0000000..0b7b8c4 --- /dev/null +++ b/SmallInjectorDemo/Items/ModuleA.cs @@ -0,0 +1,9 @@ +using SmallInjectorDemo.Interfaces; + +namespace SmallInjectorDemo.Items +{ + public class ModuleA : IModule + { + + } +} \ No newline at end of file diff --git a/SmallInjectorDemo/Items/ModuleB.cs b/SmallInjectorDemo/Items/ModuleB.cs new file mode 100644 index 0000000..5935386 --- /dev/null +++ b/SmallInjectorDemo/Items/ModuleB.cs @@ -0,0 +1,9 @@ +using SmallInjectorDemo.Interfaces; + +namespace SmallInjectorDemo.Items +{ + public class ModuleB : IModule + { + + } +} \ No newline at end of file diff --git a/SmallInjectorDemo/Items/ModuleC.cs b/SmallInjectorDemo/Items/ModuleC.cs new file mode 100644 index 0000000..0f7e1d4 --- /dev/null +++ b/SmallInjectorDemo/Items/ModuleC.cs @@ -0,0 +1,9 @@ +using SmallInjectorDemo.Interfaces; + +namespace SmallInjectorDemo.Items +{ + public class ModuleC : IModule + { + + } +} \ No newline at end of file diff --git a/SmallInjectorDemo/Program.cs b/SmallInjectorDemo/Program.cs index 1df59f7..2fe6ea4 100644 --- a/SmallInjectorDemo/Program.cs +++ b/SmallInjectorDemo/Program.cs @@ -1,6 +1,8 @@ using System; using CommonServiceLocator; using SmallInjector; +using SmallInjectorDemo.Interfaces; +using SmallInjectorDemo.Items; namespace SmallInjectorDemo { @@ -22,6 +24,10 @@ namespace SmallInjectorDemo container.RegisterType(true); Console.WriteLine("Register " + nameof(ServiceConsumer)); container.RegisterType(false); + container.RegisterType(true); + container.RegisterType(true); + container.RegisterType(true); + Console.WriteLine(); Console.WriteLine("Check registrations:"); @@ -32,9 +38,6 @@ namespace SmallInjectorDemo Console.WriteLine(); Console.WriteLine("Resolve class instances:"); - //var useful1 = container.Resolve(); - //var useful2 = container.Resolve(); - //var useful3 = container.Resolve(); var useful1 = ServiceLocator.Current.GetInstance(); var useful2 = ServiceLocator.Current.GetInstance(); var useful3 = ServiceLocator.Current.GetInstance(); diff --git a/SmallInjectorDemo/ServiceConsumer.cs b/SmallInjectorDemo/ServiceConsumer.cs index 35cfdb1..390a089 100644 --- a/SmallInjectorDemo/ServiceConsumer.cs +++ b/SmallInjectorDemo/ServiceConsumer.cs @@ -1,4 +1,5 @@ using System; +using SmallInjectorDemo.Interfaces; namespace SmallInjectorDemo { diff --git a/SmallInjectorDemo/ServiceOne.cs b/SmallInjectorDemo/ServiceOne.cs index 15b42de..df4f53a 100644 --- a/SmallInjectorDemo/ServiceOne.cs +++ b/SmallInjectorDemo/ServiceOne.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using SmallInjectorDemo.Interfaces; namespace SmallInjectorDemo { @@ -14,7 +16,7 @@ namespace SmallInjectorDemo /// /// Creates a new instance of . /// - public ServiceOne(IClock clock) + public ServiceOne(IClock clock, IModule[] modules) { _clock = clock; _id = Guid.NewGuid().EncodeBase64String(); @@ -24,12 +26,4 @@ namespace SmallInjectorDemo /// public override string ToString() => Helper.WriteMethodString(_clock, _id); } - - /// - /// Interface for service one. - /// - public interface IServiceOne - { - - } } \ No newline at end of file diff --git a/SmallInjectorDemo/ServiceTwo.cs b/SmallInjectorDemo/ServiceTwo.cs index 5a1ed03..24c5587 100644 --- a/SmallInjectorDemo/ServiceTwo.cs +++ b/SmallInjectorDemo/ServiceTwo.cs @@ -1,4 +1,5 @@ using System; +using SmallInjectorDemo.Interfaces; namespace SmallInjectorDemo { @@ -24,12 +25,4 @@ namespace SmallInjectorDemo /// public override string ToString() => Helper.WriteMethodString(_clock, _id); } - - /// - /// Interface for service two. - /// - public interface IServiceTwo - { - - } } \ No newline at end of file diff --git a/SmallInjectorDemo/SystemClock.cs b/SmallInjectorDemo/SystemClock.cs index b319c20..b2375aa 100644 --- a/SmallInjectorDemo/SystemClock.cs +++ b/SmallInjectorDemo/SystemClock.cs @@ -1,4 +1,5 @@ using System; +using SmallInjectorDemo.Interfaces; namespace SmallInjectorDemo {