RSCG-Composite Provider – part 2 -execution

Imagine this: For every interface IA you create:

  1. Your composite provider implements IA seamlessly
  2. In the constructor, pass an array of IA providers
  3. Each method returns the first successful value from your array of providers
  4. For even better performance, use a boolean flag to optimize by reusing previous successes

The RSCG_CompositeProvider package doesnΓÇÖt just solve the obvious issuesΓÇöit handles the tricky ones too:

  • Exception Handling: What if one of your providers throws an error? No worries, it moves on to the next provider without skipping a beat.
  • Asynchronous Methods: Supports `Task` returns so you can handle async operations with ease.
  • Async Enumerables: Easily works with `IAsyncEnumerable` for streaming data in chunks.

I will let you think how to solve this – you can find my solution at https://github.com/ignatandrei/RSCG_CompositeProvider

And the best news? It’s already been tested: with over 20 tests covering every edge case, you can trust this library to handle your toughest challenges.

Get started with RSCG-Composite Provider, available via NuGet: http://nuget.org/packages/RSCG_CompositeProvider.

RSCG-Composite Provider – part 1 -idea

API Outage solved with local data

The problem that I try to solve is : How an UI can have data to show , even if the API from where it gathers data does not work ?

Imagine having a robust system that adaptively switches between retrieving data from multiple sources – including internal APIs, memory-based storage, or even JSON files near the UI. This flexibility makes all the difference when working with complex systems that may suddenly lose their external connectivity.

This could be solved with interfaces and a composite provider ( see http://msprogrammer.serviciipeweb.ro/2025/03/10/pattern-compositeprovider/ )

How It Works:

  • You have an interface defining how your UI fetches data.Easy peasy!
  • One implementation pulls data from the trusty API, ideally when it’s up and running smoothly.
  • Another implementation acts as a backup hero, pulling data from a local JSON file or even hard-coded values.

And the best part? The composite provider handles switching between these sources seamlessly. No more coding headaches – it just works!

Making It Even Easier: Roslyn Code Generator to the Rescue

Tired of writing boilerplate code for this pattern every time? Please give a chance to a new Roslyn Code Generator (https://www.nuget.org/packages/RSCG_CompositeProvider). It automatically generates the composite providers you need, cutting down on repetitive work and letting you focus on what really matters – building awesome apps!

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

RSCG – DependencyModules.SourceGenerator

RSCG – DependencyModules.SourceGenerator
 
 

name DependencyModules.SourceGenerator
nuget https://www.nuget.org/packages/DependencyModules.SourceGenerator/
https://www.nuget.org/packages/DependencyModules.Runtime/
link https://github.com/ipjohnson/DependencyModules
author Ian Johnson

Generating service dependencies from attributes.

Also,by the author, a more advanced example you will find in the DemoWithTest.zip inside the zip file

 

This is how you can use DependencyModules.SourceGenerator .

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
<Project Sdk="Microsoft.NET.Sdk">
 
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
    <PropertyGroup>
        <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
        <CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
    </PropertyGroup>
    <ItemGroup>
      <PackageReference Include="DependencyModules.Runtime" Version="1.0.0-RC9074" />
      <PackageReference Include="DependencyModules.SourceGenerator" Version="1.0.0-RC9074" />
     <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.2" />
 
    </ItemGroup>
</Project>

The code that you will use is

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
using DependencyModules.Runtime;
using InjectDemo;
using Microsoft.Extensions.DependencyInjection;
 
 
var serviceCollection = new ServiceCollection();
 
serviceCollection.AddModule<MyModule>();
 
var provider = serviceCollection.BuildServiceProvider();
 
var service = provider.GetService<Database>();
 
if(service == null)
    throw new Exception("Service not found");
else
    service.Open();
1
2
3
4
5
6
7
using DependencyModules.Runtime.Attributes;
 
[DependencyModule]
public partial class MyModule
{
 
}
1
2
3
4
5
6
7
namespace InjectDemo
{
    internal interface IDatabase
    {
        public void Open();
    }
}
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
using DependencyModules.Runtime.Attributes;
 
namespace InjectDemo;
[SingletonService(ServiceType = typeof(Database))]
partial class Database : IDatabase
{
    private readonly IDatabase con;
 
    public Database(IDatabase con)
    {
        this.con = con;
    }
    public void Open()
    {
        Console.WriteLine($"open from database");
        con.Open();
    }
 
}
01
02
03
04
05
06
07
08
09
10
11
12
using DependencyModules.Runtime.Attributes;
 
namespace InjectDemo;
[SingletonService]
public partial class DatabaseCon:IDatabase
{
    public string? Connection { get; set; }
    public void Open()
    {
        Console.WriteLine("open from database con" );
    }
}

 

The code that is generated is

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
using DependencyModules.Runtime.Helpers;
using InjectDemo;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
 
public partial class MyModule
{
    private static int moduleField = DependencyRegistry<MyModule>.Add(ModuleDependencies);
 
    private static void ModuleDependencies(IServiceCollection services)
    {
        services.AddSingleton(typeof(Database), typeof(Database));
        services.AddSingleton(typeof(IDatabase), typeof(DatabaseCon));
    }
}
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
using BaseAttribute = System.Attribute;
using DependencyModules.Runtime.Helpers;
using DependencyModules.Runtime.Interfaces;
using Microsoft.Extensions.DependencyInjection;
 
#nullable enable
public partial class MyModule : IDependencyModule
{
 
    static MyModule()
    {
    }
 
    public void PopulateServiceCollection(IServiceCollection services)
    {
        DependencyRegistry<MyModule>.LoadModules(services, this);
    }
 
    void IDependencyModule.InternalApplyServices(IServiceCollection services)
    {
        DependencyRegistry<MyModule>.ApplyServices(services);
    }
 
    public override bool Equals(object? obj)
    {
        return obj is MyModule;
    }
 
    public override int GetHashCode()
    {
        return HashCode.Combine(base.GetHashCode());
    }
 
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Assembly | AttributeTargets.Method | AttributeTargets.Parameter, AllowMultiple = true)]
    public partial class Attribute : BaseAttribute, IDependencyModuleProvider
    {
 
        public IDependencyModule GetModule()
        {
            var newModule = new MyModule();
            return newModule;
        }
    }
}
#nullable disable

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/DependencyModules.SourceGenerator

