Skip to content

Commit

Permalink
Consolidate Create methods
Browse files Browse the repository at this point in the history
  • Loading branch information
khellang committed May 23, 2022
1 parent 7af565f commit 4b7b637
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 27 deletions.
3 changes: 0 additions & 3 deletions src/Scrutor/Decoration/Decorator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ public static Decorator Create(Type serviceType, Type? decoratorType, Func<objec
return new Decorator(strategy);
}

public static Decorator Create<TService>(Type? decoratorType, Func<object, IServiceProvider, object>? decoratorFactory)
=> new(new ClosedTypeDecoratorStrategy(typeof(TService), decoratorType, decoratorFactory));

public bool TryDecorate(IServiceCollection services)
{
var decorated = DecorateServices(services);
Expand Down
36 changes: 12 additions & 24 deletions src/Scrutor/ServiceCollectionExtensions.Decoration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public static IServiceCollection Decorate<TService, TDecorator>(this IServiceCol
{
Preconditions.NotNull(services, nameof(services));

var decoration = Decorator.Create<TService>(typeof(TDecorator), null);
return decoration.Decorate(services);
return Decorator.Create(typeof(TService), typeof(TDecorator), null).Decorate(services);
}

/// <summary>
Expand All @@ -34,8 +33,7 @@ public static bool TryDecorate<TService, TDecorator>(this IServiceCollection ser
{
Preconditions.NotNull(services, nameof(services));

var decoration = Decorator.Create<TService>(typeof(TDecorator), null);
return decoration.TryDecorate(services);
return Decorator.Create(typeof(TService), typeof(TDecorator), null).TryDecorate(services);
}

/// <summary>
Expand All @@ -54,8 +52,7 @@ public static IServiceCollection Decorate(this IServiceCollection services, Type
Preconditions.NotNull(serviceType, nameof(serviceType));
Preconditions.NotNull(decoratorType, nameof(decoratorType));

var decoration = Decorator.Create(serviceType, decoratorType, null);
return decoration.Decorate(services);
return Decorator.Create(serviceType, decoratorType, null).Decorate(services);
}

/// <summary>
Expand All @@ -73,8 +70,7 @@ public static bool TryDecorate(this IServiceCollection services, Type serviceTyp
Preconditions.NotNull(serviceType, nameof(serviceType));
Preconditions.NotNull(decoratorType, nameof(decoratorType));

var decoration = Decorator.Create(serviceType, decoratorType, null);
return decoration.TryDecorate(services);
return Decorator.Create(serviceType, decoratorType, null).TryDecorate(services);
}

/// <summary>
Expand All @@ -92,8 +88,7 @@ public static IServiceCollection Decorate<TService>(this IServiceCollection serv
Preconditions.NotNull(services, nameof(services));
Preconditions.NotNull(decorator, nameof(decorator));

var decoration = Decorator.Create<TService>(null, (decorated, serviceProvider) => decorator((TService)decorated, serviceProvider));
return decoration.Decorate(services);
return Decorator.Create(typeof(TService), null, (decorated, serviceProvider) => decorator((TService)decorated, serviceProvider)).Decorate(services);
}

/// <summary>
Expand All @@ -110,8 +105,7 @@ public static bool TryDecorate<TService>(this IServiceCollection services, Func<
Preconditions.NotNull(services, nameof(services));
Preconditions.NotNull(decorator, nameof(decorator));

var decoration = Decorator.Create<TService>(null, (decorated, serviceProvider) => decorator((TService)decorated, serviceProvider));
return decoration.TryDecorate(services);
return Decorator.Create(typeof(TService), null, (decorated, serviceProvider) => decorator((TService)decorated, serviceProvider)).TryDecorate(services);
}

/// <summary>
Expand All @@ -129,8 +123,7 @@ public static IServiceCollection Decorate<TService>(this IServiceCollection serv
Preconditions.NotNull(services, nameof(services));
Preconditions.NotNull(decorator, nameof(decorator));

var decoration = Decorator.Create<TService>(null, (decorated, serviceProvider) => decorator((TService)decorated));
return decoration.Decorate(services);
return Decorator.Create(typeof(TService), null, (decorated, _) => decorator((TService)decorated)).Decorate(services);
}

/// <summary>
Expand All @@ -147,8 +140,7 @@ public static bool TryDecorate<TService>(this IServiceCollection services, Func<
Preconditions.NotNull(services, nameof(services));
Preconditions.NotNull(decorator, nameof(decorator));

var decoration = Decorator.Create<TService>(null, (decorated, serviceProvider) => decorator((TService)decorated));
return decoration.TryDecorate(services);
return Decorator.Create(typeof(TService), null, (decorated, _) => decorator((TService)decorated)).TryDecorate(services);
}

/// <summary>
Expand All @@ -167,8 +159,7 @@ public static IServiceCollection Decorate(this IServiceCollection services, Type
Preconditions.NotNull(serviceType, nameof(serviceType));
Preconditions.NotNull(decorator, nameof(decorator));

var decoration = Decorator.Create(serviceType, null, decorator);
return decoration.Decorate(services);
return Decorator.Create(serviceType, null, decorator).Decorate(services);
}

/// <summary>
Expand All @@ -186,8 +177,7 @@ public static bool TryDecorate(this IServiceCollection services, Type serviceTyp
Preconditions.NotNull(serviceType, nameof(serviceType));
Preconditions.NotNull(decorator, nameof(decorator));

var decoration = Decorator.Create(serviceType, null, decorator);
return decoration.TryDecorate(services);
return Decorator.Create(serviceType, null, decorator).TryDecorate(services);
}

/// <summary>
Expand All @@ -206,8 +196,7 @@ public static IServiceCollection Decorate(this IServiceCollection services, Type
Preconditions.NotNull(serviceType, nameof(serviceType));
Preconditions.NotNull(decorator, nameof(decorator));

var decoration = Decorator.Create(serviceType, null, (decorated, serviceProvider) => decorator(decorated));
return decoration.Decorate(services);
return Decorator.Create(serviceType, null, (decorated, _) => decorator(decorated)).Decorate(services);
}

/// <summary>
Expand All @@ -225,8 +214,7 @@ public static bool TryDecorate(this IServiceCollection services, Type serviceTyp
Preconditions.NotNull(serviceType, nameof(serviceType));
Preconditions.NotNull(decorator, nameof(decorator));

var decoration = Decorator.Create(serviceType, null, (decorated, serviceProvider) => decorator(decorated));
return decoration.TryDecorate(services);
return Decorator.Create(serviceType, null, (decorated, _) => decorator(decorated)).TryDecorate(services);
}
}
}

0 comments on commit 4b7b637

Please sign in to comment.