Skip to content

Commit

Permalink
Move utility into extension method
Browse files Browse the repository at this point in the history
  • Loading branch information
khellang committed May 23, 2022
1 parent 4b7b637 commit e03e051
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
14 changes: 2 additions & 12 deletions src/Scrutor/Decoration/Decorator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,11 @@ private int DecorateServices(IServiceCollection services)
{
var decoratedType = new DecoratedType(serviceDescriptor.ServiceType);

var decoratorFactory = DecoratorStrategy.CreateDecorator(decoratedType);

// insert decorated
var decoratedServiceDescriptor = CreateDecoratedServiceDescriptor(serviceDescriptor, decoratedType);
services.Add(decoratedServiceDescriptor);
services.Add(serviceDescriptor.WithServiceType(decoratedType));

// replace decorator
var decoratorFactory = DecoratorStrategy.CreateDecorator(decoratedType);
services[i] = new ServiceDescriptor(serviceDescriptor.ServiceType, decoratorFactory, serviceDescriptor.Lifetime);

++decorated;
Expand All @@ -74,13 +72,5 @@ private int DecorateServices(IServiceCollection services)
}

private static bool IsNotAlreadyDecorated(ServiceDescriptor serviceDescriptor) => serviceDescriptor.ServiceType is not DecoratedType;

private static ServiceDescriptor CreateDecoratedServiceDescriptor(ServiceDescriptor serviceDescriptor, Type decoratedType) => serviceDescriptor switch
{
{ ImplementationType: not null } => new ServiceDescriptor(decoratedType, serviceDescriptor.ImplementationType, serviceDescriptor.Lifetime),
{ ImplementationFactory: not null } => new ServiceDescriptor(decoratedType, serviceDescriptor.ImplementationFactory, serviceDescriptor.Lifetime),
{ ImplementationInstance: not null } => new ServiceDescriptor(decoratedType, serviceDescriptor.ImplementationInstance),
_ => throw new ArgumentException($"No implementation factory or instance or type found for {serviceDescriptor.ServiceType}.", nameof(serviceDescriptor))
};
}
}
15 changes: 15 additions & 0 deletions src/Scrutor/ServiceDescriptorExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using Microsoft.Extensions.DependencyInjection;

namespace Scrutor;

internal static class ServiceDescriptorExtensions
{
public static ServiceDescriptor WithServiceType(this ServiceDescriptor descriptor, Type serviceType) => descriptor switch
{
{ ImplementationType: not null } => new ServiceDescriptor(serviceType, descriptor.ImplementationType, 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))
};
}

0 comments on commit e03e051

Please sign in to comment.