RSCG – Injectio

RSCG – Injectio
 
 

name Injectio
nuget https://www.nuget.org/packages/Injectio/
link https://github.com/loresoft/Injectio
author LoreSoft

Attributes to DI helper

 

This is how you can use Injectio .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
	<ItemGroup>
		<PackageReference Include="Injectio" Version="2.6.1" />
		<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
	</ItemGroup>
	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>
</Project>


The code that you will use is


using InjectioDemo;
using Microsoft.Extensions.DependencyInjection;

Console.WriteLine("Hello, World!");
ServiceCollection sc = new();
sc.AddInjectioDemo();
var b = sc.BuildServiceProvider();
var con = b.GetRequiredService<DatabaseCon>();
var db = b.GetRequiredService<IDatabase>();
db.Open();



using Injectio.Attributes;

namespace InjectioDemo;

[RegisterScoped]
internal class Database : IDatabase
{
    private readonly DatabaseCon con;

    public Database(DatabaseCon con)
    {
        this.con = con;
    }
    public void Open()
    {
        Console.WriteLine($"open {con.Connection}");
    }

}




namespace InjectioDemo
{
    internal interface IDatabase
    {
        public void Open();
    }
}


using Injectio.Attributes;

namespace InjectioDemo;

[RegisterSingleton]
internal class DatabaseCon
{
    public string? Connection { get; set; }
}



 

The code that is generated is

// <auto-generated />
#nullable enable

namespace Microsoft.Extensions.DependencyInjection
{
    /// <summary>
    /// Extension methods for discovered service registrations
    /// </summary>
    [global::System.CodeDom.Compiler.GeneratedCode("Injectio.Generators", "2.6.1.0")]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.Diagnostics.DebuggerStepThroughAttribute]
    public static class DiscoveredServicesExtensions
    {
        /// <summary>
        /// Adds discovered services from InjectioDemo to the specified service collection
        /// </summary>
        /// <param name="serviceCollection">The service collection.</param>
        /// <param name="tags">The service registration tags to include.</param>
        /// <returns>The service collection</returns>
        public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddInjectioDemo(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, params string[]? tags)
        {
            var tagSet = new global::System.Collections.Generic.HashSet<string>(tags ?? global::System.Linq.Enumerable.Empty<string>());

            global::Microsoft.Extensions.DependencyInjection.Extensions.ServiceCollectionDescriptorExtensions.TryAdd(
                serviceCollection,
                global::Microsoft.Extensions.DependencyInjection.ServiceDescriptor.Describe(
                    typeof(global::InjectioDemo.IDatabase),
                    typeof(global::InjectioDemo.Database), 
                    global::Microsoft.Extensions.DependencyInjection.ServiceLifetime.Scoped
                )
            );

            global::Microsoft.Extensions.DependencyInjection.Extensions.ServiceCollectionDescriptorExtensions.TryAdd(
                serviceCollection,
                global::Microsoft.Extensions.DependencyInjection.ServiceDescriptor.Describe(
                    typeof(global::InjectioDemo.Database),
                    typeof(global::InjectioDemo.Database), 
                    global::Microsoft.Extensions.DependencyInjection.ServiceLifetime.Scoped
                )
            );

            global::Microsoft.Extensions.DependencyInjection.Extensions.ServiceCollectionDescriptorExtensions.TryAdd(
                serviceCollection,
                global::Microsoft.Extensions.DependencyInjection.ServiceDescriptor.Describe(
                    typeof(global::InjectioDemo.DatabaseCon),
                    typeof(global::InjectioDemo.DatabaseCon), 
                    global::Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton
                )
            );

            return serviceCollection;
        }
    }
}

Code and pdf at

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