-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for Decorator registration
- Loading branch information
Showing
5 changed files
with
240 additions
and
7 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
src/Bindicate.Tests/Decorator/RegisterDecoratorAttributeTests.cs
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,98 @@ | ||
using Bindicate.Attributes; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Bindicate.Tests.Decorator; | ||
|
||
public class RegisterDecoratorAttributeTests | ||
{ | ||
public interface IOperation | ||
{ | ||
int Perform(int a, int b); | ||
} | ||
|
||
[AddSingleton(typeof(IOperation))] | ||
public class Operation : IOperation | ||
{ | ||
public int Perform(int a, int b) | ||
{ | ||
return a + b; | ||
} | ||
} | ||
|
||
[RegisterDecorator(typeof(IOperation))] | ||
public class LoggingOperationDecorator : IOperation | ||
{ | ||
private readonly IOperation _innerOperation; | ||
|
||
public LoggingOperationDecorator(IOperation innerOperation) | ||
{ | ||
_innerOperation = innerOperation; | ||
} | ||
|
||
public int Perform(int a, int b) | ||
{ | ||
Console.WriteLine($"Logging before operation: {a}, {b}"); | ||
var result = _innerOperation.Perform(a, b); | ||
Console.WriteLine($"Logging after operation: result {result}"); | ||
return result; | ||
} | ||
} | ||
|
||
// Test class to verify the decorator implementation using xUnit and FluentAssertions | ||
public class DecoratorTests | ||
{ | ||
private readonly IServiceProvider _serviceProvider; | ||
|
||
public DecoratorTests() | ||
{ | ||
var services = new ServiceCollection(); | ||
|
||
// Use the AutowiringBuilder to register services and decorators | ||
services.AddAutowiringForAssembly(typeof(IOperation).Assembly) | ||
.Register(); | ||
|
||
_serviceProvider = services.BuildServiceProvider(); | ||
} | ||
|
||
[Fact] | ||
public void Operation_ShouldBeDecoratedWithLogging() | ||
{ | ||
var operation = _serviceProvider.GetRequiredService<IOperation>(); | ||
|
||
using var consoleOutput = new ConsoleOutput(); | ||
|
||
var result = operation.Perform(5, 7); | ||
|
||
result.Should().Be(12); | ||
|
||
var expectedOutput = $"Logging before operation: 5, 7{Environment.NewLine}Logging after operation: result 12{Environment.NewLine}"; | ||
consoleOutput.GetOutput().Should().Be(expectedOutput); | ||
|
||
operation.Should().BeOfType<LoggingOperationDecorator>(); | ||
} | ||
} | ||
|
||
public class ConsoleOutput : IDisposable | ||
{ | ||
private readonly StringWriter _stringWriter; | ||
private readonly TextWriter _originalOutput; | ||
|
||
public ConsoleOutput() | ||
{ | ||
_stringWriter = new StringWriter(); | ||
_originalOutput = Console.Out; | ||
Console.SetOut(_stringWriter); | ||
} | ||
|
||
public string GetOutput() | ||
{ | ||
return _stringWriter.ToString(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Console.SetOut(_originalOutput); | ||
_stringWriter.Dispose(); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Bindicate/Attributes/Decorator/RegisterDecoratorAttribute.cs
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,31 @@ | ||
namespace Bindicate.Attributes; | ||
|
||
/// <summary> | ||
/// Specifies that a class is a decorator for a specified service type. | ||
/// Decorators are applied in the order specified by the <see cref="Order"/> property. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] | ||
public class RegisterDecoratorAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// Gets the type of the service to be decorated. | ||
/// </summary> | ||
public Type ServiceType { get; } | ||
|
||
/// <summary> | ||
/// Gets the order in which the decorator should be applied. | ||
/// Lower values are applied first. | ||
/// </summary> | ||
public int Order { get; } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="RegisterDecoratorAttribute"/> class. | ||
/// </summary> | ||
/// <param name="serviceType">The type of the service to be decorated.</param> | ||
/// <param name="order">The order in which the decorator should be applied. Lower values are applied first.</param> | ||
public RegisterDecoratorAttribute(Type serviceType, int order = 0) | ||
{ | ||
ServiceType = serviceType; | ||
Order = order; | ||
} | ||
} |
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