Lifestyle Mismatch not detected when decorator applied #366

Closed
dotnetjunkie opened this Issue Jan 3, 2017 · 1 comment

Projects

None yet

1 participant

@dotnetjunkie
Collaborator

Under certain conditions, a lifestyle mismatch stays undetected when decorators are applied.

I found the following unit test that succeeded, while the call to GetInstance should fail because of the (very clear) lifestyle mismatch between TransactionHandlerDecorator<RealCommand> and StubCommandHandler:

[TestMethod]
public void GetInstance_DecoratorRegisteredTwiceAsSingleton_WrapsTheDecorateeTwice()
{
	// Arrange
	var container = new Container();

	container.Register<ICommandHandler<RealCommand>, StubCommandHandler>();

	// Register the same decorator twice. 
	container.RegisterDecorator(
		typeof(ICommandHandler<>),
		typeof(TransactionHandlerDecorator<>),
		Lifestyle.Singleton);

	container.RegisterDecorator(
		typeof(ICommandHandler<>),
		typeof(TransactionHandlerDecorator<>),
		Lifestyle.Singleton);

	// Act
	var decorator1 = (TransactionHandlerDecorator<RealCommand>)
		container.GetInstance<ICommandHandler<RealCommand>>();

	// uninteresting code removed
}
@dotnetjunkie dotnetjunkie added this to the v4 milestone Jan 3, 2017
@dotnetjunkie dotnetjunkie added a commit that referenced this issue Feb 18, 2017
@dotnetjunkie dotnetjunkie Unit test added. #366 27d2c4e
@dotnetjunkie
Collaborator

This problem was already solved in v4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment