ShadowCode.Injection 2.0.0

dotnet add package ShadowCode.Injection --version 2.0.0                
NuGet\Install-Package ShadowCode.Injection -Version 2.0.0                
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="ShadowCode.Injection" Version="2.0.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ShadowCode.Injection --version 2.0.0                
#r "nuget: ShadowCode.Injection, 2.0.0"                
#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.
// Install ShadowCode.Injection as a Cake Addin
#addin nuget:?package=ShadowCode.Injection&version=2.0.0

// Install ShadowCode.Injection as a Cake Tool
#tool nuget:?package=ShadowCode.Injection&version=2.0.0                

ShadowCode.Injection

基本设置

  • nuget

Microsoft.NET.Sdk.Web

PM> Install-Package ShadowCode.Injection

Microsoft.NET.Sdk

PM> Install-Package Microsoft.Extensions.DependencyInjection
PM> Install-Package Microsoft.Extensions.Configuration
PM> Install-Package Microsoft.Extensions.Options
  • global using

global using Microsoft.Extensions.DependencyInjection;
global using ShadowCode;

AddService

生成器会根据项目的空间名 RootNamespace 生成对应的扩展方法(默认情况为项目名称)

新建项目 Project1

namespace Project1;
public interface ITest 
{ }

[AddService<ITest>(ServiceLifetime.Singleton)]
internal class MyClass : ITest 
{ }

// 或 ServiceType = typeof(ITest)
[AddService(ServiceLifetime.Singleton, ServiceType = typeof(ITest))]
internal class MyClass : ITest 
{ }

生成代码如下

namespace Project1;
public static partial class ShadowCodeInjectionExtensions
{
    public static IServiceCollection UseProject1(this IServiceCollection service, 
        IConfigurationManager config = null)
    {
        UseStart(service, config);    
        {
            var serviceType = typeof(Project1.ITest);
            var implementType = typeof(Project1.MyClass);
            service.Add(new ServiceDescriptor(serviceType, implementType, ServiceLifetime.Singleton));
        }        
        UseEnd(service, config);
        return service;
    }    
    static partial void UseStart(IServiceCollection service, IConfigurationManager config);
    static partial void UseEnd(IServiceCollection service, IConfigurationManager config);    
}

Project2 调用代码如下

using Project1;

void XXX(IServiceCollection service)
{
    IConfigurationManager config = ....
    service.UseProject1(config);
}

  • 用户扩展(可选)

namespace Project1;
partial class ShadowCodeInjectionExtensions
{
    static partial void UseStart(IServiceCollection service,
        IConfigurationManager config)
    {
        config.AddJsonFile("xxx.json", true, true);
    }
}
  • 自定义扩展方法名称(可选)

某些情况下需要重启IDE才能生效!项目未单独Nuget的情况下也会失效!

<PropertyGroup>
    
    <ShadowCode_Injection_UseMethodName>UseXXXXX</ShadowCode_Injection_UseMethodName>
</PropertyGroup>

生成代码如下

namespace Project1;
public static partial class ShadowCodeInjectionExtensions
{
    public static IServiceCollection UseXXXXX(this IServiceCollection service, IConfigurationManager config = null)
    {
       ...
    }  
}

Project2 调用代码如下

using Project1;

void XXX(IServiceCollection service)
{
     IConfigurationManager config = ....
     service.UseXXXXX(config);
}
  • 注册顺序

值越大越靠后

namespace Project1;

[AddService<ITest>(ServiceLifetime.Singleton, Index = 9527)]
[AddService<ITest>(ServiceLifetime.Transient)]
[AddService<ITest>(ServiceLifetime.Scoped)]
internal class MyClass : ITest 
{ }

生成代码如下

namespace Project1;
public static partial class ShadowCodeInjectionExtensions
{
    public static IServiceCollection UseProject1(...)
    {
        {
            ...
            service.Add(new (.., ServiceLifetime.Transient));
        }
        {
            ...
            service.Add(new (.., ServiceLifetime.Scoped));
        }
        {
            ...
            service.Add(new (.., ServiceLifetime.Singleton));
        }
    } 
} 
  • 指定构造函数

namespace Project1;

internal class MyClass : ITest
{
    public MyClass(...) 
    { }

    [AddService<ITest>(ServiceLifetime.Singleton)]
    public MyClass(string msg, ITest? test) 
    { }
}

internal class MyClass<T> : ITest<T>
{
    public MyClass(...) 
    { }

    [AddService<ITest<string>>(ServiceLifetime.Singleton)]
    public MyClass(string msg, ITest? test) 
    { }
}

生成代码如下

namespace Project1;
public static partial class ShadowCodeInjectionExtensions
{
    public static IServiceCollection UseCodeSample(this IServiceCollection service, IConfigurationManager config)
    {
        ...
        {
            var serviceType = typeof(ITest);
            static object factory(IServiceProvider s) => 
                new MyClass(s.GetRequiredService<string>(), s.GetService<ITest>());
            service.Add(new ServiceDescriptor(serviceType, factory, ServiceLifetime.Singleton));
        }
        {
            var serviceType = typeof(ITest<string>);
            static object factory(IServiceProvider s) => 
                new MyClass<string>(s.GetRequiredService<string>(), s.GetService<ITest>());
            service.Add(new ServiceDescriptor(serviceType, factory, ServiceLifetime.Singleton));
        }
        return service;
    }
} 
  • 指定工厂

namespace Project1;

[AddService<ITest>(ServiceLifetime.Singleton, Factory = nameof(Factory))]
internal class MyClass : ITest
{
    public MyClass() 
    { }
    
    public MyClass(string msg, ITest? test) 
    { }

    public static object Factory(IServiceProvider service)
    { }
}

生成代码如下

namespace Project1;
public static partial class ShadowCodeInjectionExtensions
{
    public static IServiceCollection UseProject1(...)
    {
        {
            var serviceType = typeof(Project1.ITest);
            service.Add(new (serviceType, Project1.MyClass.Factory, ServiceLifetime.Singleton));
        }
    }  
} 
  • Keyed

namespace Project1;

[AddService<ITest>(ServiceLifetime.Singleton, Keyed = "key")]
internal class MyClass : ITest
{ }

生成代码如下

namespace Project1;
public static partial class ShadowCodeInjectionExtensions
{
    public static IServiceCollection UseProject1(...)
    {
        {
            var serviceType = typeof(Project1.ITest);
            object? keyed = "key";
            var implementType = typeof(Project1.MyClass);
            service.Add(new (serviceType, keyed, implementType, ServiceLifetime.Singleton));
        }
    }  
}
  • TryAdd

namespace Project1;

[AddService<ITest>(ServiceLifetime.Singleton, TryAdd = true)]
internal class MyClass : ITest 
{ }

生成代码如下

namespace Project1;
public static partial class ShadowCodeInjectionExtensions
{
    public static IServiceCollection UseProject1(...)
    {
        {
            ...
            service.TryAdd(new (.., ServiceLifetime.Singleton));
        }
    }  
}

AddOptions

namespace Project1;

[AddOptions]
class MyOptions
{ }

 // 泛型类
[AddOptions<MyOptions<string>>]
class MyOptions<T> 
{ }

 // 泛型类 typeof
[AddOptions(Type = typeof(MyOptions<string>))]
 class MyOptions<T> 
{ }

生成代码如下

namespace Project1;

public static partial class ShadowCodeInjectionExtensions
{
    // g_configure 由生成器调用!
    static partial void g_configure(IServiceCollection service, IConfigurationManager config)
    {
        service.Configure<MyOptions>(config);
        service.Configure<MyOptions<string>>(config);
    }
}
  • 指定Section 或 Name

namespace Project1;
[AddOptions<MyOptions<string>>("sectionKey", Name = "nameValue")]
class MyOptions<T> 
{ }

// 或 typeof(MyOptions<string>)
[AddOptions("sectionKey", Name = "nameValue", Type = typeof(MyOptions<string>))]
class MyOptions<T> 
{ }

生成代码如下

namespace Project1;
public static partial class ShadowCodeInjectionExtensions
{
    // g_configure 由生成器调用!
    static partial void g_configure(IServiceCollection service, IConfigurationManager config)
    {
        service.Configure<MyOptions<string>>("nameValue", config.GetSection("sectionKey"));
    }
}
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 79 12/11/2024