Fluentish.InjectableStatic.Generator 0.2.1

dotnet add package Fluentish.InjectableStatic.Generator --version 0.2.1
                    
NuGet\Install-Package Fluentish.InjectableStatic.Generator -Version 0.2.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Fluentish.InjectableStatic.Generator" Version="0.2.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Fluentish.InjectableStatic.Generator" Version="0.2.1" />
                    
Directory.Packages.props
<PackageReference Include="Fluentish.InjectableStatic.Generator" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Fluentish.InjectableStatic.Generator --version 0.2.1
                    
#r "nuget: Fluentish.InjectableStatic.Generator, 0.2.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=Fluentish.InjectableStatic.Generator&version=0.2.1
                    
Install Fluentish.InjectableStatic.Generator as a Cake Addin
#tool nuget:?package=Fluentish.InjectableStatic.Generator&version=0.2.1
                    
Install Fluentish.InjectableStatic.Generator as a Cake Tool

Fluentish.InjectableStatic.Generator

When upstream doesn't like inversion of control

Fluentish.InjectableStatic.Generator is a source generator for C#. It allows you to generate interface and service classes that wrap static members of your target classes. By applying attributes, you can control which static classes and members become “injectable,” making it easier to work with otherwise rigid static code through a dependency injection container.

Features

  • Automatically generate wrapper classes and corresponding interfaces
  • open generics support
  • Supported member types:
    • Constants
    • static Properties
    • static Events
    • static Fields
    • static Methods
  • Filter which members are included in generated code
  • Customize namespace prefix

Usage

Basic usage:

[assembly: Fluentish.InjectableStatic.Injectable(typeof(System.Console))]

Fluentish.Injectable.System.IConsole console = new Fluentish.Injectable.System.ConsoleService();

console.WriteLine("Hello, World!");

Will generate Fluentish.Injectable.System.IConsole and Fluentish.Injectable.System.ConsoleService, which contain all the static members.

Filtering members:

  1. Exclude
    [assembly: Fluentish.InjectableStatic.Injectable(
        typeof(System.Diagnostics.Debug),
        Fluentish.InjectableStatic.FilterType.Exclude,
        nameof(System.Diagnostics.Debug.Print)
    )]
    
    Will generate Fluentish.Injectable.System.Diagnostics.IDebug and Fluentish.Injectable.System.Diagnostics.DebugService without the Print method.
  2. Include
    [assembly: Fluentish.InjectableStatic.Injectable(
        typeof(System.Diagnostics.Debug),
        Fluentish.InjectableStatic.FilterType.Include,
        nameof(System.Diagnostics.Debug.Write),
        nameof(System.Diagnostics.Debug.WriteIf),
        nameof(System.Diagnostics.Debug.WriteLine),
        nameof(System.Diagnostics.Debug.WriteLineIf)
    )]
    
    Will generate Fluentish.Injectable.System.Diagnostics.IDebug and Fluentish.Injectable.System.Diagnostics.DebugService containing only the listed members.

Configuration

By default, generated classes are prefixed with Fluentish.Injectable. to avoid name collisions. You can customize this behavior using the InjectableStaticConfigurationAttribute at the assembly level.

Available Options

[assembly: Fluentish.InjectableStatic.InjectableStaticConfigurationAttribute(
    Namespace = "Your.Custom.Namespace",
    NamespaceMode = Fluentish.InjectableStatic.NamespaceMode.Prefix // or Fluentish.InjectableStatic.NamespaceMode.Const
)]
Property Description
Namespace The base namespace for generated wrappers. Set to an empty string ("") to disable namespace prefixing entirely.
NamespaceMode Determines how the namespace is applied. See below for details.

Namespace Modes

  • NamespaceMode.Prefix (default):
    The value of Namespace is used as a prefix. For example, if set to "My.Custom", generated classes will be in "My.Custom.Original.Namespace".

  • NamespaceMode.Const:
    The value of Namespace replaces the entire namespace. All generated classes will reside in exactly the specified namespace.

Examples

Remove the default prefix:

[assembly: Fluentish.InjectableStatic.InjectableStaticConfigurationAttribute(Namespace = "")]

Use a custom namespace as a prefix:

[assembly: Fluentish.InjectableStatic.InjectableStaticConfigurationAttribute(
    Namespace = "My.Namespace",
    NamespaceMode = NamespaceMode.Prefix
)]

Force all wrappers into a constant namespace:

[assembly: Fluentish.InjectableStatic.InjectableStaticConfigurationAttribute(
    Namespace = "My.Constant.Namespace",
    NamespaceMode = NamespaceMode.Const
)]

Remarks

Please note that this project is in its early stages of development and may require significant improvements. While it is functional, it may not be suitable for production use in its current state. Any feedback, suggestions, or contributions to improve the source generator are highly appreciated.

Contributing

All contributions are appreciated, but please discuss any ideas or planned changes with me through a GitHub issue beforehand to avoid unnecessary or conflicting work.

Support

If you have any questions, issues, or suggestions, please create a new issue on the GitHub repository.

License

This project is licensed under the MIT License. See the LICENSE file for more information.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Fluentish.InjectableStatic.Generator:

Package Downloads
Godot.DependencyInjection.Services

part of custom dependency injection for Godot game engine

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.2.1 188 4/21/2025
0.2.0 156 4/21/2025
0.1.0 174 4/13/2025