RSCG – Farskeptic.AutoCompose
| name | Farskeptic.AutoCompose |
| nuget | https://www.nuget.org/packages/Farskeptic.AutoCompose/ |
| link | https://github.com/farskeptic/AutoCompose |
| author | farskeptic/jmagel |
Generating decorators for classes that implements interfaces.
This is how you can use Farskeptic.AutoCompose .
The code that you start with is
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Farskeptic.AutoCompose" Version="1.0.1" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
The code that you will use is
using Decorator; ICoffee c = new Coffee(); c = new CoffeeWithLogging(c); await c.Prepare(); var ingredients = c.GetIngredients();
namespace Decorator;
internal class Coffee : ICoffee
{
public string? Name { get; set; }
public async Task<bool> Prepare()
{
Console.WriteLine("start prepare coffee");
await Task.Delay(1000);
Console.WriteLine("finish prepare coffee");
return true;
}
public string[] GetIngredients() => new[] { "water","coffee" };
}
namespace Decorator;
internal interface ICoffee
{
Task<bool> Prepare();
string[] GetIngredients();
}
using AutoCompose.Generator.Attributes;
namespace Decorator;
[AutoCompose(typeof(ICoffee),nameof(_cof))]
internal partial class CoffeeWithLogging : ICoffee
{
protected ICoffee _cof;
public CoffeeWithLogging(ICoffee cof)
{
this._cof = cof;
}
public string[] GetIngredients()
{
Console.WriteLine("CoffeeWithLogging.GetIngredients");
return this._cof.GetIngredients();
}
}
The code that is generated is
// <auto-generated>
// WARNING: THIS CODE IS AUTO-GENERATED AT COMPILE-TIME. ANY CHANGES WILL BE OVERWRITTEN ON NEXT COMPILE.
// </auto-generated>
namespace Decorator
{
internal partial class CoffeeWithLogging
{
public virtual Task<bool> Prepare()
{
return _cof.Prepare();
}
}
}
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/Farskeptic.AutoCompose
Leave a Reply