Skip to content

Commit

Permalink
Bug fix in conditional registrations.
Browse files Browse the repository at this point in the history
Fixes #333
  • Loading branch information
dotnetjunkie committed Nov 23, 2016
1 parent 76aaada commit e4d7694
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
42 changes: 42 additions & 0 deletions src/SimpleInjector.Core.Tests.Unit/RegisterConditionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1460,6 +1460,48 @@ public void GetInstance_ResolvingUnregisteredTypeWithStringDependencyRegisteredA
Assert.AreEqual(expectedValue, service.Dependency);
}

[TestMethod]
public void GetInstance_PredicateContextForOpenGenericConsumer_ContainsTheExpectedConsumerInfo()
{
// Arrange
InjectionConsumerInfo actualConsumer = null;

var container = ContainerFactory.New();

container.Register(typeof(IGeneric<>), typeof(GenericTypeWithLoggerDependency<>), Lifestyle.Transient);

RegisterConditionalConstant<ILogger>(container, new NullLogger(),
c => { actualConsumer = c.Consumer; return true; });

// Act
container.GetInstance<IGeneric<int>>();

// Assert
AssertThat.AreEqual(typeof(IGeneric<int>), actualConsumer.ServiceType);
AssertThat.AreEqual(typeof(GenericTypeWithLoggerDependency<int>), actualConsumer.ImplementationType);
}

[TestMethod]
public void GetInstance_PredicateContextForGenericConsumer_ContainsTheExpectedConsumerInfo()
{
// Arrange
InjectionConsumerInfo actualConsumer = null;

var container = ContainerFactory.New();

container.Register<IGeneric<int>, GenericTypeWithLoggerDependency<int>>(Lifestyle.Singleton);

RegisterConditionalConstant<ILogger>(container, new NullLogger(),
c => { actualConsumer = c.Consumer; return true; });

// Act
container.GetInstance<IGeneric<int>>();

// Assert
AssertThat.AreEqual(typeof(IGeneric<int>), actualConsumer.ServiceType);
AssertThat.AreEqual(typeof(GenericTypeWithLoggerDependency<int>), actualConsumer.ImplementationType);
}

private static void RegisterConditionalConstant<T>(Container container, T constant,
Predicate<PredicateContext> predicate)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ public class GenericType<T> : IGeneric<T>
{
}

public class GenericTypeWithLoggerDependency<T> : IGeneric<T>
{
public GenericTypeWithLoggerDependency(ILogger logger)
{
}
}

public class IntAndFloatGeneric : IGeneric<int>, IGeneric<float>
{
}
Expand Down
3 changes: 3 additions & 0 deletions src/SimpleInjector/Registration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,9 @@ private Action<object> BuildInstanceInitializer()

private Expression BuildNewExpression(Type serviceType, Type implementationType)
{
// HACK: Fixes #333. In case of a generic registration, the serviceType might be same as implementationType.
serviceType = this.GetCurrentProducer()?.ServiceType ?? serviceType;

ConstructorInfo constructor =
this.Container.Options.SelectConstructor(serviceType, implementationType);

Expand Down
9 changes: 8 additions & 1 deletion src/changes.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
Version [Version Number] ([Friendly Version Number]) [Release Date]


Version 3.3.0 (v3.3.0 RTM)
Version 3.3.1 (v3.3.1 RTM) 2016-11-23

Bug fixes:
-[Core] PredicateContext.Consumer.ServiceType property contained incorrect value when the
parent/consumer of a conditional registration was an open-generic registration. (fixes #333)


Version 3.3.0 (v3.3.0 RTM) 2016-11-21

Improvements/Changes:
-[Core] Marked ContainerOptions.ResolveUnregisteredCollections obsolete. (fixes #303)
Expand Down

0 comments on commit e4d7694

Please sign in to comment.