Skip to content

Commit

Permalink
Merge branch 'main' of github.com:gustavopsantos/Reflex
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavopsantos committed Dec 4, 2023
2 parents 1c97333 + 04074fd commit 689327f
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Reflex is an [Dependency Injection](https://stackify.com/dependency-injection/)
- [Scopes](#-scopes)
- [Bindings](#-bindings)
- [Resolving](#-resolving)
- [Decorating](#-decorating)
- [Callbacks](#-callbacks)
- [Debugger](#-debugger)
- [Settings](#-settings)
Expand Down Expand Up @@ -333,6 +334,65 @@ private void Documentation_Bindings()
}
```

## 🪆 Decorating
Reflex supports decorator pattern through de `ContainerDescriptor` API `AddDecorator`, heres an usage example:

```csharp
public interface INumber
{
int Get();
}
```

```csharp
public class Number : INumber
{
private int _value;
public int Get() => _value;

public static Number FromValue(int value)
{
return new Number
{
_value = value
};
}
}
```

```csharp
public class DoubledNumber : INumber
{
private readonly INumber _number;
public DoubledNumber(INumber number) => _number = number;
public int Get() => _number.Get() * 2;
}
```

```csharp
public class HalvedNumber : INumber
{
private readonly INumber _number;
public HalvedNumber(INumber number) => _number = number;
public int Get() => _number.Get() / 2;
}
```

```csharp
var container = new ContainerDescriptor("")
.AddSingleton(Number.FromValue(10), contracts: typeof(INumber))
.AddDecorator(typeof(DoubledNumber), typeof(INumber))
.AddDecorator(typeof(HalvedNumber), typeof(INumber))
.AddDecorator(typeof(DoubledNumber), typeof(INumber))
.Build();

var number = container.Single<INumber>();
number.Get().Should().Be(20);
```
> An decorated singleton will respect singleton lifetime, returning always the same instance
> An decorated transient will respect transient lifetime, returning always a new instance
---

## 🪝 Callbacks
Expand Down

0 comments on commit 689327f

Please sign in to comment.