Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/Scrutor/DecorationStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ internal static DecorationStrategy WithFactory(Type serviceType, string? service

protected static Func<IServiceProvider, object?, object> TypeDecorator(Type serviceType, string serviceKey, Type decoratorType)
{
var factory = ActivatorUtilities.CreateFactory(decoratorType, new[] { serviceType });
return (serviceProvider, _) =>
{
var instanceToDecorate = serviceProvider.GetRequiredKeyedService(serviceType, serviceKey);
return factory(serviceProvider, new object[] { instanceToDecorate });
return ActivatorUtilities.CreateInstance(serviceProvider, decoratorType, instanceToDecorate);
};
}

Expand Down
27 changes: 27 additions & 0 deletions test/Scrutor.Tests/DecorationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,23 @@ public void Issue148_Decorate_IsAbleToDecorateConcreateTypes()
Assert.NotNull(inner.Dependency);
}

[Fact]
public void Issue235_Decorate_IsAbleToDecorateClassesThatTheirConstructorDoesNotContainTheDecoratedTypeDirectly()
{
var provider = ConfigureProvider(services =>
{
services.AddScoped<Decorated>()
.AddScoped<IDecoratedService>(x => x.GetRequiredService<Decorated>())
.Decorate<IDecoratedService, Decorator3>();
});

using (var scope = provider.CreateScope())
{
var result = scope.ServiceProvider.GetService<IDecoratedService>();
Assert.NotNull(result);
}
}

#region Individual functions tests

[Fact]
Expand Down Expand Up @@ -470,6 +487,16 @@ public Decorator2(DecoratedService decoratedService)
public DecoratedService Inner { get; }
}

public class Decorator3 : IDecoratedService
{
public Decorator3(Decorated inner)
{
Inner = inner ?? throw new ArgumentNullException(nameof(inner));
}

public Decorated Inner { get; }
}

public interface IService { }

private class SomeRandomService : IService { }
Expand Down