From 07c626d42a4edb5d420513a9cd177685b35f76af Mon Sep 17 00:00:00 2001 From: Simon G Date: Thu, 19 Nov 2020 12:02:10 +0100 Subject: [PATCH] #45: add registration method for IMultipleMultitonRegistration --- LightweightIocContainer/IocContainer.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/LightweightIocContainer/IocContainer.cs b/LightweightIocContainer/IocContainer.cs index c95a555..cb64982 100644 --- a/LightweightIocContainer/IocContainer.cs +++ b/LightweightIocContainer/IocContainer.cs @@ -193,6 +193,14 @@ namespace LightweightIocContainer return registration; } + public IMultipleMultitonRegistration RegisterMultiton() where TImplementation : TInterface1, TInterface2 + { + IMultipleMultitonRegistration registration = _registrationFactory.RegisterMultiton(); + Register(registration); + + return registration; + } + /// /// Register an Interface as an abstract typed factory /// @@ -377,7 +385,7 @@ namespace LightweightIocContainer throw new MultitonResolveException($"Can not resolve multiton without the first argument being the scope (should be of type {registration.Scope}).", typeof(T)); //if a multiton for the given scope exists return it - var instances = _multitons.FirstOrDefault(m => m.type == typeof(T) && m.scope == registration.Scope).instances; //get instances for the given type and scope + var instances = _multitons.FirstOrDefault(m => m.type == registration.ImplementationType && m.scope == registration.Scope).instances; //get instances for the given type and scope (use implementation type to resolve the correct instance for multiple multiton registrations as well) if (instances != null) { if (instances.TryGetValue(scopeArgument, out object instance)) @@ -394,7 +402,7 @@ namespace LightweightIocContainer ConditionalWeakTable weakTable = new ConditionalWeakTable(); weakTable.Add(scopeArgument, newInstance); - _multitons.Add((typeof(T), registration.Scope, weakTable)); + _multitons.Add((registration.ImplementationType, registration.Scope, weakTable)); return newInstance; }