Skip to main content

RSCG_CompositeProvider by Ignat Andrei

Nuget / site data

NugetNuget GitHub last commit GitHub Repo stars

Details

Info

Original Readme

note

RSCG_CompositeProvider

Composite provider from interface . Given multiple implementation of an interface , return data from each / one

Usage

Add the nuget package to your project

dotnet add package RSCG_CompositeProvider
dotnet add package RSCG_CompositeProviderCommon

or put in your csproj file

  <ItemGroup>
<PackageReference Include="RSCG_CompositeProvider" Version="2025.218.2100" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<PackageReference Include="RSCG_CompositeProvider_Common" Version="2025.218.2100" />
</ItemGroup>

Then if you have an interface like this

public interface IDataFrom
{
string Name { get; }
Task<string> KeyFromValue(string value, bool isKey);
}

and multiple implementation of the interface like this

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";
}
}

then you can call the composite provider to get data from all the implementation of the interface like this


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);

About

note

Generate composite class from interface, using multiple sources

How to use

Example ( source csproj, source files )

This is the CSharp Project that references RSCG_CompositeProvider

<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>

Generated Files

Those are taken from $(BaseIntermediateOutputPath)\GX

// <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();
}

}

Usefull

Download Example (.NET C# )

Share RSCG_CompositeProvider

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

In the same category (Interface) - 11 other generators

Biwen.AutoClassGen

CopyCat

Farskeptic.AutoCompose

MakeInterface.Generator

Matryoshki

Minerals.AutoInterfaces

NetAutomaticInterface

ProxyGen

Roozie.AutoInterface

rscg_Interface_to_null_object

RSCG_Static