RSCG – MockMe

RSCG – MockMe
 
 

name MockMe
nuget https://www.nuget.org/packages/MockMe/
link https://github.com/connorivy/MockMe/
author connorivy

Creating mocks for testing classes

 

This is how you can use MockMe .

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
22
23
24
25
26
27
28
29
<Project Sdk="Microsoft.NET.Sdk">
 
  <PropertyGroup>
    <TargetFramework>net9.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
 
    <IsPackable>false</IsPackable>
    <IsTestProject>true</IsTestProject>
  </PropertyGroup>
 
  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
    <PackageReference Include="MockMe" Version="1.1.2" />
    <PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
    <PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
    <PackageReference Include="coverlet.collector" Version="3.2.0" />
  </ItemGroup>
 
  <ItemGroup>
    <ProjectReference Include="..\MockData\MockData.csproj" />
  </ItemGroup>
 
    <PropertyGroup>
        <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
        <CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
    </PropertyGroup>
 
</Project>

The code that you will use is

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
using MockMe;
 
namespace TestClock;
 
[TestClass]
public class TestClock
{
    [TestMethod]
    public void TestMyClock()
    {
        var mock = Mock.Me(default(MyClock));
        mock.Setup.GetUtcNow().Returns(DateTime.Now.AddYears(-1));
        mock.Setup.GetNow().Returns(DateTime.Now.AddYears(-1));
        MyClock clock = mock;
        Assert.AreEqual(DateTime.Now.AddYears(-1).Year, clock.GetNow().Year);
    }
}

 

The code that is generated is

1
 
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
// <auto-generated />
#pragma warning disable
using System;
 
namespace MockMe
{
    internal static partial class Mock
    {
        public static object Me(global::MockMe.DummyClass unusedInstance)
        {
            throw new global::System.NotImplementedException();
        }
    }
}
#pragma warning restore
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
// <auto-generated />
#pragma warning disable
#nullable enable
 
namespace MockMe
{
    internal static partial class Mock
    {
 
 
        [global::System.CodeDom.Compiler.GeneratedCode("MockMe", "1.1.2.0")]
        public static global::MockMe.Generated.MockData.MyClockMock Me(global::MockData.MyClock? unusedInstance)
        {
            return new();
        }
 
    }
}
#pragma warning restore
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// <auto-generated />
#pragma warning disable
#nullable enable
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Reflection;
using HarmonyLib;
using MockMe;
using MockMe.Mocks;
using MockMe.Mocks.ClassMemberMocks;
using MockMe.Mocks.ClassMemberMocks.CallTracker;
 
