Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jwshyns/DudNet
Proxy-pattern source generator
https://github.com/jwshyns/DudNet
Last synced: 3 months ago
JSON representation
Proxy-pattern source generator
- Host: GitHub
- URL: https://github.com/jwshyns/DudNet
- Owner: jwshyns
- Created: 2023-08-21T00:10:07.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2023-10-24T03:18:18.000Z (about 1 year ago)
- Last Synced: 2024-05-12T12:41:52.832Z (6 months ago)
- Language: C#
- Size: 43 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- RSCG_Examples - https://github.com/jwshyns/DudNet
- csharp-source-generators - DudNet - ![stars](https://img.shields.io/github/stars/jwshyns/dudnet?style=flat-square&cacheSeconds=604800) ![last commit](https://img.shields.io/github/last-commit/jwshyns/dudnet?style=flat-square&cacheSeconds=604800) A C# source generator for implementing a proxy pattern. (Source Generators / Patterns)
README
# DudNet [![NuGet Badge](https://buildstats.info/nuget/Jwshyns.DudNet)](https://www.nuget.org/packages/Jwshyns.DudNet)
DudNet is a C# source generator for implementing a proxy pattern.## Example
Generating a proxy for a class is as simple as marking it with the `ProxyServiceAttribute` as follows:
```csharp
using DudNet.Attributes;public interface IExampleService {
public void ExampleFunction();
public int ExampleFunctionWithArgumentAndReturn(int number);
}[ProxyService]
public class ExampleService : IExampleService {
public void ExampleFunction(){
// omitted for brevity
}
public int ExampleFunctionWithArgumentAndReturn(int number){
// omitted for brevity
}
public void FunctionNotOnInterface(){
// ommitted for brevity
}
}
```Which would generate the following two classes:
```csharp
using System.Runtime.CompilerServices;
using DudNet.Attributes;public partial class ExampleServiceProxy : IExampleService {
private readonly IExampleService _service;
public void ExampleFunction() {
Interceptor();
ExampleFunctionInterceptor();
_service.ExampleFunction();
}
public int ExampleFunctionWithArgumentAndReturn(int number) {
Interceptor();
ExampleFunctionWithArgumentAndReturnInterceptor(number);
_service.ExampleFunctionWithArgumentAndReturn(number);
}
partial void Interceptor([CallerMemberName]string callerName = null);partial void ExampleFunctionInterceptor();
partial void ExampleFunctionWithArgumentAndReturnInterceptor(int number);
}
```
and
```csharp
using DudNet.Attributes;public class ExampleServiceDud : IExampleService {
public void ExampleFunction() {
}
public int ExampleFunctionWithArgumentAndReturn(int number) {
}}
```These generated classes can be used by further implementing the `partial` proxy class as follows:
```csharp
public partial class ExampleServiceProxy : IExampleService {
public ExampleServiceProxy(ExampleProxyService service) {
// Some logic to determine whether you want to effectively "disable" the service
if (Random.Shared.NextDouble() > 0.5)
{
_service = service;
return;
}
_service = new ExampleServiceDud();
}
partial void Interceptor([CallerMemberName]string callerName = null) {
Console.Writeline("'{caller}' was called", callerName);
}
partial void ExampleFunctionWithArgumentAndReturnInterceptor(int number) {
if(number > 5)
{
throw new Exception("Received number value '{number}' - too high!", number);
}
}}
```