Fix bugs when using the StyletIoC syntax 'Bind(typeof(IInterface<>)).ToAllImplementations()'

This commit is contained in:
Antony Male 2014-05-01 17:44:22 +01:00
parent 998237b83d
commit 44f1631a27
2 changed files with 27 additions and 9 deletions

View File

@ -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)

View File

@ -20,8 +20,13 @@ namespace StyletUnitTests
class C21<T> : I2<T> { }
class C22<T> : I2<T> { }
interface I3<T> { }
class C31 : I3<int> { }
class C32 : I3<string> { }
[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<C3>("Key");
Assert.IsInstanceOf<C3>(result);
var result = ioc.Get<C4>("Key");
Assert.IsInstanceOf<C4>(result);
}
[Test]
@ -117,5 +122,16 @@ namespace StyletUnitTests
var result2 = ioc.Get<C11>();
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<I3<int>>();
Assert.IsInstanceOf<C31>(c31);
}
}
}