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

Select behavior for RegistrationStrategy.Skip #167 #172

Closed
wants to merge 3 commits 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
47 changes: 42 additions & 5 deletions src/Scrutor/RegistrationStrategy.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System.Linq;

namespace Scrutor;

public abstract class RegistrationStrategy
{
/// <summary>
/// Skips registrations for services that already exists.
/// Skips registrations for services that already exist using <see cref="ReplacementBehavior.Default"/>.
/// </summary>
public static readonly RegistrationStrategy Skip = new SkipRegistrationStrategy();
public static RegistrationStrategy Skip()
{
return new SkipRegistrationStrategy(SkipBehavior.Default);
}

/// <summary>
/// Skips registrations for services that already exist based on the specified <see cref="ReplacementBehavior"/>.
/// </summary>
/// <param name="behavior">The behavior to use when replacing services.</param>
public static RegistrationStrategy Skip(SkipBehavior behavior)
{
return new SkipRegistrationStrategy(behavior);
}

/// <summary>
/// Appends a new registration for existing services.
Expand Down Expand Up @@ -38,15 +51,39 @@ public static RegistrationStrategy Replace(ReplacementBehavior behavior)
}

/// <summary>
/// Applies the the <see cref="ServiceDescriptor"/> to the <see cref="IServiceCollection"/>.
/// Applies the selected <see cref="RegistrationStrategy"/> for the <see cref="ServiceDescriptor"/> to the <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="services">The service collection.</param>
/// <param name="descriptor">The descriptor to apply.</param>
public abstract void Apply(IServiceCollection services, ServiceDescriptor descriptor);

private sealed class SkipRegistrationStrategy : RegistrationStrategy
{
public override void Apply(IServiceCollection services, ServiceDescriptor descriptor) => services.TryAdd(descriptor);
public SkipRegistrationStrategy(SkipBehavior behavior)
{
Behavior = behavior;
}

private SkipBehavior Behavior { get; }

public override void Apply(IServiceCollection services, ServiceDescriptor descriptor)
{
if (Behavior is SkipBehavior.Default or SkipBehavior.ServiceType)
{
services.TryAdd(descriptor);
}
else if (Behavior is SkipBehavior.ImplementationType)
{
if (!services.Any((ServiceDescriptor d) => d.ImplementationType == descriptor.ImplementationType))
{
services.Add(descriptor);
}
}
else
{
services.TryAddEnumerable(descriptor);
}
}
}

private sealed class AppendRegistrationStrategy : RegistrationStrategy
Expand Down Expand Up @@ -110,4 +147,4 @@ public override void Apply(IServiceCollection services, ServiceDescriptor descri
services.Add(descriptor);
}
}
}
}
4 changes: 2 additions & 2 deletions src/Scrutor/ReplacementBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public enum ReplacementBehavior
ImplementationType = 2,

/// <summary>
/// Replace existing services by either service- or implementation type.
/// Replace existing services by either service or implementation type.
/// </summary>
All = ServiceType | ImplementationType
}
}
28 changes: 28 additions & 0 deletions src/Scrutor/SkipBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace Scrutor;

public enum SkipBehavior
{
/// <summary>
/// Skip registration if the service type has already been registered. Same as <see cref="ServiceCollectionDescriptorExtensions.TryAdd(IServiceCollection, Microsoft.Extensions.DependencyInjection.ServiceDescriptor)"/>.
/// </summary>
Default = 0,

/// <summary>
/// Skip registration if the service type has already been registered (default). Same as <see cref="ServiceCollectionDescriptorExtensions.TryAdd(IServiceCollection, Microsoft.Extensions.DependencyInjection.ServiceDescriptor)"/>.
/// </summary>
ServiceType = 1,

/// <summary>
/// Skip registration if the implementation type has already been registered.
/// </summary>
ImplementationType = 2,

/// <summary>
/// Skip registration if a descriptor with the same <see cref="ServiceDescriptor.ServiceType"/> and implementation has already been registered.
/// </summary>
Exact = 3,
}

75 changes: 70 additions & 5 deletions test/Scrutor.Tests/ScanningTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,86 @@ public void UsingRegistrationStrategy_None()
}

