-
-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RegistrationStrategy to skip, append or replace existing services. (
#22)
- Loading branch information
1 parent
0ea37ce
commit 451c124
Showing
12 changed files
with
212 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
|
||
namespace Scrutor | ||
{ | ||
public abstract class RegistrationStrategy | ||
{ | ||
public static readonly RegistrationStrategy Skip = new SkipRegistrationStrategy(); | ||
|
||
public static readonly RegistrationStrategy Append = new AppendRegistrationStrategy(); | ||
|
||
public static RegistrationStrategy Replace(ReplacementBehavior behavior = ReplacementBehavior.Default) => new ReplaceRegistrationStrategy(behavior); | ||
|
||
public abstract void Apply(IServiceCollection services, ServiceDescriptor descriptor); | ||
|
||
private sealed class SkipRegistrationStrategy : RegistrationStrategy | ||
{ | ||
public override void Apply(IServiceCollection services, ServiceDescriptor descriptor) => services.TryAdd(descriptor); | ||
} | ||
|
||
private sealed class AppendRegistrationStrategy : RegistrationStrategy | ||
{ | ||
public override void Apply(IServiceCollection services, ServiceDescriptor descriptor) => services.Add(descriptor); | ||
} | ||
|
||
private sealed class ReplaceRegistrationStrategy : RegistrationStrategy | ||
{ | ||
public ReplaceStrategy(ReplacementBehavior behavior) | ||
{ | ||
Behavior = behavior; | ||
} | ||
|
||
private ReplacementBehavior Behavior { get; } | ||
|
||
public override void Apply(IServiceCollection services, ServiceDescriptor descriptor) | ||
{ | ||
var behavior = Behavior; | ||
|
||
if (behavior == ReplacementBehavior.Default) | ||
{ | ||
behavior = ReplacementBehavior.ServiceType; | ||
} | ||
|
||
for (var i = services.Count - 1; i >= 0; i--) | ||
{ | ||
if ((behavior.HasFlag(ReplacementBehavior.ServiceType) && services[i].ServiceType == descriptor.ServiceType) | ||
|| (behavior.HasFlag(ReplacementBehavior.ImplementationType) && services[i].ImplementationType == descriptor.ImplementationType)) | ||
{ | ||
services.RemoveAt(i); | ||
} | ||
} | ||
|
||
services.Add(descriptor); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
|
||
namespace Scrutor | ||
{ | ||
[Flags] | ||
public enum ReplacementBehavior | ||
{ | ||
/// <summary> | ||
/// ServiceType only is the default | ||
/// </summary> | ||
Default = 0, | ||
/// <summary> | ||
/// Replace by ServiceType (default) | ||
/// </summary> | ||
ServiceType = 1, | ||
/// <summary> | ||
/// Replace by ImplementationType. | ||
/// </summary> | ||
ImplementationType = 2, | ||
All = ServiceType | ImplementationType | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters