From 44f1631a278c6af0caca9ae309a46ecff766471c Mon Sep 17 00:00:00 2001 From: Antony Male Date: Thu, 1 May 2014 17:44:22 +0100 Subject: [PATCH] Fix bugs when using the StyletIoC syntax 'Bind(typeof(IInterface<>)).ToAllImplementations()' --- Stylet/StyletIoC/StyletIoCBuilder.cs | 14 +++++++----- .../StyletIoC/StyletIoCAutobindingTests.cs | 22 ++++++++++++++++--- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/Stylet/StyletIoC/StyletIoCBuilder.cs b/Stylet/StyletIoC/StyletIoCBuilder.cs index fd9897a..5891250 100644 --- a/Stylet/StyletIoC/StyletIoCBuilder.cs +++ b/Stylet/StyletIoC/StyletIoCBuilder.cs @@ -151,8 +151,10 @@ namespace StyletIoC return this; } - protected void EnsureType(Type implementationType) + protected void EnsureType(Type implementationType, Type serviceType = null) { + serviceType = serviceType ?? this.serviceType; + if (!implementationType.IsClass || implementationType.IsAbstract) throw new StyletIoCRegistrationException(String.Format("Type {0} is not a concrete class, and so can't be used to implemented service {1}", implementationType.Name, this.serviceType.Name)); @@ -169,9 +171,9 @@ namespace StyletIoC if (this.serviceType.GetTypeInfo().GenericTypeParameters.Length != implementationType.GetTypeInfo().GenericTypeParameters.Length) throw new StyletIoCRegistrationException(String.Format("If you're registering an unbound generic type to an unbound generic service, both service and type must have the same number of type parameters. Service: {0}, Type: {1}", this.serviceType.Name, implementationType.Name)); } - else if (this.serviceType.IsGenericTypeDefinition) + else if (serviceType.IsGenericTypeDefinition) { - throw new StyletIoCRegistrationException(String.Format("You cannot bind the bound generic / non-generic type {0} to unbound generic service {1}", implementationType.Name, this.serviceType.Name)); + throw new StyletIoCRegistrationException(String.Format("You cannot bind the bound generic / non-generic type {0} to the unbound generic service {1}", implementationType.Name, serviceType.Name)); } if (!implementationType.Implements(this.serviceType)) @@ -183,7 +185,7 @@ namespace StyletIoC { serviceType = serviceType ?? this.serviceType; - if (this.serviceType.IsGenericTypeDefinition) + if (serviceType.IsGenericTypeDefinition) { var unboundGeneric = new UnboundGeneric(implementationType, container, this.isSingleton); container.AddUnboundGeneric(new TypeKey(serviceType, this.Key), unboundGeneric); @@ -193,7 +195,7 @@ namespace StyletIoC var creator = new TypeCreator(implementationType, container); IRegistration registration = this.isSingleton ? (IRegistration)new SingletonRegistration(creator) : (IRegistration)new TransientRegistration(creator); - container.AddRegistration(new TypeKey(this.serviceType, this.Key ?? creator.AttributeKey), registration); + container.AddRegistration(new TypeKey(serviceType, this.Key ?? creator.AttributeKey), registration); } } @@ -262,7 +264,7 @@ namespace StyletIoC { try { - this.EnsureType(candidate.Type); + this.EnsureType(candidate.Type, candidate.Base); this.BindImplementationToService(container, candidate.Type, candidate.Base); } catch (StyletIoCRegistrationException e) diff --git a/StyletUnitTests/StyletIoC/StyletIoCAutobindingTests.cs b/StyletUnitTests/StyletIoC/StyletIoCAutobindingTests.cs index 63069e3..cfe2616 100644 --- a/StyletUnitTests/StyletIoC/StyletIoCAutobindingTests.cs +++ b/StyletUnitTests/StyletIoC/StyletIoCAutobindingTests.cs @@ -20,8 +20,13 @@ namespace StyletUnitTests class C21 : I2 { } class C22 : I2 { } + + interface I3 { } + class C31 : I3 { } + class C32 : I3 { } + [Inject("Key")] - class C3 { } + class C4 { } [Test] public void NongenericInterfaceToAllImplementations() @@ -101,8 +106,8 @@ namespace StyletUnitTests builder.Autobind(); var ioc = builder.BuildContainer(); - var result = ioc.Get("Key"); - Assert.IsInstanceOf(result); + var result = ioc.Get("Key"); + Assert.IsInstanceOf(result); } [Test] @@ -117,5 +122,16 @@ namespace StyletUnitTests var result2 = ioc.Get(); Assert.AreEqual(result2, result1); } + + [Test] + public void BindsGenericInterfaceToAllNonGenericImplementations() + { + var builder = new StyletIoCBuilder(); + builder.Bind(typeof(I3<>)).ToAllImplementations(); + var ioc = builder.BuildContainer(); + + var c31 = ioc.Get>(); + Assert.IsInstanceOf(c31); + } } }