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