Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing issue where validation was applied to the ServiceDescriptor by… #173

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion src/Scrutor/ServiceCollectionExtensions.Decoration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public static bool TryDecorate(this IServiceCollection services, DecorationStrat
var decoratedType = new DecoratedType(serviceDescriptor.ServiceType);

// Insert decorated
services.Add(serviceDescriptor.WithServiceType(decoratedType));
services.Add(serviceDescriptor.WithDecoratedType(decoratedType));

// Replace decorator
services[i] = serviceDescriptor.WithImplementationFactory(strategy.CreateDecorator(decoratedType));
Expand Down
9 changes: 6 additions & 3 deletions src/Scrutor/ServiceDescriptorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ internal static class ServiceDescriptorExtensions
public static ServiceDescriptor WithImplementationFactory(this ServiceDescriptor descriptor, Func<IServiceProvider, object> implementationFactory) =>
new(descriptor.ServiceType, implementationFactory, descriptor.Lifetime);

public static ServiceDescriptor WithServiceType(this ServiceDescriptor descriptor, Type serviceType) => descriptor switch
public static ServiceDescriptor WithDecoratedType(this ServiceDescriptor descriptor, DecoratedType serviceType) => descriptor switch
{
{ ImplementationType: not null } => new ServiceDescriptor(serviceType, descriptor.ImplementationType, descriptor.Lifetime),
{ ImplementationType: not null } => new ServiceDescriptor(serviceType, descriptor.ImplementationType.ToFactory(), descriptor.Lifetime),
{ ImplementationFactory: not null } => new ServiceDescriptor(serviceType, descriptor.ImplementationFactory, descriptor.Lifetime),
{ ImplementationInstance: not null } => new ServiceDescriptor(serviceType, descriptor.ImplementationInstance),
_ => throw new ArgumentException($"No implementation factory or instance or type found for {descriptor.ServiceType}.", nameof(descriptor))
};
};

private static Func<IServiceProvider, object> ToFactory(this Type serviceType) =>
serviceProvider => ActivatorUtilities.CreateInstance(serviceProvider, serviceType);
}
21 changes: 21 additions & 0 deletions test/Scrutor.Tests/DecorationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,27 @@ public void Issue148_Decorate_IsAbleToDecorateConcreateTypes()
Assert.NotNull(inner.Dependency);
}

[Fact]
public void Issue171_Decorate_IsAbleToCreateValidRegistration()
{
var services = new ServiceCollection();

services
.AddTransient<IDecoratedService, Decorated>()
.Decorate<IDecoratedService, Decorator>();

foreach (var serviceDescriptor in services)
{
if (serviceDescriptor.ImplementationType != null
&& serviceDescriptor.ServiceType != serviceDescriptor.ImplementationType)
{
var implementationTypeInterfaces = serviceDescriptor.ImplementationType.GetInterfaces();
var implementationAssignableToServiceType = implementationTypeInterfaces.Any(t => t == serviceDescriptor.ServiceType);
Assert.True(implementationAssignableToServiceType);
}
}
}

#region Individual functions tests

[Fact]
Expand Down