Skip to content

Commit

Permalink
ActivatorUtilities.CreateFactory created breaking change from version…
Browse files Browse the repository at this point in the history
… 4.2.2 to 5.0.1 khellang#235
  • Loading branch information
Napoleon Maraidonis committed Nov 20, 2024
1 parent e6a329e commit 62928a9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
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

0 comments on commit 62928a9

Please sign in to comment.