Category: .NET

What I have learned by building .NET Stars -part 5 – always available data for a display website

What I have learned by building .NET Stars -part 5 – always available data for a display website

Dotnet Stars being a site just for displaying data, it does not require an API per se. Yes, for development purposes it needs a database and an API to display – but later – the data could be retrieved from local.

The first part is to write data in JSON files near to Blazor . But how to export by default this ?

And here ASPIRE thrives : I have made a console app to export data – and registered in ASPIRE with dependency of Blazor – and can see where Blazor folder is.

this is the extension

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
public static IResourceBuilder<TRes> AddPathToEnvironmment<TProject,TRes>(
        this IResourceBuilder<TRes> builder, TProject p, string name)
        where TProject : IProjectMetadata, new()
        where TRes : IResourceWithEnvironment           
{
    //var p = new TProject();       
    string pathPrj = p.ProjectPath;
    var fi = new FileInfo(pathPrj);
    string dirName = fi?.DirectoryName ?? "";
    var projectBuilder = builder
        .WithEnvironment(ctx=>
        {
            ctx.EnvironmentVariables[name] =dirName;
            ctx.EnvironmentVariables[$"{name}csproj"] = pathPrj;
        });
 
    return projectBuilder;
}

and this is how it is used

1
2
3
4
5
6
var exportToJson = builder.AddProject<Projects.StatsExport>("statsExport")
    .WithReference(ui)   
    .WithReference(db)
    .WaitFor(db)
    .AddPathToEnvironmment(new Projects.StatsBlazorUI(),"pathToWrite")
    ;

And the code that uses this

1
2
3
4
5
6
var pathToWrite = Environment.GetEnvironmentVariable("pathToWrite");
if (string.IsNullOrWhiteSpace(pathToWrite))
{
    Console.WriteLine("please add a path to write");
    return;
}

The second part is to get data from WebAPI , if available, and, if not, from JSON files.
And here the Roslyn Code Generator, https://github.com/ignatandrei/RSCG_CompositeProvider , it is useful .
We have 2 implementations of same interface ,

1
2
3
4
5
public interface IStatsData
{
   //other code
    IAsyncEnumerable<IProjectWithStars> GetProjectsWithStars();
}

And we have an implementation from WebAPI and another from JSON files

With the nugethttps://nuget.org/packages/RSCG_CompositeProviderwe can obtain data from the first that returns data.

1
2
3
4
5
6
7
8
builder.Services.AddKeyedScoped<IStatsData>("both", (sp, obj) =>
{
    var statsDataLocal = sp.GetRequiredKeyedService<IStatsData>("local_host");
    var statsDataAPI = sp.GetRequiredKeyedService<IStatsData>("statsconsole_host");
    StatsData_CP composite = new(statsDataAPI, statsDataLocal);
    composite.UseFirstTheLastOneThatWorks = true;
    return composite;
});

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

Pattern: CompositeProvider

Description

Composite Provider pattern is a structural design pattern that lets you compose objects into tree structures to represent part-whole hierarchies.
Composite lets clients treat individual objects and compositions of objects uniformly.

Example in .NET :

CompositeProvider

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
using Microsoft.Extensions.FileProviders;
 
namespace CompositeProvider;
public class CompositeProviderDemo
{
    private readonly IFileProvider _fileProvider;
    public CompositeProviderDemo(string folder)
    {
        var fileOnHDDProvider = new PhysicalFileProvider(folder);
        var manifestEmbeddedProvider =
    new ManifestEmbeddedFileProvider(this.GetType().Assembly);
        _fileProvider = new CompositeFileProvider(fileOnHDDProvider,manifestEmbeddedProvider);
 
    }
    public string[] GetFiles()
    {
        List<string> ret = [];
        var files = _fileProvider.GetDirectoryContents(string.Empty);
        var contents = _fileProvider.GetDirectoryContents(string.Empty);
        foreach (var fileInfo in contents)
        {
            ret.Add(fileInfo.Name);
        }
        return ret.ToArray();
    }
    public string GetContentFile(string name)
    {
        var fileInfo = _fileProvider.GetFileInfo(name);
        if (fileInfo.Exists)
        {
            using var stream = fileInfo.CreateReadStream();
            using var reader = new StreamReader(stream);
            return reader.ReadToEnd();
        }
        return string.Empty;
    }
 
}

And his usage

01
02
03
04
05
06
07
08
09
10
11
12
13
14
// See https://aka.ms/new-console-template for more information
using CompositeProvider;
 
Console.WriteLine("Hello, World!");
var path = Directory.GetParent(Environment.CurrentDirectory);
ArgumentNullException.ThrowIfNull(path);
CompositeProviderDemo compositeProvider = new(path.FullName) ;
Console.WriteLine("Files in the current directory:");
foreach (var file in compositeProvider.GetFiles())
{
    Console.WriteLine(file);
}
Console.WriteLine("obtain MyResource.txt even if does not exists on folder");
Console.WriteLine(compositeProvider.GetContentFile("MyResource.txt"));

Learn More

Source Code for Microsoft implementation of CompositeProvider SourceCode Composite Provider
Learn More File Providers in ASP.NET Core
Wikipedia
}

Homework

Imagine that you have 2 loggers , for a file and a console.
Implement a composite provider that allows you to log to multiple destinations at the same time.

What I have learned by building .NET Stars -part 4- Keyed Dependency Injection – constructor by keys

As I said before, I have constructed first the interfaces. Then I have a null object pattern for each interface, constructed automatically with a Roslyn Code Generator , https://www.nuget.org/packages/rscg_Interface_to_null_object .

And now, what I want is to have keyed DI for each new implementation -with a twist: It’s like a game of matching keys when another class is injected, the first one gets chosen if it has the exact same key!

Let me give an illustrative example :

Imagine interfaces IA and IB, with Class_A_Null and Class_B_Null as their null object counterparts. These are injected by default into our DI container

We have class class_A_Data1 that implements IA.
It is injected with key “Data1”

We have class B_Data1 ( implements IB ) that has a constructor that has a parameter IA .
It is injected with key “Data1”

We have class B_Data2 ( implements IB ) that has a constructor that has a parameter IA .
It is injected with key “Data2”

So every time we construct an IB it requires it’s companion , IA .
 
Now  let’s say I want DI to construct B_Data1 and B_Data2

When want to construct B_Data1 , it sees that has a Key “Data1” . See that he needs also an IA –
DI can choose between default implementation “Class_A_Null” ( without key ) and “class_A_Data1” with the key “Data1”
Because it has the same key (“Data1” ) as the class that must be implemented (“B_Data1” ) chooses class_A_Data1

When want to construct B_Data2 , it sees that has a Key “Data2” . See that he needs also an IA.
DI can choose between default implementation “Class_A_Null” ( without key ) and “class_A_Data1” with the key “Data1”
Because the class to be constructed has the key “Data2”, the “class_A_Data1” with the key “Data1” is eliminated.
So it constructs B_Data2 with the default implementation , “Class_A_Null” ( without key )

Why this constraint ? Because I started with null implementation, and add one by one the implementation. 

The WebAPI  works every time ( the null implementation does nothing , so it works ).
And slowly but surely we add to the DI key another implementation and we are sure that works every time .

This innovative use of keyed dependency injection, particularly starting with null object patterns, allows for iterative development without breaking existing functionalities. As each new implementation is added, the system remains stable and functional, gradually enhancing its capabilities with each iteration.

Feel free to experiment with these ideas and adapt them to your own projects. Start by trying out the rscg_Interface_to_null_object package, and see how it can transform your development workflow

You can see at https://ignatandrei.github.io/dotnetstats/ and the source code at http://github.com/ignatandrei/dotnetstats/

The code for this special DI is at https://github.com/ignatandrei/dotnetstats/blob/main/src/Stats/StatsConsole/KeyedServiceCollection.cs ( too long to put here )

What I have learned by building .NET Stars -part 3- Aspire

I have that same idea to build a project : is what is called today a Modular Monolith – and it is very old in .NET world as can be implemented as a .sln solution.
For those unfamiliar, a Modular Monolith allows you to break down your application into independent modules (think database access, data flow logic, a sleek UI), yet keep them tightly integrated within a single, cohesive solution.

It’s how all these components can work together seamlessly. This idea resonated with me when I started thinking about my project. So, let me break it down:

Interfaces for Data/Flow These lay the foundation, ensuring that data and operations flow smoothly between different parts of the project.
  
– A database stores all the necessary information, serving as the backbone of my application.

– Then there’s the WebAPI, which acts like a messenger, transferring data between the database and the users’ interfaces.

– And finally, for the User Interface, I’ve chosen Blazor. It brings together code, design, and interactivity i

Aspire is a game-changer in the .NET world, offering a simple yet powerful way to coordinate multiple projects. By starting an app host with all the projects intertwined, it simplifies the process of building complex applications.  More, Aspire let me coordinate another project to save the data on Blazor – but this will be discussed in another blog post later .

I have had just 2 modifications to make it work flawlessly :

1. Blazor

  To know the address of the WebAPI to obtain the data ( once published, Blazor will be near the WebAPI in the wwwroot, but until then it needs the adress )

Blazor can have the configuration stored in a appsettings.json in the wwwroot – but how to write ? I developed an extension for ASPIRE in order to write the data

2. Database

In order to have the database with data , I need to write scripts for create table / insert the data.

This code shows how

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
var paramPass = builder.AddParameter("password", "P@ssw0rd");
 
var sqlserver = builder.AddSqlServer("sqlserver",paramPass,1433)
    //.WithArgs("pwd","&amp;","ls")
    // Mount the init scripts directory into the container.
    .WithBindMount("./sqlserverconfig", "/usr/config")
    // Mount the SQL scripts directory into the container so that the init scripts run.
    .WithBindMount("../../Scripts/data/sqlserver", "/docker-entrypoint-initdb.d")
    // Run the custom entrypoint script on startup.
    .WithEntrypoint("/usr/config/entrypoint.sh")
    // Configure the container to store data in a volume so that it persists across instances.
    .WithDataVolume()
    // Keep the container running between app host sessions.
    .WithLifetime(ContainerLifetime.Persistent)
 
    ;
var db= sqlserver.AddDatabase("DotNetStats");

Also , I have an .gitattributes that contains

1
2
3
4
* text=auto
*.sh text eol=lf
*.mod text eol=lf
*.sum text eol=lf

in order for the .sh files to maintain linux ending.

You can see at https://ignatandrei.github.io/dotnetstats/ and the source code at http://github.com/ignatandrei/dotnetstats/

What I have learned by building .NET Stars -part 2- interfaces

In my latest project, building a robust system using interfaces has been both a fascinating challenge and an enlightening experience. As I embarked on this journey, it quickly became evident how immensely powerful and transformative thinking in terms of interfaces can be.

From the outset, interfacing brought clarity to several core aspects of the application:

  • Data Management: Interfaces provided a transparent view of data structures and their usage within the app.
  • Data Flow: They illuminated how data was accessed and exchanged, simplifying what seemed complex at first glance.
  • Application Flow: Interfaces helped chart out the application’s workflow, providing insights even in its dormant state.

While initially daunting, embracing interfaces has offered immense benefits almost immediately. A notable revelation was how adopting the NullObjectPattern through an interface (you can explore this further at rscg_Interface_to_null_object) enabled me to navigate and visualize my application’s flow effortlessly – a crucial step even when it performs no operations.

One of the quicker wins was leveraging interfaces to support multiple data sources. This flexibility meant that I could seamlessly pull project details from diverse platforms like the .NET Foundation, GitHub repositories such as quozd/awesome-dotnet and thangchung/awesome-dotnet-core by implementing a common interface, IStatsData, and then efficiently saving this consolidated data with a single implementation of IProjectsData.

Our interface designs have opened up a world of possibilities for flexibility and reusability. Consider these interfaces :

01
02
03
04
05
06
07
08
09
10
11
12
13
14
[ToNullObject]
public interface IStatsData
{
    //other code
IAsyncEnumerable<IProject> RefreshProjects();
}
 
[ToNullObject]
public interface IProjectsData
{
    IAsyncEnumerable<IProject> GetProjectsAsync();
    Task<bool> SaveProjects(IProject[] projects);
 
}

 

With these interfaces, we have the freedom to implement various strategies for acquiring projects from diverse sources such as DotNetFoundation, awesome-dotnet, and awesome-dotnet-core. Moreover, we can centralize the project storage logic with just one implementation of IProjectsData.

The result is a dynamic, versatile application that not only offers rich functionality but also encourages continuous improvement.

Eager to see the fruits of this approach? You can dive into our project live at https://ignatandrei.github.io/dotnetstats/. Additionally, the source code is available for all to explore at http://github.com/ignatandrei/dotnetstats/.

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.