Category: .NET Core

RSCG – ProxyGen

RSCG – ProxyGen
 
 

name ProxyGen
nuget https://www.nuget.org/packages/ProxyGen.net/
link https://github.com/Sholtee/ProxyGen
author Dénes Solti

intercepting and duck typing

 

This is how you can use ProxyGen .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>
  <ItemGroup>
    <PackageReference Include="ProxyGen.NET" Version="8.2.1" />
  </ItemGroup>

</Project>


The code that you will use is


Person person = new ();
person.FirstName= "Andrei";
person.LastName = "Ignat";
IPerson duck = DuckGenerator<IPerson, Person>.Activate(Tuple.Create(person));
Console.WriteLine(duck.FullName());



namespace ProxyGenDemo;

public class Person 
{
    public int ID { get; set; }
    public string? FirstName { get; set; }
    public string? LastName { get; set; }
    public string FullName()
    {
        return $"{FirstName} {LastName}";
    }
}



namespace ProxyGenDemo;

public interface IPerson
{
    string? FirstName { get; set; }
    string? LastName { get; set; }

    string FullName();
}


global using ProxyGenDemo;
global using Solti.Utils.Proxy.Generators;
global using Solti.Utils.Proxy.Attributes;

//[assembly: EmbedGeneratedType(typeof(ProxyGenerator<IMyInterface, MyInterceptor<IMyInterface>>))]
[assembly: EmbedGeneratedType(typeof(DuckGenerator<IPerson, Person>))]

 

The code that is generated is

#pragma warning disable
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("ProxyGen.NET", "8.2.1.0"), global::System.Diagnostics.DebuggerNonUserCodeAttribute, global::System.Runtime.CompilerServices.CompilerGeneratedAttribute]
internal sealed class Duck_BB1E45629CF5010E4068E5BFBB7EF53B : global::Solti.Utils.Proxy.Internals.DuckBase<global::ProxyGenDemo.Person>, global::ProxyGenDemo.IPerson
{
    public Duck_BB1E45629CF5010E4068E5BFBB7EF53B(global::ProxyGenDemo.Person target) : base(target)
    {
    }