[Fact]
public void UsingRegistrationStrategy_SkipIfExists()
public void UsingRegistrationStrategy_SkipDefault()
{
Collection.Scan(scan => scan
.FromAssemblyOf<ITransientService>()
.AddClasses(classes => classes.AssignableTo<ITransientService>())
.UsingRegistrationStrategy(RegistrationStrategy.Skip())
.AsImplementedInterfaces()
.WithTransientLifetime()
.AddClasses(classes => classes.AssignableTo<ITransientService>())
.UsingRegistrationStrategy(RegistrationStrategy.Skip)
.UsingRegistrationStrategy(RegistrationStrategy.Skip())
.AsImplementedInterfaces()
.WithSingletonLifetime());

var services = Collection.GetDescriptors<ITransientService>();

Assert.Equal(4, services.Count(x => x.ServiceType == typeof(ITransientService)));
Assert.Equal(1, services.Count(x => x.Lifetime == ServiceLifetime.Transient));
Assert.Equal(0, services.Count(x => x.Lifetime == ServiceLifetime.Singleton));
}

[Fact]
public void UsingRegistrationStrategy_SkipServiceTypes()
{
Collection.Scan(scan => scan
.FromAssemblyOf<ITransientService>()
.AddClasses(classes => classes.AssignableTo<ITransientService>())
.UsingRegistrationStrategy(RegistrationStrategy.Skip(SkipBehavior.ServiceType))
.AsImplementedInterfaces()
.WithTransientLifetime()
.AddClasses(classes => classes.AssignableTo<ITransientService>())
.UsingRegistrationStrategy(RegistrationStrategy.Skip(SkipBehavior.ServiceType))
.AsImplementedInterfaces()
.WithSingletonLifetime());

var services = Collection.GetDescriptors<ITransientService>();

Assert.Equal(1, services.Count(x => x.Lifetime == ServiceLifetime.Transient));
Assert.Equal(0, services.Count(x => x.Lifetime == ServiceLifetime.Singleton));
}

[Fact]
public void UsingRegistrationStrategy_SkipImplementationTypes()
{
Collection.Scan(scan => scan
.FromAssemblyOf<ITransientService>()
.AddClasses(classes => classes.AssignableTo<ITransientService>())
.UsingRegistrationStrategy(RegistrationStrategy.Skip(SkipBehavior.ImplementationType))
.AsImplementedInterfaces()
.WithTransientLifetime()
.AddClasses(classes => classes.AssignableTo<ITransientService>())
.UsingRegistrationStrategy(RegistrationStrategy.Skip(SkipBehavior.ImplementationType))
.AsImplementedInterfaces()
.WithSingletonLifetime());


var services = Collection.GetDescriptors<ITransientService>();

Assert.Equal(4, services.Count(x => x.Lifetime == ServiceLifetime.Transient));
Assert.Equal(0, services.Count(x => x.Lifetime == ServiceLifetime.Singleton));
Assert.Equal(0, services.Count(x => x.ServiceType == typeof(IOtherInheritance)));
}

[Fact]
public void UsingRegistrationStrategy_SkipExactTypes()
{
Collection.Scan(scan => scan
.FromAssemblyOf<ITransientService>()
.AddClasses(classes => classes.AssignableTo<ITransientService>())
.UsingRegistrationStrategy(RegistrationStrategy.Skip(SkipBehavior.Exact))
.AsImplementedInterfaces()
.WithTransientLifetime()
.AddClasses(classes => classes.AssignableTo<ITransientService>())
.UsingRegistrationStrategy(RegistrationStrategy.Skip(SkipBehavior.Exact))
.AsImplementedInterfaces()
.WithSingletonLifetime());

var services = Collection.GetDescriptors<ITransientService>();

Assert.Equal(4, services.Count(x => x.Lifetime == ServiceLifetime.Transient));
Assert.Equal(0, services.Count(x => x.Lifetime == ServiceLifetime.Singleton));
Assert.Equal(1, Collection.Count(x => x.ServiceType == typeof(IOtherInheritance)));
}

[Fact]
Expand Down Expand Up @@ -558,7 +623,7 @@ public interface IOtherInheritance { }
[ServiceDescriptor(typeof(IDuplicateInheritance))]
[ServiceDescriptor(typeof(IDuplicateInheritance))]
public class DuplicateInheritance : IDuplicateInheritance, IOtherInheritance { }

public interface IDefault1 { }

public interface IDefault2 { }
Expand All @@ -573,7 +638,7 @@ public class DefaultAttributes : IDefault3Level2, IDefault1, IDefault2 { }
[CompilerGenerated]
public class CompilerGenerated { }

public class CombinedService2: IDefault1, IDefault2, IDefault3Level2 { }
public class CombinedService2 : IDefault1, IDefault2, IDefault3Level2 { }
}

namespace Scrutor.Tests.ChildNamespace
Expand Down