RSCG – RSCG_CompositeProvider

RSCG – RSCG_CompositeProvider
 
 

name RSCG_CompositeProvider
nuget https://www.nuget.org/packages/RSCG_CompositeProvider/
https://www.nuget.org/packages/RSCG_CompositeProvider_common/
link https://github.com/ignatandrei/RSCG_CompositeProvider
author Ignat Andrei

Generate composite class from interface, using multiple sources

 

This is how you can use RSCG_CompositeProvider .

The code that you start with is

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
<Project Sdk="Microsoft.NET.Sdk">
 
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
      <IsPackable>false</IsPackable>
  <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
 
   
  <ItemGroup>
    <PackageReference Include="RSCG_CompositeProvider" Version="2025.218.2100" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
    <PackageReference Include="RSCG_CompositeProvider_Common" Version="2025.218.2100" />
  </ItemGroup>
  <PropertyGroup>
        <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
        <CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
    </PropertyGroup>
</Project>

The code that you will use is

1
2
3
4
5
6
7
8
using CP_Console;
 
IDataValue provider = new DataValue_CP(new DataFromHttp(), new DataFromMemory());
var result = await provider.KeyFromValue("test", false);
Console.WriteLine(result);
DataValue_CP realClass = (DataValue_CP)provider ;
var lastInterface = realClass.lastUsedInterface ?? -1;
Console.WriteLine("value was obtained from " + realClass.Get(lastInterface).Name);
01
02
03
04
05
06
07
08
09
10
11
using RSCG_CompositeProvider_Common;
 
namespace CP_Console;
[CompositeProvider]
public interface IDataValue
{
    public string Name { get; set; }
    public Task<string> KeyFromValue(string key, bool defaultValue);
 
     
}
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
namespace CP_Console;
 
class DataFromHttp : IDataValue
{
    public string Name { get { return "DataFromHttp"; } set { } }
 
    public async Task<string> KeyFromValue(string key, bool defaultValue)
    {
        var http=new HttpClient();
        var result = await http.GetStringAsync("https://www."+ Guid.NewGuid().ToString()+".com/" + key);
        return result;
    }
}
 
 
class DataFromMemory : IDataValue
{
    public string Name { get { return "DataFromMemory"; } set { } }
 
    public async Task<string> KeyFromValue(string key, bool defaultValue)
    {
        await Task.Delay(1000);
        return $"this is value for {key} from memory";
    }
}

 

The code that is generated is

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
// <auto-generated/>
namespace Generated.CP_Console
{
    public static class TheAssemblyInfo
    {
         
        public static readonly System.DateTime DateGeneratedUTC ;
        public const string AssemblyName = "CP_Console";
        public const string GeneratedNameNice = "Bertrand Russell is feeling good-natured in Rothera";
        public const string GeneratedNameSmall = "good-natured-Bertrand Russell";
        public const string GeneratedName = "good-natured-Bertrand Russell-Rothera";
        static TheAssemblyInfo(){
            DateGeneratedUTC = System.DateTime.ParseExact("2025-02-24 14:32:51", "yyyy-MM-dd HH:mm:ss", null);
        }
    }
}
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// <auto-generated>
    //     This code was generated by a tool :RSCG_CompositeProvider
    //     Runtime Version: Herta Müller is feeling amiable in George Town
    //     DateOfTool : 2025-02-18 17:23:31
    //     Changes to this file may cause incorrect behavior and will be lost if
    //     the code is regenerated.
    //</auto-generated>
//------------------------------------------------------------------------------
/// <summary>
    /// This static partial class is a composite provider of IDataValue objects.
    ///</summary>
 
#nullable enable
#pragma warning disable CS8603
#pragma warning disable CS8625
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.CodeDom.Compiler.GeneratedCode("GeneratorName","2025.10218.11723.131")]
[System.Diagnostics.DebuggerDisplay("Name = {Name} ")]
public partial class DataValue_CP : global::CP_Console.IDataValue
{
public int? lastUsedInterface ;
 
private global::CP_Console.IDataValue[] values;
public DataValue_CP(params global::CP_Console.IDataValue[] values){
this.values=values;
}
public int Count{
get{
return values.Length;
}
}
public global::CP_Console.IDataValue Get(int nr){
    return values[nr];
}
 
 
 
 
        public string Name { get
        {
        lastUsedInterface = null;
        foreach(var item in values){
        lastUsedInterface =(lastUsedInterface ??-1)+1;
        if(item == null)continue;
        try{
        return item.Name;
        }
        catch(Exception ){
        //try with the next one
        }
        }
        throw new System.Collections.Generic.KeyNotFoundException();
        }
        set
        {
        foreach(var item in values){
        if(item == null)continue;
        try{
        item.Name = value;
        }
        catch(Exception ){
        //try with the next one
        }
        }
        }
 
        }
     
            public virtual  async  System.Threading.Tasks.Task<string> KeyFromValue(string key, bool defaultValue) {
                lastUsedInterface =null;
                foreach(var item in values){
                    lastUsedInterface =(lastUsedInterface ??-1)+1;
                    if(item == null)continue;
                    try{
                        var data=   await  item.KeyFromValue(key, defaultValue) ;
                        return data;
                    }
                    catch(Exception ){
                        //try with the next one
                    }
                }
                throw new System.Collections.Generic.KeyNotFoundException();
            }
         
}

Code and pdf at

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