Skip to content

Improve DI-related APIs to support custom types#27

Merged
smdn merged 6 commits intomainfrom
extend-di-apis
Jun 1, 2025
Merged

Improve DI-related APIs to support custom types#27
smdn merged 6 commits intomainfrom
extend-di-apis

Conversation

@smdn
Copy link
Owner

@smdn smdn commented May 31, 2025

Description

Extend the dependency injection-related APIs.

This PR allows users to use the types defined by them for the node type added to IServiceCollection and the builder type used to configure the node.

By making MuninNodeOptions and MuninNodeBuilder public and extensible, users can extend them based on these types.
In particular, in MuninNodeBuilder, by changing it to be able to construct and return any implementation of IMuninNode, it becomes possible to register user-extended Munin nodes in IServiceCollection.
In addition, make changes so that TService and TImplementation can be specified when registering nodes in IServiceCollection.

IMuninNodeBuilder is being deprecated because, once MuninNodeBuilder is made public, it will no longer be functional as an abstract interface for defining user-defined builder types.

The outline of the API changes is as follows:

-  public sealed class MuninNodeOptions { }
+  public class MuninNodeOptions { }
 
+  [Obsolete("Use or inherit MuninNodeBuilder instead.")]
   public interface IMuninNodeBuilder { }
 
+  public class MuninNodeBuilder : IMuninNodeBuilder {
+    protected virtual IMuninNode Build(IPluginProvider pluginProvider, IMuninNodeListenerFactory? listenerFactory, IServiceProvider serviceProvider)
+  }
 
+  [Obsolete("Use MuninNodeBuilderExtensions instead.")]
   public static class IMuninNodeBuilderExtensions { }
 
+  public static class MuninNodeBuilderExtensions { }
 
   public static class IMuninServiceBuilderExtensions {
+    public static TMuninNodeBuilder AddNode<TMuninNode, TMuninNodeOptions, TMuninNodeBuilder>(this IMuninServiceBuilder builder, Action<TMuninNodeOptions> configure, Func<IMuninServiceBuilder, string, TMuninNodeBuilder> createBuilder) where TMuninNode : class, IMuninNode where TMuninNodeOptions : MuninNodeOptions, new() where TMuninNodeBuilder : MuninNodeBuilder {}
+    public static TMuninNodeBuilder AddNode<TMuninNodeOptions, TMuninNodeBuilder>(this IMuninServiceBuilder builder, Action<TMuninNodeOptions> configure, Func<IMuninServiceBuilder, string, TMuninNodeBuilder> createBuilder) where TMuninNodeOptions : MuninNodeOptions, new() where TMuninNodeBuilder : MuninNodeBuilder {}
+    public static TMuninNodeBuilder AddNode<TMuninNodeService, TMuninNodeImplementation, TMuninNodeOptions, TMuninNodeBuilder>(this IMuninServiceBuilder builder, Action<TMuninNodeOptions> configure, Func<IMuninServiceBuilder, string, TMuninNodeBuilder> createBuilder) where TMuninNodeService : class, IMuninNode where TMuninNodeImplementation : class, TMuninNodeService where TMuninNodeOptions : MuninNodeOptions, new() where TMuninNodeBuilder : MuninNodeBuilder {}
 }

@smdn smdn merged commit afe0e01 into main Jun 1, 2025
13 checks passed
@smdn smdn deleted the extend-di-apis branch June 1, 2025 09:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant