Copycat 1.0.0
dotnet add package Copycat --version 1.0.0
NuGet\Install-Package Copycat -Version 1.0.0
<PackageReference Include="Copycat" Version="1.0.0" />
paket add Copycat --version 1.0.0
#r "nuget: Copycat, 1.0.0"
// Install Copycat as a Cake Addin #addin nuget:?package=Copycat&version=1.0.0 // Install Copycat as a Cake Tool #tool nuget:?package=Copycat&version=1.0.0
Copycat
Copycat is a source generator for creating decorators by templates. The source generator intents to simplify implementation of a Decorator Pattern.
Basic Use Case
To activate generator, use DecorateAttribute on a class. The class must be partial and have exactly one interface to decorate:
using Copycat;
public interface ISomeInterface
{
void DoSomething();
void DoSomethingElse(int a, string b);
}
[Decorate]
public partial class SimpleDecorator : ISomeInterface { }
In this example, Copycat generates pass-through decorator:
// <auto-generated/>
public partial class SimpleDecorator
{
private ISomeInterface _decorated;
public SimpleDecorator(ISomeInterface decorated)
{
_decorated = decorated;
}
public void DoSomething() => _decorated.DoSomething();
public void DoSomethingElse(int a, string b) => _decorated.DoSomethingElse(a, b);
}
To decorate with custom logic, use TemplateAttribute:
public interface ICache<T>
{
Task<T> Get(string key);
Task<T> Set(string key, T value);
}
[Decorate]
public partial class CacheDecorator<T> : ICache<T>
{
[Template]
public async Task<T> RetryOnce(Func<Task<T>> action, string key)
{
try
{
return await action();
}
catch (Exception e)
{
Console.WriteLine($"Retry {nameof(action)} for {key} due to {e.Message}");
return await action();
}
}
}
Template is a method that takes parameterless delegate which has the same return type as the method itself. Use any names for the template method and a delegate (as usual, it's better to keep them self-explanatory).
Copycat then generates decorator based on the template:
// <auto-generated/>
public partial class CacheDecorator<T>
{
private ICache<T> _decorated;
public CacheDecorator(ICache<T> decorated)
{
_decorated = decorated;
}
/// <see cref = "CacheDecorator.RetryOnce(Func{Task{T}}, string)"/>
public async Task<T> Get(string key)
{
try
{
return await _decorated.Get(key);
}
catch (Exception e)
{
Console.WriteLine($"Retry {nameof(Get)} for {key} due to {e.Message}");
return await _decorated.Get(key);
}
}
/// <see cref = "CacheDecorator.RetryOnce(Func{Task{T}}, string)"/>
public async Task<T> Set(string key, T value)
{
try
{
return await _decorated.Set(key, value);
}
catch (Exception e)
{
Console.WriteLine($"Retry {nameof(Set)} for {key} due to {e.Message}");
return await _decorated.Set(key, value);
}
}
}
Advanced Use Cases
That was only basic usage, see repository Wiki for more examples and details.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
-
net6.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 1,078 | 2/11/2024 |
1.0.0-beta.1 | 66 | 2/11/2024 |
0.9.0-beta.1 | 65 | 2/11/2024 |
0.8.0-beta.1 | 556 | 1/21/2024 |
0.7.0-beta.1 | 69 | 1/21/2024 |
0.6.0-beta.1 | 79 | 1/20/2024 |
0.5.0-beta.1 | 126 | 1/18/2024 |
0.4.0-beta.1 | 86 | 1/16/2024 |
0.3.0-beta.1 | 82 | 1/15/2024 |
0.2.0-beta.1 | 237 | 1/9/2024 |
0.1.0-beta.1 | 641 | 12/17/2023 |