Skip to content

Commit 6b18b93

Browse files
committed
Add open-generic regression test for #1459
Mirrors the closed-generic DecoratorTests case: an open-generic decorator whose constructor takes a more-derived service dependency (IDecoratedService<T>) than the decorated service (IService<T>) must resolve that dependency from the container rather than receiving the decorated instance. Fails with InvalidCastException against the pre-fix middleware.
1 parent 2b4a81d commit 6b18b93

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

test/Autofac.Test/Features/Decorators/OpenGenericDecoratorTests.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,53 @@ public void ResolvesMultipleDecoratedServicesWhenResolvedByOtherServices()
923923
});
924924
}
925925

926+
// Issue 1459: A decorator constructor may take a dependency typed as a more
927+
// derived service than the one being decorated. The decorated instance must
928+
// not be force-injected into that parameter; it should be resolved normally.
929+
private class PlainService<T> : IService<T>
930+
{
931+
}
932+
933+
private class DerivedDependencyDecorator<T> : IService<T>
934+
{
935+
public DerivedDependencyDecorator(IDecoratedService<T> derived, IService<T> decorated)
936+
{
937+
Derived = derived;
938+
Decorated = decorated;
939+
}
940+
941+
public IDecoratedService<T> Derived
942+
{
943+
get;
944+
}
945+
946+
public IService<T> Decorated
947+
{
948+
get;
949+
}
950+
}
951+
952+
[Fact]
953+
public void DecoratorWithMoreDerivedServiceDependencyResolvesDependencyNormally()
954+
{
955+
// Issue 1459: The decorated service (IService<T>) should be supplied to
956+
// the "Decorated" parameter, while the more-derived "Derived"
957+
// (IDecoratedService<T>) parameter must be resolved from the container
958+
// rather than receiving the decorated instance (which is only an
959+
// IService<T> here, not an IDecoratedService<T>).
960+
var builder = new ContainerBuilder();
961+
builder.RegisterGeneric(typeof(PlainService<>)).As(typeof(IService<>));
962+
builder.RegisterGeneric(typeof(ImplementorA<>)).As(typeof(IDecoratedService<>));
963+
builder.RegisterGenericDecorator(typeof(DerivedDependencyDecorator<>), typeof(IService<>));
964+
var container = builder.Build();
965+
966+
var resolved = container.Resolve<IService<int>>();
967+
968+
var decorator = Assert.IsType<DerivedDependencyDecorator<int>>(resolved);
969+
Assert.IsType<PlainService<int>>(decorator.Decorated);
970+
Assert.IsType<ImplementorA<int>>(decorator.Derived);
971+
}
972+
926973
private interface ICommandHandler<T>
927974
{
928975
void Handle(T command);

0 commit comments

Comments
 (0)