namespace MockMe.Generated.MockData
{
    [global::System.CodeDom.Compiler.GeneratedCode("MockMe", "1.1.2.0")]
    internal class MyClockMock
        : global::MockMe.Abstractions.SealedTypeMock<global::MockData.MyClock>
    {
         
        public MyClockMock()
        {
            this.Setup = new MyClockMockSetup();
            this.CallTracker = new MyClockMockSetup.MyClockMockCallTracker(this.Setup);
            this.Assert = new MyClockMockSetup.MyClockMockCallTracker.MyClockMockAsserter(this.CallTracker);
            global::MockMe.MockStore<global::MockData.MyClock>.Store.TryAdd(this.MockedObject, this);
        }
 
        public MyClockMockSetup Setup { get; }
        public MyClockMockSetup.MyClockMockCallTracker.MyClockMockAsserter Assert { get; }
        private MyClockMockSetup.MyClockMockCallTracker CallTracker { get; }
 
        internal sealed class Patch23f715ba229342e08d6fb11494a17f90
        {
            private static bool Prefix(global::MockData.MyClock __instance, ref global::System.DateTime __result)
            {
                if (global::MockMe.MockStore<global::MockData.MyClock>.TryGetValue<MyClockMock>(__instance, out var mock))
                {
                    __result = mock.CallTracker.GetNow();
                    return false;
                }
 
                return true;
            }
        }
        internal sealed class Patch147ac8e5593f4d678d1855b2e81726e7
        {
            private static bool Prefix(global::MockData.MyClock __instance, ref global::System.DateTime __result)
            {
                if (global::MockMe.MockStore<global::MockData.MyClock>.TryGetValue<MyClockMock>(__instance, out var mock))
                {
                    __result = mock.CallTracker.GetUtcNow();
                    return false;
                }
 
                return true;
            }
        }
        static MyClockMock()
        {
            var harmony = new global::HarmonyLib.Harmony("com.mockme.patch");
 
            var originalPatch23f715ba229342e08d6fb11494a17f90 = typeof(global::MockData.MyClock).GetMethod("GetNow", new Type[] {  } );
            var Patch23f715ba229342e08d6fb11494a17f90 = typeof(Patch23f715ba229342e08d6fb11494a17f90).GetMethod("Prefix", global::System.Reflection.BindingFlags.Static | global::System.Reflection.BindingFlags.NonPublic);
 
            harmony.Patch(originalPatch23f715ba229342e08d6fb11494a17f90, prefix: new HarmonyMethod(Patch23f715ba229342e08d6fb11494a17f90));
 
            var originalPatch147ac8e5593f4d678d1855b2e81726e7 = typeof(global::MockData.MyClock).GetMethod("GetUtcNow", new Type[] {  } );
            var Patch147ac8e5593f4d678d1855b2e81726e7 = typeof(Patch147ac8e5593f4d678d1855b2e81726e7).GetMethod("Prefix", global::System.Reflection.BindingFlags.Static | global::System.Reflection.BindingFlags.NonPublic);
 
            harmony.Patch(originalPatch147ac8e5593f4d678d1855b2e81726e7, prefix: new HarmonyMethod(Patch147ac8e5593f4d678d1855b2e81726e7));
 
        }
 
    }
 
    [global::System.CodeDom.Compiler.GeneratedCode("MockMe", "1.1.2.0")]
    internal class MyClockMockSetup : global::MockMe.Mocks.ClassMemberMocks.Setup.MemberMockSetup
    {
 
        private global::MockMe.Mocks.ClassMemberMocks.MemberMock<global::System.DateTime>? GetNow_BagStore;
        public global::MockMe.Mocks.ClassMemberMocks.MemberMock<global::System.DateTime> GetNow()
        {
 
            return this.GetNow_BagStore ??= new();;
        }
        private global::MockMe.Mocks.ClassMemberMocks.MemberMock<global::System.DateTime>? GetUtcNow_BagStore;
        public global::MockMe.Mocks.ClassMemberMocks.MemberMock<global::System.DateTime> GetUtcNow()
        {
 
            return this.GetUtcNow_BagStore ??= new();;
        }
        [global::System.CodeDom.Compiler.GeneratedCode("MockMe", "1.1.2.0")]
        internal class MyClockMockCallTracker : MockCallTracker
        {
            private readonly MyClockMockSetup setup;
            public MyClockMockCallTracker(MyClockMockSetup setup)
            {
                this.setup = setup;
            }
 
            private int GetNow_CallStore;
 
            public global::System.DateTime GetNow()
            {
                this.GetNow_CallStore++;
                return MockCallTracker.CallMemberMock<global::System.DateTime>(this.setup.GetNow_BagStore);
            }
 
            private int GetUtcNow_CallStore;
 
            public global::System.DateTime GetUtcNow()
            {
                this.GetUtcNow_CallStore++;
                return MockCallTracker.CallMemberMock<global::System.DateTime>(this.setup.GetUtcNow_BagStore);
            }
 
            [global::System.CodeDom.Compiler.GeneratedCode("MockMe", "1.1.2.0")]
            internal class MyClockMockAsserter : MockAsserter
            {
                private readonly MyClockMockSetup.MyClockMockCallTracker tracker;
                public MyClockMockAsserter(MyClockMockSetup.MyClockMockCallTracker tracker)
                {
                    this.tracker = tracker;
                }
 
                public global::MockMe.Asserters.MemberAsserter GetNow() =>
                    new(this.tracker.GetNow_CallStore);
 
                public global::MockMe.Asserters.MemberAsserter GetUtcNow() =>
                    new(this.tracker.GetUtcNow_CallStore);
 
            }
 
        }
 
    }
 
}
#pragma warning restore

Code and pdf at

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

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.