    [global::System.Runtime.CompilerServices.MethodImplAttribute(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
    global::System.String global::ProxyGenDemo.IPerson.FullName() => this.Target.FullName();
    global::System.String global::ProxyGenDemo.IPerson.FirstName {[global::System.Runtime.CompilerServices.MethodImplAttribute(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
        get => this.Target.FirstName; [global::System.Runtime.CompilerServices.MethodImplAttribute(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
        set => this.Target.FirstName = value; }

    global::System.String global::ProxyGenDemo.IPerson.LastName {[global::System.Runtime.CompilerServices.MethodImplAttribute(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
        get => this.Target.LastName; [global::System.Runtime.CompilerServices.MethodImplAttribute(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
        set => this.Target.LastName = value; }

    public static readonly global::System.Func<global::System.Object, global::System.Object> __Activator = tuple =>
    {
        switch (tuple)
        {
            case global::System.Tuple<global::ProxyGenDemo.Person> t0:
                return new global::Duck_BB1E45629CF5010E4068E5BFBB7EF53B(t0.Item1);
            default:
                throw new global::System.MissingMethodException("Constructor with the given layout cannot be found.");
        }
    };
    [global::System.Runtime.CompilerServices.ModuleInitializerAttribute]
    public static void Initialize() => global::Solti.Utils.Proxy.Internals.LoadedTypes.Register(typeof(global::Duck_BB1E45629CF5010E4068E5BFBB7EF53B));
}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/ProxyGen

RSCG – DeeDee

RSCG – DeeDee
 
 

name DeeDee
nuget https://www.nuget.org/packages/DeeDee/
link https://github.com/joh-pot/DeeDee/
author joh-pot

Mediatr generated data

 

This is how you can use DeeDee .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>
	<ItemGroup>
		<PackageReference Include="DeeDee" Version="2.0.0" />
		<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
		
	</ItemGroup>

</Project>


The code that you will use is


Console.WriteLine("Hello, World!");
ServiceCollection services = new ();
DeeDeeDemo.DeeDee.Generated.IocExtensions.AddDispatcher(services);

services.AddSingleton(typeof(IPipelineAction<Ping, Pong>), typeof(GenericLoggerHandler)); // This will run 1st

var serviceProvider = services.BuildServiceProvider();

var mediator = serviceProvider.GetRequiredService<DeeDeeDemo.DeeDee.Generated.Models.IDispatcher>();
var id = Guid.NewGuid();
var request = new Ping(id);

var response = mediator.Send(request);

Console.WriteLine("-----------------------------------");
Console.WriteLine("ID: " + id);
Console.WriteLine(request);
Console.WriteLine(response);




public sealed record Ping(Guid Id) : IRequest<Pong>;

public sealed record Pong(Guid Id);


public sealed class PingHandler : IPipelineAction<Ping, Pong>
{

    public Pong Invoke(Ping request, ref PipelineContext<Pong> context, Next<Pong> next)
    {
        Console.WriteLine("4) Returning pong!");
        return new Pong(request.Id);
    }
}



public sealed class GenericLoggerHandler : IPipelineAction<Ping, Pong>
{
    public Pong Invoke(Ping request, ref PipelineContext<Pong> context, Next<Pong> next)
    {
        Console.WriteLine("1) Running logger handler");
        try
        {
            var response = next(request , ref context);
            Console.WriteLine("5) No error!");
            return response;
        }
        catch (Exception ex)
        {
            Console.WriteLine("error:" + ex.Message);
            throw;
        }
    }
}

 

The code that is generated is

using System;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
using DeeDee.Models;
using ServiceProvider = DeeDee.Models.ServiceProvider;

namespace DeeDeeDemo.DeeDee.Generated.Models
{
    public class Dispatcher : IDispatcher
    {
        private readonly ServiceProvider _serviceFactory;
        private readonly Lazy<Next<Pong>> _Ping_Pong_lazy;
        public Dispatcher(ServiceProvider service)
        {
            _serviceFactory = service;
            _Ping_Pong_lazy = new Lazy<Next<Pong>>(Build<Ping, Pong>);
        }

        public Pong Send(Ping request)
        {
            var context = new PipelineContext<Pong>();
            Next<Pong> builtPipeline = _Ping_Pong_lazy.Value;
            return builtPipeline(request, ref context);
        }

        private NextAsync BuildAsync<TRequest>()
            where TRequest : IRequest
        {
            {
                var actions = _serviceFactory.GetServices<IPipelineActionAsync<TRequest>>();
                var builtPipeline = actions.Aggregate((NextAsync)((req, ctx, tkn) => Task.CompletedTask), (next, pipeline) => (req, ctx, tkn) => pipeline.InvokeAsync((TRequest)req, ctx, next, tkn));
                return builtPipeline;
            }
        }

        private Next Build<TRequest>()
            where TRequest : IRequest
        {
            {
                var actions = _serviceFactory.GetServices<IPipelineAction<TRequest>>();
                var builtPipeline = actions.Aggregate((Next)((IRequest req, ref PipelineContext ctx) =>
                {
                    {
                    }
                }), (next, pipeline) => (IRequest req, ref PipelineContext ctx) => pipeline.Invoke((TRequest)req, ref ctx, next));
                return builtPipeline;
            }
        }

        private NextAsync<TResponse> BuildAsync<TRequest, TResponse>()
            where TRequest : IRequest<TResponse>
        {
            {
                var actions = _serviceFactory.GetServices<IPipelineActionAsync<TRequest, TResponse>>();
                var builtPipeline = actions.Aggregate((NextAsync<TResponse>)((req, ctx, tkn) => Task.FromResult(ctx.Result)), (next, pipeline) => (req, ctx, tkn) => pipeline.InvokeAsync((TRequest)req, ctx, next, tkn));
                return builtPipeline;
            }
        }

        private Next<TResponse> Build<TRequest, TResponse>()
            where TRequest : IRequest<TResponse>
        {
            {
                var actions = _serviceFactory.GetServices<IPipelineAction<TRequest, TResponse>>();
                var builtPipeline = actions.Aggregate((Next<TResponse>)((IRequest<TResponse> req, ref PipelineContext<TResponse> ctx) => ctx.Result), (next, pipeline) => (IRequest<TResponse> req, ref PipelineContext<TResponse> ctx) => pipeline.Invoke((TRequest)req, ref ctx, next));
                return builtPipeline;
            }
        }
    }
}
using System;
using System.Threading;
using System.Threading.Tasks;
using DeeDee.Models;

namespace DeeDeeDemo.DeeDee.Generated.Models
{
    public interface IDispatcher
    {
        public Pong Send(Ping request);
    }
}

            using System;
            using System.Linq;
            using System.Reflection;
            using DeeDee.Models;
            using DeeDeeDemo.DeeDee.Generated.Models;
            using Microsoft.Extensions.DependencyInjection;
            using ServiceProvider = DeeDee.Models.ServiceProvider;
            namespace DeeDeeDemo.DeeDee.Generated

            {
                internal static class IocExtensions
                {
                    public static IServiceCollection AddDispatcher(this IServiceCollection services, Lifetime lifetime = Lifetime.Singleton)
                    {
                        switch(lifetime)
                        {
                            case Lifetime.Singleton:
                                services.AddSingleton<IDispatcher, Dispatcher>();
                                services.AddSingleton<ServiceProvider>(ctx => ctx.GetRequiredService);
                                break;

                            case Lifetime.Scoped:
                                services.AddScoped<IDispatcher, Dispatcher>();
                                services.AddScoped<ServiceProvider>(ctx => ctx.GetRequiredService);
                                break;

                            case Lifetime.Transient:
                                services.AddTransient<IDispatcher, Dispatcher>();
                                services.AddTransient<ServiceProvider>(ctx => ctx.GetRequiredService);
                                break;
                        }
                        
                        RegisterPipelineActions(services);
                        
                        return services;
                    }

                    private static void RegisterPipelineActions(IServiceCollection services)
                    {
                        var pipelineTypes = AppDomain
                            .CurrentDomain
                            .GetAssemblies()
                            .SelectMany
                            (
                                a => a.GetTypes().Where
                                (
                                   x => !x.IsInterface &&
                                        !x.IsAbstract &&
                                        x.GetInterfaces()
                                            .Any
                                            (
                                                y => y.Name.Equals(typeof(IPipelineActionAsync<,>).Name,
                                                         StringComparison.InvariantCulture) ||
                                                     y.Name.Equals(typeof(IPipelineActionAsync<>).Name,
                                                         StringComparison.InvariantCulture) ||
                                                     y.Name.Equals(typeof(IPipelineAction<>).Name,
                                                         StringComparison.InvariantCulture) ||
                                                     y.Name.Equals(typeof(IPipelineAction<,>).Name, StringComparison.InvariantCulture)
                                            )
                                ).GroupBy(type => type.GetInterfaces()[0]).SelectMany(g => g.OrderByDescending(s => s.GetCustomAttribute<StepAttribute>()?.Order))
                            );

                        foreach (var type in pipelineTypes)
                        {
                            foreach (var implementedInterface in type.GetInterfaces())
                            {
                                var bindAs = type.GetCustomAttribute<BindAsAttribute>();
                                switch (bindAs?.Lifetime)
                                {
                                    case Lifetime.Singleton:
                                        services.AddSingleton(implementedInterface, type);
                                        break;
                                    case Lifetime.Scoped:
                                        services.AddScoped(implementedInterface, type);
                                        break;  
                                    case Lifetime.Transient:
                                        services.AddTransient(implementedInterface, type);
                                        break;
                                    default:
                                        services.AddSingleton(implementedInterface, type);
                                        break;
                                }
                                
                            }
                        } 
                    }
                }
            }

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/DeeDee

RSCG – MemoryPack

RSCG – MemoryPack
 
 

name MemoryPack
nuget https://www.nuget.org/packages/MemoryPack/
link https://github.com/Cysharp/MemoryPack
author Cysharp, Inc

Efficient serializer

 

This is how you can use MemoryPack .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="MemoryPack" Version="1.9.16" />
  </ItemGroup>
	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>
</Project>


The code that you will use is


var v = new Person { Age = 53, Name = "Andrei Ignat" };

var bin = MemoryPackSerializer.Serialize(v);
var val = MemoryPackSerializer.Deserialize<Person>(bin);
Console.WriteLine(val.Name);


namespace MemoryPackDemo;

[MemoryPackable]
public partial class Person
{
    public int Age { get; set; }
    public string? Name { get; set; }
}


 

The code that is generated is


// <auto-generated/>
#nullable enable
#pragma warning disable CS0108 // hides inherited member
#pragma warning disable CS0162 // Unreachable code
#pragma warning disable CS0164 // This label has not been referenced
#pragma warning disable CS0219 // Variable assigned but never used
#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
#pragma warning disable CS8601 // Possible null reference assignment
#pragma warning disable CS8602
#pragma warning disable CS8604 // Possible null reference argument for parameter
#pragma warning disable CS8619
#pragma warning disable CS8620
#pragma warning disable CS8631 // The type cannot be used as type parameter in the generic type or method
#pragma warning disable CS8765 // Nullability of type of parameter
#pragma warning disable CS9074 // The 'scoped' modifier of parameter doesn't match overridden or implemented member
#pragma warning disable CA1050 // Declare types in namespaces.

using System;
using MemoryPack;

namespace MemoryPackDemo;

/// <remarks>
/// MemoryPack GenerateType: Object<br/>
/// <code>
/// <b>int</b> Age<br/>
/// <b>string</b> Name<br/>
/// </code>
/// </remarks>
partial class Person : IMemoryPackable<Person>
{


    static Person()
    {
        global::MemoryPack.MemoryPackFormatterProvider.Register<Person>();
    }

    [global::MemoryPack.Internal.Preserve]
    static void IMemoryPackFormatterRegister.RegisterFormatter()
    {
        if (!global::MemoryPack.MemoryPackFormatterProvider.IsRegistered<Person>())
        {
            global::MemoryPack.MemoryPackFormatterProvider.Register(new global::MemoryPack.Formatters.MemoryPackableFormatter<Person>());
        }
        if (!global::MemoryPack.MemoryPackFormatterProvider.IsRegistered<Person[]>())
        {
            global::MemoryPack.MemoryPackFormatterProvider.Register(new global::MemoryPack.Formatters.ArrayFormatter<Person>());
        }

    }

    [global::MemoryPack.Internal.Preserve]
    static void IMemoryPackable<Person>.Serialize<TBufferWriter>(ref MemoryPackWriter<TBufferWriter> writer, scoped ref Person? value) 
    {

        if (value == null)
        {
            writer.WriteNullObjectHeader();
            goto END;
        }

        writer.WriteUnmanagedWithObjectHeader(2, value.@Age);
        writer.WriteString(value.@Name);

    END:

        return;
    }

    [global::MemoryPack.Internal.Preserve]
    static void IMemoryPackable<Person>.Deserialize(ref MemoryPackReader reader, scoped ref Person? value)
    {

        if (!reader.TryReadObjectHeader(out var count))
        {
            value = default!;
            goto END;
        }


        
        int __Age;
        string __Name;

        
        if (count == 2)
        {
            if (value == null)
            {
                reader.ReadUnmanaged(out __Age);
                __Name = reader.ReadString();


                goto NEW;
            }
            else
            {
                __Age = value.@Age;
                __Name = value.@Name;

                reader.ReadUnmanaged(out __Age);
                __Name = reader.ReadString();

                goto SET;
            }

        }
        else if (count > 2)
        {
            MemoryPackSerializationException.ThrowInvalidPropertyCount(typeof(Person), 2, count);
            goto READ_END;
        }
        else
        {
            if (value == null)
            {
               __Age = default!;
               __Name = default!;
            }
            else
            {
               __Age = value.@Age;
               __Name = value.@Name;
            }


            if (count == 0) goto SKIP_READ;
            reader.ReadUnmanaged(out __Age); if (count == 1) goto SKIP_READ;
            __Name = reader.ReadString(); if (count == 2) goto SKIP_READ;

    SKIP_READ:
            if (value == null)
            {
                goto NEW;
            }
            else            
            {
                goto SET;
            }

        }

    SET:
        
        value.@Age = __Age;
        value.@Name = __Name;
        goto READ_END;

    NEW:
        value = new Person()
        {
            @Age = __Age,
            @Name = __Name
        };
    READ_END:

    END:

        return;
    }
}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/MemoryPack

RSCG – Matryoshki

RSCG – Matryoshki
 
 

name Matryoshki
nuget https://www.nuget.org/packages/Matryoshki/
link https://github.com/krasin-ga/matryoshki/
author Georgy Krasin

Adding decorators to an implementation of interface

 

This is how you can use Matryoshki .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>
	<ItemGroup>
		<PackageReference Include="Matryoshki" Version="1.1.4" />
		<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />

	</ItemGroup>


</Project>


The code that you will use is



using Matryoshki.Abstractions;
Decorate<IPerson> // you can use Decorate<> alias if you prefer
    .With<AddLog>()
    .Name<PersonMatryoshka>();

var services = new ServiceCollection();

services.AddTransient<IPerson, Person>();
services.AddTransient<PersonMatryoshka, PersonMatryoshka>();
var serviceProvider = services.BuildServiceProvider();
var sp=serviceProvider.GetRequiredService<PersonMatryoshka>();
sp.FirstName = "Andrei";
sp.LastName = "Ignat";
Console.WriteLine(sp.FullName());




namespace MatryoshkiDemo;

internal class AddLog : IAdornment
{
    public TResult MethodTemplate<TResult>(Call<TResult> call)
    {        
        Console.WriteLine($"start Calling {call.MemberName}  !");
        var data    =call.Forward();
        Console.WriteLine($"end calling {call.MemberName} !");
        return data;

    }
}


namespace MatryoshkiDemo;

public interface IPerson
{
    string? FirstName { get; set; }
    int ID { get; set; }
    string? LastName { get; set; }

    string FullName();
}



namespace MatryoshkiDemo;

public class Person : IPerson
{
    public int ID { get; set; }
    public string? FirstName { get; set; }
    public string? LastName { get; set; }

    public string FullName()
    {
        return $"{FirstName} {LastName}";
    }
}


 

The code that is generated is

[assembly: Matryoshki.Abstractions.CompiledAdornmentAttribute("MatryoshkiDemo.AddLog", "AddLog", "DQpuYW1lc3BhY2UgTWF0cnlvc2hraURlbW87DQoNCmludGVybmFsIGNsYXNzIEFkZExvZyA6IElBZG9ybm1lbnQNCnsNCiAgICBwdWJsaWMgVFJlc3VsdCBNZXRob2RUZW1wbGF0ZTxUUmVzdWx0PihDYWxsPFRSZXN1bHQ+IGNhbGwpDQogICAgeyAgICAgICAgDQogICAgICAgIENvbnNvbGUuV3JpdGVMaW5lKCQic3RhcnQgQ2FsbGluZyB7Y2FsbC5NZW1iZXJOYW1lfSAgISIpOw0KICAgICAgICB2YXIgZGF0YSAgICA9Y2FsbC5Gb3J3YXJkKCk7DQogICAgICAgIENvbnNvbGUuV3JpdGVMaW5lKCQiZW5kIGNhbGxpbmcge2NhbGwuTWVtYmVyTmFtZX0gISIpOw0KICAgICAgICByZXR1cm4gZGF0YTsNCg0KICAgIH0NCn0=")]
using System;

#nullable enable
public interface IPerson
{
    int ID { get; set; }

    string? FirstName { get; set; }

    string? LastName { get; set; }

    string FullName();
    public class Adapter : IPerson
    {
        private readonly MatryoshkiDemo.Person _inner;
        public Adapter(MatryoshkiDemo.Person inner)
        {
            _inner = inner;
        }

        public int ID { get => _inner.ID; set => _inner.ID = value; }
        public string? FirstName { get => _inner.FirstName; set => _inner.FirstName = value; }
        public string? LastName { get => _inner.LastName; set => _inner.LastName = value; }

        public string FullName() => _inner.FullName();
    }
}
using System;
using MatryoshkiDemo;

#nullable enable
public class IPersonWithAddLog : MatryoshkiDemo.IPerson
{
    private readonly MatryoshkiDemo.IPerson _inner;
    public IPersonWithAddLog(MatryoshkiDemo.IPerson inner)
    {
        _inner = inner;
    }

    private static readonly string[] MethodParameterNamesForPropertyFirstName = new string[]
    {
    };
    public string? FirstName
    {
        get
        {
            Console.WriteLine($"Hello, {"FirstName"}!");
            return _inner.FirstName;
        }

        set
        {
            Console.WriteLine($"Hello, {"FirstName"}!");
            {
                Matryoshki.Abstractions.Nothing.FromPropertyAction(_inner, value, static (@innerΔΔΔ, @valueΔΔΔ) => @innerΔΔΔ.FirstName = @valueΔΔΔ);
                return;
            }
        }
    }

    private static readonly string[] MethodParameterNamesForPropertyID = new string[]
    {
    };
    public int ID
    {
        get
        {
            Console.WriteLine($"Hello, {"ID"}!");
            return _inner.ID;
        }

        set
        {
            Console.WriteLine($"Hello, {"ID"}!");
            {
                Matryoshki.Abstractions.Nothing.FromPropertyAction(_inner, value, static (@innerΔΔΔ, @valueΔΔΔ) => @innerΔΔΔ.ID = @valueΔΔΔ);
                return;
            }
        }
    }

    private static readonly string[] MethodParameterNamesForPropertyLastName = new string[]
    {
    };
    public string? LastName
    {
        get
        {
            Console.WriteLine($"Hello, {"LastName"}!");
            return _inner.LastName;
        }

        set
        {
            Console.WriteLine($"Hello, {"LastName"}!");
            {
                Matryoshki.Abstractions.Nothing.FromPropertyAction(_inner, value, static (@innerΔΔΔ, @valueΔΔΔ) => @innerΔΔΔ.LastName = @valueΔΔΔ);
                return;
            }
        }
    }

    private static readonly string[] MethodParameterNamesForMethodFullName = new string[]
    {
    };
    public string FullName()
    {
        Console.WriteLine($"Hello, {"FullName"}!");
        return _inner.FullName();
    }
}
using System;
using MatryoshkiDemo;

#nullable enable
public class PersonMatryoshka : MatryoshkiDemo.IPerson
{
    private readonly MatryoshkiDemo.IPerson _inner;
    public PersonMatryoshka(MatryoshkiDemo.IPerson inner)
    {
        _inner = inner;
    }

    private static readonly string[] MethodParameterNamesForPropertyFirstName = new string[]
    {
    };
    public string? FirstName
    {
        get
        {
            Console.WriteLine($"start Calling {"FirstName"}  !");
            var data = _inner.FirstName;
            Console.WriteLine($"end calling {"FirstName"} !");
            return data;
        }

        set
        {
            Console.WriteLine($"start Calling {"FirstName"}  !");
            var data = Matryoshki.Abstractions.Nothing.FromPropertyAction(_inner, value, static (@innerΔΔΔ, @valueΔΔΔ) => @innerΔΔΔ.FirstName = @valueΔΔΔ);
            Console.WriteLine($"end calling {"FirstName"} !");
            return;
        }
    }

    private static readonly string[] MethodParameterNamesForPropertyID = new string[]
    {
    };
    public int ID
    {
        get
        {
            Console.WriteLine($"start Calling {"ID"}  !");
            var data = _inner.ID;
            Console.WriteLine($"end calling {"ID"} !");
            return data;
        }

        set
        {
            Console.WriteLine($"start Calling {"ID"}  !");
            var data = Matryoshki.Abstractions.Nothing.FromPropertyAction(_inner, value, static (@innerΔΔΔ, @valueΔΔΔ) => @innerΔΔΔ.ID = @valueΔΔΔ);
            Console.WriteLine($"end calling {"ID"} !");
            return;
        }
    }

    private static readonly string[] MethodParameterNamesForPropertyLastName = new string[]
    {
    };
    public string? LastName
    {
        get
        {
            Console.WriteLine($"start Calling {"LastName"}  !");
            var data = _inner.LastName;
            Console.WriteLine($"end calling {"LastName"} !");
            return data;
        }

        set
        {
            Console.WriteLine($"start Calling {"LastName"}  !");
            var data = Matryoshki.Abstractions.Nothing.FromPropertyAction(_inner, value, static (@innerΔΔΔ, @valueΔΔΔ) => @innerΔΔΔ.LastName = @valueΔΔΔ);
            Console.WriteLine($"end calling {"LastName"} !");
            return;
        }
    }

    private static readonly string[] MethodParameterNamesForMethodFullName = new string[]
    {
    };
    public string FullName()
    {
        Console.WriteLine($"start Calling {"FullName"}  !");
        var data = _inner.FullName();
        Console.WriteLine($"end calling {"FullName"} !");
        return data;
    }
}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/Matryoshki

RSCG – Mediator

RSCG – Mediator
 
 

name Mediator
nuget https://www.nuget.org/packages/Mediator.SourceGenerator
https://www.nuget.org/packages/Mediator.Abstractions
link https://github.com/martinothamar/Mediator
author Martin Othamar

Gnerating mediator data without reflection

 

This is how you can use Mediator .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>
	<ItemGroup>
		<PackageReference Include="Mediator.Abstractions" Version="2.1.5" />
		<PackageReference Include="Mediator.SourceGenerator" Version="2.1.5" OutputItemType="Analyzer">
		  <PrivateAssets>all</PrivateAssets>
		  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
		</PackageReference>
		<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
		
	</ItemGroup>

</Project>


The code that you will use is


Console.WriteLine("Hello, World!");
var services = new ServiceCollection();
services.AddMediator();
services.AddSingleton(typeof(IPipelineBehavior<,>), typeof(GenericLoggerHandler<,>)); // This will run 1st

var serviceProvider = services.BuildServiceProvider();

var mediator = serviceProvider.GetRequiredService<IMediator>();
var id = Guid.NewGuid();
var request = new Ping(id);

var response = await mediator.Send(request);

Console.WriteLine("-----------------------------------");
Console.WriteLine("ID: " + id);
Console.WriteLine(request);
Console.WriteLine(response);




public sealed class GenericLoggerHandler<TMessage, TResponse> : IPipelineBehavior<TMessage, TResponse>
    where TMessage : IMessage
{
    public async ValueTask<TResponse> Handle(TMessage message, CancellationToken cancellationToken, MessageHandlerDelegate<TMessage, TResponse> next)    
    {
        Console.WriteLine("1) Running logger handler");
        try
        {
            var response = await next(message, cancellationToken);
            Console.WriteLine("5) No error!");
            return response;
        }
        catch (Exception ex)
        {
            Console.WriteLine("error:"+ex.Message);
            throw;
        }
    }

}



public sealed record Ping(Guid Id) : IRequest<Pong>;

public sealed record Pong(Guid Id);


public sealed class PingHandler : IRequestHandler<Ping, Pong>
{
    public ValueTask<Pong> Handle(Ping request, CancellationToken cancellationToken)
    {
        Console.WriteLine("4) Returning pong!");
        return new ValueTask<Pong>(new Pong(request.Id));
    }
}


 

The code that is generated is

// <auto-generated>
//     Generated by the Mediator source generator.
// </auto-generated>

#pragma warning disable CS8019 // Unused usings
#pragma warning disable CS8321 // Unused local function
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System.Linq;

using SD = global::Microsoft.Extensions.DependencyInjection.ServiceDescriptor;

namespace Microsoft.Extensions.DependencyInjection
{
    /// <summary>
    /// DI extensions for Mediator.
    /// </summary>
    [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "2.1.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.Diagnostics.DebuggerStepThroughAttribute]
    public static class MediatorDependencyInjectionExtensions
    {
        /// <summary>
        /// Adds the Mediator implementation and handlers of your application.
        /// </summary>
        public static IServiceCollection AddMediator(this IServiceCollection services)
        {
            return AddMediator(services, null);
        }

        internal sealed class Dummy { }

        /// <summary>
        /// Adds the Mediator implementation and handlers of your application, with specified options.
        /// </summary>
        public static IServiceCollection AddMediator(this IServiceCollection services, global::System.Action<global::Mediator.MediatorOptions> options)
        {
            var opts = new global::Mediator.MediatorOptions();
            if (options != null)
                options(opts);

            var configuredViaAttribute = false;
            if (opts.ServiceLifetime != global::Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton && !configuredViaAttribute)
            {
                var errMsg = "Invalid configuration detected for Mediator. ";
                errMsg += "Generated code for 'Singleton' lifetime, but got '" + opts.ServiceLifetime + "' lifetime from options. ";
                errMsg += "This means that the source generator hasn't seen the 'AddMediator' method call during compilation. ";
                errMsg += "Make sure that the 'AddMediator' method is called from the project that references the Mediator.SourceGenerator package.";
                throw new global::System.Exception(errMsg);
            }


            services.Add(new SD(typeof(global::Mediator.Mediator), typeof(global::Mediator.Mediator), global::Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton));
            services.TryAdd(new SD(typeof(global::Mediator.IMediator), sp => sp.GetRequiredService<global::Mediator.Mediator>(), global::Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton));
            services.TryAdd(new SD(typeof(global::Mediator.ISender), sp => sp.GetRequiredService<global::Mediator.Mediator>(), global::Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton));
            services.TryAdd(new SD(typeof(global::Mediator.IPublisher), sp => sp.GetRequiredService<global::Mediator.Mediator>(), global::Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton));

            services.TryAdd(new SD(typeof(global::PingHandler), typeof(global::PingHandler), global::Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton));
            services.Add(new SD(
                typeof(global::Mediator.RequestClassHandlerWrapper<global::Ping, global::Pong>),
                sp =>
                {
                    return new global::Mediator.RequestClassHandlerWrapper<global::Ping, global::Pong>(
                        sp.GetRequiredService<global::PingHandler>(),
                        sp.GetServices<global::Mediator.IPipelineBehavior<global::Ping, global::Pong>>()
                    );
                },
                global::Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton
            ));




            services.AddSingleton<Dummy>();

            return services;

        }
    }
}

namespace Mediator
{
    [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "2.1.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.Diagnostics.DebuggerStepThroughAttribute]
    internal sealed class RequestClassHandlerWrapper<TRequest, TResponse>
        where TRequest : class, global::Mediator.IRequest<TResponse>
    {
        private readonly global::Mediator.MessageHandlerDelegate<TRequest, TResponse> _rootHandler;

        public RequestClassHandlerWrapper(
            global::Mediator.IRequestHandler<TRequest, TResponse> concreteHandler,
            global::System.Collections.Generic.IEnumerable<global::Mediator.IPipelineBehavior<TRequest, TResponse>> pipelineBehaviours
        )
        {
            var handler = (global::Mediator.MessageHandlerDelegate<TRequest, TResponse>)concreteHandler.Handle;

            foreach (var pipeline in pipelineBehaviours.Reverse())
            {
                var handlerCopy = handler;
                var pipelineCopy = pipeline;
                handler = (TRequest message, System.Threading.CancellationToken cancellationToken) => pipelineCopy.Handle(message, cancellationToken, handlerCopy);
            }

            _rootHandler = handler;
        }

        public global::System.Threading.Tasks.ValueTask<TResponse> Handle(TRequest request, global::System.Threading.CancellationToken cancellationToken) =>
            _rootHandler(request, cancellationToken);
    }
    [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "2.1.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.Diagnostics.DebuggerStepThroughAttribute]
    internal sealed class RequestStructHandlerWrapper<TRequest, TResponse>
        where TRequest : struct, global::Mediator.IRequest<TResponse>
    {
        private readonly global::Mediator.MessageHandlerDelegate<TRequest, TResponse> _rootHandler;

        public RequestStructHandlerWrapper(
            global::Mediator.IRequestHandler<TRequest, TResponse> concreteHandler,
            global::System.Collections.Generic.IEnumerable<global::Mediator.IPipelineBehavior<TRequest, TResponse>> pipelineBehaviours
        )
        {
            var handler = (global::Mediator.MessageHandlerDelegate<TRequest, TResponse>)concreteHandler.Handle;

            foreach (var pipeline in pipelineBehaviours.Reverse())
            {
                var handlerCopy = handler;
                var pipelineCopy = pipeline;
                handler = (TRequest message, System.Threading.CancellationToken cancellationToken) => pipelineCopy.Handle(message, cancellationToken, handlerCopy);
            }

            _rootHandler = handler;
        }

        public global::System.Threading.Tasks.ValueTask<TResponse> Handle(TRequest request, global::System.Threading.CancellationToken cancellationToken) =>
            _rootHandler(request, cancellationToken);
    }
    [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "2.1.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.Diagnostics.DebuggerStepThroughAttribute]
    internal sealed class StreamRequestClassHandlerWrapper<TRequest, TResponse>
        where TRequest : class, global::Mediator.IStreamRequest<TResponse>
    {
        private readonly global::Mediator.StreamHandlerDelegate<TRequest, TResponse> _rootHandler;

        public StreamRequestClassHandlerWrapper(
            global::Mediator.IStreamRequestHandler<TRequest, TResponse> concreteHandler,
            global::System.Collections.Generic.IEnumerable<global::Mediator.IStreamPipelineBehavior<TRequest, TResponse>> pipelineBehaviours
        )
        {
            var handler = (global::Mediator.StreamHandlerDelegate<TRequest, TResponse>)concreteHandler.Handle;

            foreach (var pipeline in pipelineBehaviours.Reverse())
            {
                var handlerCopy = handler;
                var pipelineCopy = pipeline;
                handler = (TRequest message, System.Threading.CancellationToken cancellationToken) => pipelineCopy.Handle(message, cancellationToken, handlerCopy);
            }

            _rootHandler = handler;
        }

        public global::System.Collections.Generic.IAsyncEnumerable<TResponse> Handle(TRequest request, global::System.Threading.CancellationToken cancellationToken) =>
            _rootHandler(request, cancellationToken);
    }
    [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "2.1.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.Diagnostics.DebuggerStepThroughAttribute]
    internal sealed class StreamRequestStructHandlerWrapper<TRequest, TResponse>
        where TRequest : struct, global::Mediator.IStreamRequest<TResponse>
    {
        private readonly global::Mediator.StreamHandlerDelegate<TRequest, TResponse> _rootHandler;

        public StreamRequestStructHandlerWrapper(
            global::Mediator.IStreamRequestHandler<TRequest, TResponse> concreteHandler,
            global::System.Collections.Generic.IEnumerable<global::Mediator.IStreamPipelineBehavior<TRequest, TResponse>> pipelineBehaviours
        )
        {
            var handler = (global::Mediator.StreamHandlerDelegate<TRequest, TResponse>)concreteHandler.Handle;

            foreach (var pipeline in pipelineBehaviours.Reverse())
            {
                var handlerCopy = handler;
                var pipelineCopy = pipeline;
                handler = (TRequest message, System.Threading.CancellationToken cancellationToken) => pipelineCopy.Handle(message, cancellationToken, handlerCopy);
            }

            _rootHandler = handler;
        }

        public global::System.Collections.Generic.IAsyncEnumerable<TResponse> Handle(TRequest request, global::System.Threading.CancellationToken cancellationToken) =>
            _rootHandler(request, cancellationToken);
    }
    [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "2.1.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.Diagnostics.DebuggerStepThroughAttribute]
    internal sealed class CommandClassHandlerWrapper<TRequest, TResponse>
        where TRequest : class, global::Mediator.ICommand<TResponse>
    {
        private readonly global::Mediator.MessageHandlerDelegate<TRequest, TResponse> _rootHandler;

        public CommandClassHandlerWrapper(
            global::Mediator.ICommandHandler<TRequest, TResponse> concreteHandler,
            global::System.Collections.Generic.IEnumerable<global::Mediator.IPipelineBehavior<TRequest, TResponse>> pipelineBehaviours
        )
        {
            var handler = (global::Mediator.MessageHandlerDelegate<TRequest, TResponse>)concreteHandler.Handle;

            foreach (var pipeline in pipelineBehaviours.Reverse())
            {
                var handlerCopy = handler;
                var pipelineCopy = pipeline;
                handler = (TRequest message, System.Threading.CancellationToken cancellationToken) => pipelineCopy.Handle(message, cancellationToken, handlerCopy);
            }

            _rootHandler = handler;
        }

        public global::System.Threading.Tasks.ValueTask<TResponse> Handle(TRequest request, global::System.Threading.CancellationToken cancellationToken) =>
            _rootHandler(request, cancellationToken);
    }
    [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "2.1.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.Diagnostics.DebuggerStepThroughAttribute]
    internal sealed class CommandStructHandlerWrapper<TRequest, TResponse>
        where TRequest : struct, global::Mediator.ICommand<TResponse>
    {
        private readonly global::Mediator.MessageHandlerDelegate<TRequest, TResponse> _rootHandler;

        public CommandStructHandlerWrapper(
            global::Mediator.ICommandHandler<TRequest, TResponse> concreteHandler,
            global::System.Collections.Generic.IEnumerable<global::Mediator.IPipelineBehavior<TRequest, TResponse>> pipelineBehaviours
        )
        {
            var handler = (global::Mediator.MessageHandlerDelegate<TRequest, TResponse>)concreteHandler.Handle;

            foreach (var pipeline in pipelineBehaviours.Reverse())
            {
                var handlerCopy = handler;
                var pipelineCopy = pipeline;
                handler = (TRequest message, System.Threading.CancellationToken cancellationToken) => pipelineCopy.Handle(message, cancellationToken, handlerCopy);
            }

            _rootHandler = handler;
        }

        public global::System.Threading.Tasks.ValueTask<TResponse> Handle(TRequest request, global::System.Threading.CancellationToken cancellationToken) =>
            _rootHandler(request, cancellationToken);
    }
    [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "2.1.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.Diagnostics.DebuggerStepThroughAttribute]
    internal sealed class StreamCommandClassHandlerWrapper<TRequest, TResponse>
        where TRequest : class, global::Mediator.IStreamCommand<TResponse>
    {
        private readonly global::Mediator.StreamHandlerDelegate<TRequest, TResponse> _rootHandler;

        public StreamCommandClassHandlerWrapper(
            global::Mediator.IStreamCommandHandler<TRequest, TResponse> concreteHandler,
            global::System.Collections.Generic.IEnumerable<global::Mediator.IStreamPipelineBehavior<TRequest, TResponse>> pipelineBehaviours
        )
        {
            var handler = (global::Mediator.StreamHandlerDelegate<TRequest, TResponse>)concreteHandler.Handle;

            foreach (var pipeline in pipelineBehaviours.Reverse())
            {
                var handlerCopy = handler;
                var pipelineCopy = pipeline;
                handler = (TRequest message, System.Threading.CancellationToken cancellationToken) => pipelineCopy.Handle(message, cancellationToken, handlerCopy);
            }

            _rootHandler = handler;
        }

        public global::System.Collections.Generic.IAsyncEnumerable<TResponse> Handle(TRequest request, global::System.Threading.CancellationToken cancellationToken) =>
            _rootHandler(request, cancellationToken);
    }
    [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "2.1.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.Diagnostics.DebuggerStepThroughAttribute]
    internal sealed class StreamCommandStructHandlerWrapper<TRequest, TResponse>
        where TRequest : struct, global::Mediator.IStreamCommand<TResponse>
    {
        private readonly global::Mediator.StreamHandlerDelegate<TRequest, TResponse> _rootHandler;

        public StreamCommandStructHandlerWrapper(
            global::Mediator.IStreamCommandHandler<TRequest, TResponse> concreteHandler,
            global::System.Collections.Generic.IEnumerable<global::Mediator.IStreamPipelineBehavior<TRequest, TResponse>> pipelineBehaviours
        )
        {
            var handler = (global::Mediator.StreamHandlerDelegate<TRequest, TResponse>)concreteHandler.Handle;

            foreach (var pipeline in pipelineBehaviours.Reverse())
            {
                var handlerCopy = handler;
                var pipelineCopy = pipeline;
                handler = (TRequest message, System.Threading.CancellationToken cancellationToken) => pipelineCopy.Handle(message, cancellationToken, handlerCopy);
            }

            _rootHandler = handler;
        }

        public global::System.Collections.Generic.IAsyncEnumerable<TResponse> Handle(TRequest request, global::System.Threading.CancellationToken cancellationToken) =>
            _rootHandler(request, cancellationToken);
    }
    [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "2.1.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.Diagnostics.DebuggerStepThroughAttribute]
    internal sealed class QueryClassHandlerWrapper<TRequest, TResponse>
        where TRequest : class, global::Mediator.IQuery<TResponse>
    {
        private readonly global::Mediator.MessageHandlerDelegate<TRequest, TResponse> _rootHandler;

        public QueryClassHandlerWrapper(
            global::Mediator.IQueryHandler<TRequest, TResponse> concreteHandler,
            global::System.Collections.Generic.IEnumerable<global::Mediator.IPipelineBehavior<TRequest, TResponse>> pipelineBehaviours
        )
        {
            var handler = (global::Mediator.MessageHandlerDelegate<TRequest, TResponse>)concreteHandler.Handle;

            foreach (var pipeline in pipelineBehaviours.Reverse())
            {
                var handlerCopy = handler;
                var pipelineCopy = pipeline;
                handler = (TRequest message, System.Threading.CancellationToken cancellationToken) => pipelineCopy.Handle(message, cancellationToken, handlerCopy);
            }

            _rootHandler = handler;
        }

        public global::System.Threading.Tasks.ValueTask<TResponse> Handle(TRequest request, global::System.Threading.CancellationToken cancellationToken) =>
            _rootHandler(request, cancellationToken);
    }
    [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "2.1.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.Diagnostics.DebuggerStepThroughAttribute]
    internal sealed class QueryStructHandlerWrapper<TRequest, TResponse>
        where TRequest : struct, global::Mediator.IQuery<TResponse>
    {
        private readonly global::Mediator.MessageHandlerDelegate<TRequest, TResponse> _rootHandler;

        public QueryStructHandlerWrapper(
            global::Mediator.IQueryHandler<TRequest, TResponse> concreteHandler,
            global::System.Collections.Generic.IEnumerable<global::Mediator.IPipelineBehavior<TRequest, TResponse>> pipelineBehaviours
        )
        {
            var handler = (global::Mediator.MessageHandlerDelegate<TRequest, TResponse>)concreteHandler.Handle;

            foreach (var pipeline in pipelineBehaviours.Reverse())
            {
                var handlerCopy = handler;
                var pipelineCopy = pipeline;
                handler = (TRequest message, System.Threading.CancellationToken cancellationToken) => pipelineCopy.Handle(message, cancellationToken, handlerCopy);
            }

            _rootHandler = handler;
        }

        public global::System.Threading.Tasks.ValueTask<TResponse> Handle(TRequest request, global::System.Threading.CancellationToken cancellationToken) =>
            _rootHandler(request, cancellationToken);
    }
    [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "2.1.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.Diagnostics.DebuggerStepThroughAttribute]
    internal sealed class StreamQueryClassHandlerWrapper<TRequest, TResponse>
        where TRequest : class, global::Mediator.IStreamQuery<TResponse>
    {
        private readonly global::Mediator.StreamHandlerDelegate<TRequest, TResponse> _rootHandler;

        public StreamQueryClassHandlerWrapper(
            global::Mediator.IStreamQueryHandler<TRequest, TResponse> concreteHandler,
            global::System.Collections.Generic.IEnumerable<global::Mediator.IStreamPipelineBehavior<TRequest, TResponse>> pipelineBehaviours
        )
        {
            var handler = (global::Mediator.StreamHandlerDelegate<TRequest, TResponse>)concreteHandler.Handle;

            foreach (var pipeline in pipelineBehaviours.Reverse())
            {
                var handlerCopy = handler;
                var pipelineCopy = pipeline;
                handler = (TRequest message, System.Threading.CancellationToken cancellationToken) => pipelineCopy.Handle(message, cancellationToken, handlerCopy);
            }

            _rootHandler = handler;
        }

        public global::System.Collections.Generic.IAsyncEnumerable<TResponse> Handle(TRequest request, global::System.Threading.CancellationToken cancellationToken) =>
            _rootHandler(request, cancellationToken);
    }
    [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "2.1.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.Diagnostics.DebuggerStepThroughAttribute]
    internal sealed class StreamQueryStructHandlerWrapper<TRequest, TResponse>
        where TRequest : struct, global::Mediator.IStreamQuery<TResponse>
    {
        private readonly global::Mediator.StreamHandlerDelegate<TRequest, TResponse> _rootHandler;

        public StreamQueryStructHandlerWrapper(
            global::Mediator.IStreamQueryHandler<TRequest, TResponse> concreteHandler,
            global::System.Collections.Generic.IEnumerable<global::Mediator.IStreamPipelineBehavior<TRequest, TResponse>> pipelineBehaviours
        )
        {
            var handler = (global::Mediator.StreamHandlerDelegate<TRequest, TResponse>)concreteHandler.Handle;

            foreach (var pipeline in pipelineBehaviours.Reverse())
            {
                var handlerCopy = handler;
                var pipelineCopy = pipeline;
                handler = (TRequest message, System.Threading.CancellationToken cancellationToken) => pipelineCopy.Handle(message, cancellationToken, handlerCopy);
            }

            _rootHandler = handler;
        }

        public global::System.Collections.Generic.IAsyncEnumerable<TResponse> Handle(TRequest request, global::System.Threading.CancellationToken cancellationToken) =>
            _rootHandler(request, cancellationToken);
    }

    /// <summary>
    /// Generated code for Mediator implementation.
    /// This type is also registered as a DI service.
    /// Can be used directly for high performance scenarios.
    /// </summary>
    [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "2.1.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.Diagnostics.DebuggerStepThroughAttribute]
    public sealed partial class Mediator : global::Mediator.IMediator, global::Mediator.ISender, global::Mediator.IPublisher
    {
        private readonly global::System.IServiceProvider _sp;
        private FastLazyValue<DICache> _diCacheLazy;

        /// <summary>
        /// The lifetime of Mediator-related service registrations in DI container.
        /// </summary>
        public static global::Microsoft.Extensions.DependencyInjection.ServiceLifetime ServiceLifetime { get; } = global::Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton;

        private readonly global::System.Func<global::System.Collections.Generic.IEnumerable<object>, int> _getServicesLength;

        /// <summary>
        /// Constructor for DI, should not be used by consumer.
        /// </summary>
        public Mediator(global::System.IServiceProvider sp)
        {
            _sp = sp;
            _diCacheLazy = new FastLazyValue<DICache>(() => new DICache(_sp));

            global::System.Func<global::System.Collections.Generic.IEnumerable<object>, int> fastGetLength = s => ((object[])s).Length;
            global::System.Func<global::System.Collections.Generic.IEnumerable<object>, int> slowGetLength = s => s.Count();

            var dummy = sp.GetServices<global::Microsoft.Extensions.DependencyInjection.MediatorDependencyInjectionExtensions.Dummy>();
            _getServicesLength = dummy.GetType() == typeof(global::Microsoft.Extensions.DependencyInjection.MediatorDependencyInjectionExtensions.Dummy[])
                 ? fastGetLength : slowGetLength;
        }

        private struct FastLazyValue<T>
            where T : struct
        {
            private const long UNINIT = 0;
            private const long INITING = 1;
            private const long INITD = 2;
            
            

            private global::System.Func<T> _generator;
            private long _state;
            private T _value;

            public T Value
            {
                [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
                get
                {
                    if (_state != INITD)
                        return ValueSlow;

                    return _value;
                }
            }

            private T ValueSlow
            {
                [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
                get
                {
                    var prevState = global::System.Threading.Interlocked.CompareExchange(ref _state, INITING, UNINIT);
                    switch (prevState)
                    {
                        case INITD:
                            // Someone has already completed init
                            return _value;
                        case INITING:
                            // Wait for someone else to complete
                            var spinWait = default(global::System.Threading.SpinWait);
                            while (global::System.Threading.Interlocked.Read(ref _state) < INITD)
                                spinWait.SpinOnce();
                            return _value;
                        case UNINIT:
                            _value = _generator();
                            global::System.Threading.Interlocked.Exchange(ref _state, INITD);
                            return _value;
                    }

                    return _value;
                }
            }


            public FastLazyValue(global::System.Func<T> generator)
            {
                _generator = generator;
                _state = UNINIT;
                _value = default;
            }
        }

                private readonly struct DICache
        {
            private readonly global::System.IServiceProvider _sp;

            public readonly global::Mediator.RequestClassHandlerWrapper<global::Ping, global::Pong> Wrapper_For_Ping;

            public DICache(global::System.IServiceProvider sp)
            {
                _sp = sp;

                Wrapper_For_Ping = sp.GetRequiredService<global::Mediator.RequestClassHandlerWrapper<global::Ping, global::Pong>>();
            }
        }

        /// <summary>
        /// Send a message of type global::Ping.
        /// Throws <see cref="global::System.ArgumentNullException"/> if message is null.
        /// </summary>
        /// <param name="message">Incoming message</param>
        /// <param name="cancellationToken">Cancellation token</param>
        /// <returns>Awaitable task</returns>
        public global::System.Threading.Tasks.ValueTask<global::Pong> Send(
            global::Ping message,
            global::System.Threading.CancellationToken cancellationToken = default
        )
        {
            ThrowIfNull(message, nameof(message));
            
            return _diCacheLazy.Value.Wrapper_For_Ping.Handle(message, cancellationToken);
        }

        /// <summary>
        /// Send request.
        /// Throws <see cref="global::System.ArgumentNullException"/> if message is null.
        /// Throws <see cref="global::Mediator.MissingMessageHandlerException"/> if no handler is registered.
        /// </summary>
        /// <param name="request">Incoming request</param>
        /// <param name="cancellationToken">Cancellation token</param>
        /// <returns>Awaitable task</returns>
        public global::System.Threading.Tasks.ValueTask<TResponse> Send<TResponse>(
            global::Mediator.IRequest<TResponse> request,
            global::System.Threading.CancellationToken cancellationToken = default
        )
        {
            switch (request)
            {
                case global::Ping r:
                {
                    if(typeof(TResponse) == typeof(global::Pong))
                    {
                        var task = Send(r, cancellationToken);
                        return global::System.Runtime.CompilerServices.Unsafe.As<global::System.Threading.Tasks.ValueTask<global::Pong>, global::System.Threading.Tasks.ValueTask<TResponse>>(ref task);
                    }
                    else
                    {
                        return SendAsync(request, cancellationToken);
                    }
                }
                default:
                {
                    ThrowArgumentNullOrInvalidMessage(request, nameof(request));
                    return default;
                }
            }
            
        }

        /// <summary>
        /// Send request.
        /// Throws <see cref="global::System.ArgumentNullException"/> if message is null.
        /// Throws <see cref="global::Mediator.MissingMessageHandlerException"/> if no handler is registered.
        /// </summary>
        /// <param name="request">Incoming request</param>
        /// <param name="cancellationToken">Cancellation token</param>
        /// <returns>Awaitable task</returns>
        private async global::System.Threading.Tasks.ValueTask<TResponse> SendAsync<TResponse>(
            global::Mediator.IRequest<TResponse> request,
            global::System.Threading.CancellationToken cancellationToken = default
        )
        {
            switch (request)
            {
                case global::Ping r:
                {
                    var response = await Send(r, cancellationToken);
                    return global::System.Runtime.CompilerServices.Unsafe.As<global::Pong, TResponse>(ref response);
                }
                default:
                {
                    ThrowArgumentNullOrInvalidMessage(request, nameof(request));
                    return default;
                }
            }
            
        }

        /// <summary>
        /// Create stream for request.
        /// Throws <see cref="global::System.ArgumentNullException"/> if message is null.
        /// Throws <see cref="global::Mediator.MissingMessageHandlerException"/> if no handler is registered.
        /// </summary>
        /// <param name="request">Incoming message</param>
        /// <param name="cancellationToken">Cancellation token</param>
        /// <returns>Async enumerable</returns>
        public global::System.Collections.Generic.IAsyncEnumerable<TResponse> CreateStream<TResponse>(
            global::Mediator.IStreamRequest<TResponse> request,
            global::System.Threading.CancellationToken cancellationToken = default
        )
        {
            ThrowInvalidMessage(request);
            return default;
            
        }

        /// <summary>
        /// Send command.
        /// Throws <see cref="global::System.ArgumentNullException"/> if message is null.
        /// Throws <see cref="global::Mediator.MissingMessageHandlerException"/> if no handler is registered.
        /// </summary>
        /// <param name="command">Incoming command</param>
        /// <param name="cancellationToken">Cancellation token</param>
        /// <returns>Awaitable task</returns>
        public global::System.Threading.Tasks.ValueTask<TResponse> Send<TResponse>(
            global::Mediator.ICommand<TResponse> command,
            global::System.Threading.CancellationToken cancellationToken = default
        )
        {
            ThrowInvalidMessage(command);
            return default;
            
        }

        /// <summary>
        /// Send command.
        /// Throws <see cref="global::System.ArgumentNullException"/> if message is null.
        /// Throws <see cref="global::Mediator.MissingMessageHandlerException"/> if no handler is registered.
        /// </summary>
        /// <param name="command">Incoming command</param>
        /// <param name="cancellationToken">Cancellation token</param>
        /// <returns>Awaitable task</returns>
        private async global::System.Threading.Tasks.ValueTask<TResponse> SendAsync<TResponse>(
            global::Mediator.ICommand<TResponse> command,
            global::System.Threading.CancellationToken cancellationToken = default
        )
        {
            ThrowInvalidMessage(command);
            return default;
            
        }

        /// <summary>
        /// Create stream for command.
        /// Throws <see cref="global::System.ArgumentNullException"/> if message is null.
        /// Throws <see cref="global::Mediator.MissingMessageHandlerException"/> if no handler is registered.
        /// </summary>
        /// <param name="command">Incoming message</param>
        /// <param name="cancellationToken">Cancellation token</param>
        /// <returns>Async enumerable</returns>
        public global::System.Collections.Generic.IAsyncEnumerable<TResponse> CreateStream<TResponse>(
            global::Mediator.IStreamCommand<TResponse> command,
            global::System.Threading.CancellationToken cancellationToken = default
        )
        {
            ThrowInvalidMessage(command);
            return default;
            
        }

        /// <summary>
        /// Send query.
        /// Throws <see cref="global::System.ArgumentNullException"/> if message is null.
        /// Throws <see cref="global::Mediator.MissingMessageHandlerException"/> if no handler is registered.
        /// </summary>
        /// <param name="query">Incoming query</param>
        /// <param name="cancellationToken">Cancellation token</param>
        /// <returns>Awaitable task</returns>
        public global::System.Threading.Tasks.ValueTask<TResponse> Send<TResponse>(
            global::Mediator.IQuery<TResponse> query,
            global::System.Threading.CancellationToken cancellationToken = default
        )
        {
            ThrowInvalidMessage(query);
            return default;
            
        }

        /// <summary>
        /// Send query.
        /// Throws <see cref="global::System.ArgumentNullException"/> if message is null.
        /// Throws <see cref="global::Mediator.MissingMessageHandlerException"/> if no handler is registered.
        /// </summary>
        /// <param name="query">Incoming query</param>
        /// <param name="cancellationToken">Cancellation token</param>
        /// <returns>Awaitable task</returns>
        private async global::System.Threading.Tasks.ValueTask<TResponse> SendAsync<TResponse>(
            global::Mediator.IQuery<TResponse> query,
            global::System.Threading.CancellationToken cancellationToken = default
        )
        {
            ThrowInvalidMessage(query);
            return default;
            
        }

        /// <summary>
        /// Create stream for query.
        /// Throws <see cref="global::System.ArgumentNullException"/> if message is null.
        /// Throws <see cref="global::Mediator.MissingMessageHandlerException"/> if no handler is registered.
        /// </summary>
        /// <param name="query">Incoming message</param>
        /// <param name="cancellationToken">Cancellation token</param>
        /// <returns>Async enumerable</returns>
        public global::System.Collections.Generic.IAsyncEnumerable<TResponse> CreateStream<TResponse>(
            global::Mediator.IStreamQuery<TResponse> query,
            global::System.Threading.CancellationToken cancellationToken = default
        )
        {
            ThrowInvalidMessage(query);
            return default;
            
        }

        /// <summary>
        /// Send message.
        /// Throws <see cref="global::System.ArgumentNullException"/> if message is null.
        /// Throws <see cref="global::Mediator.MissingMessageHandlerException"/> if no handler is registered.
        /// </summary>
        /// <param name="message">Incoming message</param>
        /// <param name="cancellationToken">Cancellation token</param>
        /// <returns>Awaitable task</returns>
        public async global::System.Threading.Tasks.ValueTask<object> Send(
            object message,
            global::System.Threading.CancellationToken cancellationToken = default
        )
        {
            switch (message)
            {
                case global::Ping m: return await Send(m, cancellationToken);
                default:
                {
                    ThrowArgumentNullOrInvalidMessage(message as global::Mediator.IMessage, nameof(message));
                    return default;
                }
            }
            
        }

        /// <summary>
        /// Create stream.
        /// Throws <see cref="global::System.ArgumentNullException"/> if message is null.
        /// Throws <see cref="global::Mediator.MissingMessageHandlerException"/> if no handler is registered.
        /// </summary>
        /// <param name="message">Incoming message</param>
        /// <param name="cancellationToken">Cancellation token</param>
        /// <returns>Async enumerable</returns>
        public global::System.Collections.Generic.IAsyncEnumerable<object> CreateStream(
            object message,
            global::System.Threading.CancellationToken cancellationToken = default
        )
        {
            ThrowInvalidMessage(message as global::Mediator.IStreamMessage);
            return default;
            
        }

        /// <summary>
        /// Publish notification.
        /// Throws <see cref="global::System.ArgumentNullException"/> if message is null.
        /// </summary>
        /// <param name="notification">Incoming notification</param>
        /// <param name="cancellationToken">Cancellation token</param>
        /// <returns>Awaitable task</returns>
        public global::System.Threading.Tasks.ValueTask Publish(
            object notification,
            global::System.Threading.CancellationToken cancellationToken = default
        )
        {
            return default;
            
        }


        /// <summary>
        /// Publish notification.
        /// Throws <see cref="global::System.ArgumentNullException"/> if message is null.
        /// </summary>
        /// <param name="notification">Incoming notification</param>
        /// <param name="cancellationToken">Cancellation token</param>
        /// <returns>Awaitable task</returns>
        public global::System.Threading.Tasks.ValueTask Publish<TNotification>(
            TNotification notification,
            global::System.Threading.CancellationToken cancellationToken = default
        )
            where TNotification : global::Mediator.INotification
        {
            return default;
            
        }

        [global::System.Diagnostics.CodeAnalysis.DoesNotReturn]
        private static void ThrowInvalidMessage(object msg) =>
            throw new global::Mediator.MissingMessageHandlerException(msg);

        [global::System.Diagnostics.CodeAnalysis.DoesNotReturn]
        private static void ThrowArgumentNull(string paramName) =>
            throw new global::System.ArgumentNullException(paramName);

        private static void ThrowIfNull<T>(T argument, string paramName)
        {
            if (argument is null)
            {
                ThrowArgumentNull(paramName);
            }
        }

        [global::System.Diagnostics.CodeAnalysis.DoesNotReturn]
        private static void ThrowArgumentNullOrInvalidMessage(object msg, string paramName)
        {
            if (msg is null)
            {
                ThrowArgumentNull(paramName);
            }
            else
            {
                ThrowInvalidMessage(msg);
            }
        }

        [global::System.Diagnostics.CodeAnalysis.DoesNotReturn]
        private static void ThrowAggregateException(global::System.Collections.Generic.List<global::System.Exception> exceptions) =>
            throw new global::System.AggregateException(exceptions);

        private static void MaybeThrowAggregateException(global::System.Collections.Generic.List<global::System.Exception> exceptions)
        {
            if (exceptions != null)
            {
                ThrowAggregateException(exceptions);
            }
        }
    }
}

// <auto-generated>
//     Generated by the Mediator source generator.
// </auto-generated>

namespace Mediator
{
    /// <summary>
    /// Provide options for the Mediator source generator.
    /// </summary>
    [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "2.1.0.0")]
    public sealed class MediatorOptions
    {
        /// <summary>
        /// The namespace in which the Mediator implementation is generated.
        /// </summary>
        public string Namespace { get; set; } = "Mediator";

        /// <summary>
        /// The default lifetime of the services registered in the DI container by
        /// the Mediator source generator.
        /// Singleton by default.
        /// </summary>
        public global::Microsoft.Extensions.DependencyInjection.ServiceLifetime ServiceLifetime { get; set; } =
            global::Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton;
    }
}

// <auto-generated>
//     Generated by the Mediator source generator.
// </auto-generated>

namespace Mediator
{
    /// <summary>
    /// Provide options for the Mediator source generator.
    /// </summary>
    [global::System.AttributeUsage(global::System.AttributeTargets.Assembly, AllowMultiple = false)]
    [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "2.1.0.0")]
    public sealed class MediatorOptionsAttribute : global::System.Attribute
    {
        /// <summary>
        /// The namespace in which the Mediator implementation is generated.
        /// </summary>
        public string Namespace { get; set; } = "Mediator";

        /// <summary>
        /// The default lifetime of the services registered in the DI container by
        /// the Mediator source generator.
        /// Singleton by default.
        /// </summary>
        public global::Microsoft.Extensions.DependencyInjection.ServiceLifetime ServiceLifetime { get; set; } =
            global::Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton;
    }
}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/Mediator

RSCG – MorrisMoxy

RSCG – MorrisMoxy
 
 

name MorrisMoxy
nuget https://www.nuget.org/packages/Morris.Moxy/
link https://github.com/mrpmorris/Morris.Moxy
author Peter Morris

Generate C# code for classes from template using attributes

 

This is how you can use MorrisMoxy .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>


  <ItemGroup>
    <None Remove="mixin\IDName.mixin" />
  </ItemGroup>

  <ItemGroup>
    <AdditionalFiles Include="mixin\IDName.mixin" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Morris.Moxy" Version="1.5.0" OutputItemType="Analyzer" ReferenceOutputAssembly="false"  />
  </ItemGroup>

</Project>


The code that you will use is


// See https://aka.ms/new-console-template for more information
using MorrisMoxyDemo;
var e = new Employee();
e.Name = "Andrei";
var d = new Department();
d.Name = "IT";
Console.WriteLine(e.ID);




namespace MorrisMoxyDemo;
[IDName]
partial class Employee
{
}




namespace MorrisMoxyDemo;
[IDName]
partial class Department
{
}


 

The code that is generated is

// Generated from mixin\IDName.mixin at 2023-07-31 19:27:42 UTC
namespace MorrisMoxyDemo
{
    
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = true)]
    internal class IDNameAttribute : Attribute
    {
    }
}


// Generated at 2023-07-31 19:27:42 UTC
namespace MorrisMoxyDemo
{
  partial class Department
  {
    public string Name {get;set;}
    public long  ID { get; set; }
  }
}

// Generated at 2023-07-31 19:27:42 UTC
namespace MorrisMoxyDemo
{
  partial class Employee
  {
    public string Name {get;set;}
    public long  ID { get; set; }
  }
}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/MorrisMoxy

RSCG – Refit

RSCG – Refit
 
 

name Refit
nuget https://www.nuget.org/packages/Refit/
link https://github.com/reactiveui/refit
author ReactiveUI

Generates code for retrieving data from HTTP API

 

This is how you can use Refit .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Refit" Version="7.0.0" />
  </ItemGroup>
	 <PropertyGroup>
        <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
        <CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
    </PropertyGroup>

</Project>


The code that you will use is


Console.WriteLine("Hello, World!");
var gitHubApi = RestService.For<IFindPosts>("https://jsonplaceholder.typicode.com/");
var data = await gitHubApi.GetPost(1);
Console.WriteLine(data.Title);


namespace RefitDemo;

public record Post
{
    public int Id { get; set; }
    public string Title { get; set; }
}


namespace RefitDemo;
public interface IFindPosts
{
    [Get("/posts/{nr}")]
    Task<Post> GetPost(long nr);
}


 

The code that is generated is


#pragma warning disable
namespace Refit.Implementation
{

    /// <inheritdoc />
    [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
    [global::System.Diagnostics.DebuggerNonUserCode]
    [global::RefitDemoRefitInternalGenerated.PreserveAttribute]
    [global::System.Reflection.Obfuscation(Exclude=true)]
    [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
    internal static partial class Generated
    {
    }
}
#pragma warning restore

#nullable enable
#pragma warning disable
namespace Refit.Implementation
{

    partial class Generated
    {

    /// <inheritdoc />
    [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
    [global::System.Diagnostics.DebuggerNonUserCode]
    [global::RefitDemoRefitInternalGenerated.PreserveAttribute]
    [global::System.Reflection.Obfuscation(Exclude=true)]
    [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
    partial class RefitDemoIFindPosts
        : global::RefitDemo.IFindPosts

    {
        /// <inheritdoc />
        public global::System.Net.Http.HttpClient Client { get; }
        readonly global::Refit.IRequestBuilder requestBuilder;

        /// <inheritdoc />
        public RefitDemoIFindPosts(global::System.Net.Http.HttpClient client, global::Refit.IRequestBuilder requestBuilder)
        {
            Client = client;
            this.requestBuilder = requestBuilder;
        }
    


        /// <inheritdoc />
        public global::System.Threading.Tasks.Task<global::RefitDemo.Post> GetPost(long @nr) 
        {
            var ______arguments = new object[] { @nr };
            var ______func = requestBuilder.BuildRestResultFuncForMethod("GetPost", new global::System.Type[] { typeof(long) } );
            return (global::System.Threading.Tasks.Task<global::RefitDemo.Post>)______func(this.Client, ______arguments);
        }

        /// <inheritdoc />
        global::System.Threading.Tasks.Task<global::RefitDemo.Post> global::RefitDemo.IFindPosts.GetPost(long @nr) 
        {
            var ______arguments = new object[] { @nr };
            var ______func = requestBuilder.BuildRestResultFuncForMethod("GetPost", new global::System.Type[] { typeof(long) } );
            return (global::System.Threading.Tasks.Task<global::RefitDemo.Post>)______func(this.Client, ______arguments);
        }
    }
    }
}

#pragma warning restore


#pragma warning disable
namespace RefitDemoRefitInternalGenerated
{
    [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
    [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
    [global::System.AttributeUsage (global::System.AttributeTargets.Class | global::System.AttributeTargets.Struct | global::System.AttributeTargets.Enum | global::System.AttributeTargets.Constructor | global::System.AttributeTargets.Method | global::System.AttributeTargets.Property | global::System.AttributeTargets.Field | global::System.AttributeTargets.Event | global::System.AttributeTargets.Interface | global::System.AttributeTargets.Delegate)]
    sealed class PreserveAttribute : global::System.Attribute
    {
        //
        // Fields
        //
        public bool AllMembers;

        public bool Conditional;
    }
}
#pragma warning restore

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/Refit

RSCG – Gedaq

RSCG – Gedaq
 
 

name Gedaq
nuget https://www.nuget.org/packages/Gedaq/
link https://github.com/SoftStoneDevelop/Gedaq
author Vyacheslav Brevnov

Generating code from attribute query

 

This is how you can use Gedaq .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">

	<PropertyGroup>
		<OutputType>Exe</OutputType>
		<TargetFramework>net7.0</TargetFramework>
		<ImplicitUsings>enable</ImplicitUsings>
		<Nullable>enable</Nullable>
	</PropertyGroup>
	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>
	<ItemGroup>
	  <PackageReference Include="Gedaq" Version="1.4.10" OutputItemType="Analyzer" ReferenceOutputAssembly="True" />
	  <PackageReference Include="Gedaq.DbConnection" Version="1.2.4" />
	  <PackageReference Include="Gedaq.SqlClient" Version="0.2.4" />
	</ItemGroup>
</Project>


The code that you will use is


using GedaqDemoConsole;

var data = new GetData();

var list=data.GetPersons();





namespace GedaqDemoConsole;

public class Person
{
    public int Id { get; set; }

    public string? FirstName { get; set; }

    public string? MiddleName { get; set; }

    public string? LastName { get; set; }

}




using Microsoft.Data.SqlClient;

namespace GedaqDemoConsole;

public partial class GetData
{
    string connectionString = "asdasd";

    [Gedaq.SqlClient.Attributes.Query(
        @"
SELECT 
    p.id,
    p.firstname,
FROM person p
"
    ,
        "GetPersons",
        typeof(Person)
        ),
        ]
    public Person[] GetPersons()
    {
        using (var sql = new SqlConnection(connectionString))
        {
            sql.Open();

            return GetPersons(sql, 48).ToArray();//using generated method
        }
    }
}


 

The code that is generated is


using System;
using System.Data;
using System.Data.Common;
using Microsoft.Data.SqlClient;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;

namespace GedaqDemoConsole
{
    public  partial class GetData
    {
        
        public  IEnumerable<GedaqDemoConsole.Person> GetPersons(

            SqlConnection connection
,
            int? timeout = null
,
            SqlTransaction transaction = null

        )
        {

            bool needClose = connection.State == ConnectionState.Closed;
            if(needClose)
            {
                connection.Open();
            }

            SqlCommand command = null;
            SqlDataReader reader = null;
            try
            {
                command =

                CreateGetPersonsCommand(connection

                , false)

                ;
                SetGetPersonsParametrs(
                    command
,
                    timeout
,
                    transaction

                    );

                reader = command.ExecuteReader();

                while (reader.Read())
                {

                    var item = new GedaqDemoConsole.Person();

                        if(!reader.IsDBNull(0))
                        {

                            if(item == null)
                            {
                                 item = new GedaqDemoConsole.Person();
                            }

                            item.Id = reader.GetFieldValue<System.Int32>(0);

                        }

                        if(!reader.IsDBNull(1))
                        {

                            if(item == null)
                            {
                                 item = new GedaqDemoConsole.Person();
                            }

                            item.FirstName = reader.GetFieldValue<System.String>(1);

                        }
 
                    yield return item;

                }

                while (reader.NextResult())
                {
                }

                reader.Dispose();
                reader = null;
            }
            finally
            {
                if (reader != null)
                {
                    if (!reader.IsClosed)
                    {
                        try 
                        {
                            command.Cancel();
                        }
                        catch { /* ignore */ }
                    }
                
                    reader.Dispose();
                }

                if (needClose)
                {
                    connection.Close();
                }

                if(command != null)
                {
                    command.Parameters.Clear();
                    command.Dispose();
                }
            }

        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public  SqlCommand CreateGetPersonsCommand(
            SqlConnection connection
,
            bool prepare = false

        )
        {
            var command = connection.CreateCommand();

            command.CommandText = @"

SELECT 
    p.id,
    p.firstname,
FROM person p

";

            if(prepare)
            {
                try
                {
                    command.Prepare();
                }
                catch
                {
                    command.Dispose();
                    throw;
                }
            }

            return command;
        }

        public  IEnumerable<GedaqDemoConsole.Person> ExecuteGetPersonsCommand(SqlCommand command)
        {

            SqlDataReader reader = null;
            try
            {

                reader = command.ExecuteReader();
                while (reader.Read())
                {

                    var item = new GedaqDemoConsole.Person();

                        if(!reader.IsDBNull(0))
                        {

                            if(item == null)
                            {
                                 item = new GedaqDemoConsole.Person();
                            }

                            item.Id = reader.GetFieldValue<System.Int32>(0);

                        }

                        if(!reader.IsDBNull(1))
                        {

                            if(item == null)
                            {
                                 item = new GedaqDemoConsole.Person();
                            }

                            item.FirstName = reader.GetFieldValue<System.String>(1);

                        }
 
                    yield return item;

                }

                while (reader.NextResult())
                {
                }
                reader.Dispose();
                reader = null;
            }
            finally
            {
                if (reader != null)
                {
                    if (!reader.IsClosed)
                    {
                        try 
                        {
                            command.Cancel();
                        }
                        catch { /* ignore */ }
                    }
                
                    reader.Dispose();
                }
            }

        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public  void SetGetPersonsParametrs(
            SqlCommand command
,
            int? timeout = null

            ,
            SqlTransaction transaction = null

        )
        {

            if(timeout.HasValue)
            {
                command.CommandTimeout = timeout.Value;
            }

            if(transaction != null)
            {
                command.Transaction = transaction;
            }

        }

    }
}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/Gedaq

RSCG – Lombok.NET

RSCG – Lombok.NET
 
 

name Lombok.NET
nuget https://www.nuget.org/packages/Lombok.NET/
link https://github.com/CollinAlpert/Lombok.NET
author Colin Alpert

Generating toString from props/fields. Other demos on site

 

This is how you can use Lombok.NET .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Lombok.NET" Version="2.1.2" />
  </ItemGroup>
	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>
</Project>


The code that you will use is


// See https://aka.ms/new-console-template for more information
using Lombok.NETDemo;

Console.WriteLine("Hello, World!");
Person p = new();
p.FirstName = "Andrei";
p.LastName="Ignat";
Console.WriteLine(p.ToString());



using Lombok.NET;

namespace Lombok.NETDemo;

[ToString(AccessTypes=AccessTypes.Public,  MemberType=MemberType.Property)]
//[AllArgsConstructor(AccessTypes=AccessTypes.Public,MemberType = MemberType.Property)]
public partial class Person
{
    public Person()
    {
        
    }
    public string? FirstName{ get; set; }
    public string? LastName { get; set; }
    public string FullName
    {
        get
        {
            return FirstName + " " + LastName;
        }
    }
}


 

The code that is generated is

// <auto-generated/>
namespace Lombok.NETDemo;
#nullable enable
public partial class Person
{
    public override string ToString()
    {
        return $"Person: FirstName={FirstName}; LastName={LastName}; FullName={FullName}";
    }
}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/Lombok.NET

RSCG – EmbedResourceCSharp

RSCG – EmbedResourceCSharp
 
 

name EmbedResourceCSharp
nuget https://www.nuget.org/packages/EmbedResourceCSharp/
link https://github.com/pCYSl5EDgo/EmbeddingResourceCSharp
author pCYSl5EDgo

reading embedded resources fast

 

This is how you can use EmbedResourceCSharp .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>
  <ItemGroup>
    <None Remove="createDB.txt" />
  </ItemGroup>

  <ItemGroup>
    <EmbeddedResource Include="createDB.txt" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="EmbedResourceCSharp" Version="1.1.3">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>

</Project>


The code that you will use is


// See https://aka.ms/new-console-template for more information
using System;
using System.Text;

var value = EmbeddingResourceCSharpDemo.MyResource.GetContentOfCreate();
StringBuilder sb = new ();
foreach (byte b in value)
{
    sb.Append((char)b);
}
;
//EncodingExtensions.GetString(Encoding.UTF8, value);
Console.WriteLine(sb.ToString());




namespace EmbeddingResourceCSharpDemo;

public partial class MyResource
{
    [EmbedResourceCSharp.FileEmbed("createDB.txt")]
    public static partial System.ReadOnlySpan<byte> GetContentOfCreate();
}



create database Andrei;
GO;
use Andrei;

 

The code that is generated is

namespace EmbedResourceCSharp
{
    internal enum PathSeparator
    {
        AsIs,
        Slash,
        BackSlash,
    }

    [global::System.AttributeUsage(global::System.AttributeTargets.Method, AllowMultiple = false)]
    internal sealed class FileEmbedAttribute : global::System.Attribute
    {
        public string Path { get; }

        public FileEmbedAttribute(string path)
        {
            Path = path;
        }
    }

    [global::System.AttributeUsage(global::System.AttributeTargets.Method, AllowMultiple = false)]
    internal sealed class FolderEmbedAttribute : global::System.Attribute
    {
        public string Path { get; private set; }
        public string Filter { get; private set; }
        public global::System.IO.SearchOption Option { get; private set; }
        public PathSeparator Separator { get; private set; }

        public FolderEmbedAttribute(string path, string filter = "*", global::System.IO.SearchOption option = global::System.IO.SearchOption.AllDirectories, PathSeparator separator = PathSeparator.Slash)
        {
            Path = path;
            Filter = filter;
            Option = option;
            Separator = separator;
        }
    }
}

namespace EmbeddingResourceCSharpDemo
{
    public partial class MyResource
    {
        public static partial global::System.ReadOnlySpan<byte> GetContentOfCreate()
        {
            return new byte[] { 239, 187, 191, 99, 114, 101, 97, 116, 101, 32, 100, 97, 116, 97, 98, 97, 115, 101, 32, 65, 110, 100, 114, 101, 105, 59, 13, 10, 71, 79, 59, 13, 10, 117, 115, 101, 32, 65, 110, 100, 114, 101, 105, 59 };
        }
    }
}


Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/EmbedResourceCSharp

Andrei Ignat weekly software news(mostly .NET)

* indicates required

Please select all the ways you would like to hear from me:

You can unsubscribe at any time by clicking the link in the footer of our emails. For information about our privacy practices, please visit our website.

We use Mailchimp as our marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp's privacy practices here.