Dncy.DependencyInjection.Generator 1.0.0

dotnet add package Dncy.DependencyInjection.Generator --version 1.0.0                
NuGet\Install-Package Dncy.DependencyInjection.Generator -Version 1.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="Dncy.DependencyInjection.Generator" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Dncy.DependencyInjection.Generator --version 1.0.0                
#r "nuget: Dncy.DependencyInjection.Generator, 1.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 Dncy.DependencyInjection.Generator as a Cake Addin
#addin nuget:?package=Dncy.DependencyInjection.Generator&version=1.0.0

// Install Dncy.DependencyInjection.Generator as a Cake Tool
#tool nuget:?package=Dncy.DependencyInjection.Generator&version=1.0.0                

依赖注入代码自动生成器

  1. 原生注入代码生成

注入使用示例:

测试项目 ConsoleTest

 [Injectable(InjectLifeTime.Scoped)]
public class Demo
{
   public void Cw()
   {
      Console.WriteLine("Demo");
   }
}

 [Injectable(InjectLifeTime.Scoped, typeof(IServiceA))]
    public class ServiceA:IServiceA
    {
        public void Cw()
        {
            Console.WriteLine("IServiceA");
        }
    }
// 以上两个都会有被注入

var services = new ServiceCollection();
services.AutoInjectConsoleTest(); // 这里扩展名称是:AutoInject+程序集名称方式
  1. 原生注入解析代码生成

构造函数解析使用示例:

    [ApiController]
    [AutoResolveDependency]
    [Route("[controller]")]
    public partial class  ValueController:ControllerBase
    {
        [AutoInject]
        private readonly IBaseRepository<WeatherForecast> _baseRepository;

        [AutoInject]
        private readonly ICustomeRepository _customeRepository;


        [HttpGet]
        public IEnumerable<string> Get()
        {
            var weatherForecasts = _baseRepository.GetList();
            return new List<string>() {"123"};
        }
    }

支持类继承的解析

调试

在调试的时候,目标项目需要引入生成器先项目,然后再 csproj 中加入:


<ItemGroup>
    <ProjectReference Include="..\..\DependencyInjection.Generator\Dncy.MicrosoftDependencyInjection.Generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  </ItemGroup>

然后在生成器的Initialize方法中使用Debugger

if (!Debugger.IsAttached)
{
    Debugger.Launch();
}

调试的时候 可能更改完不会生成最新的目标代码,需要重启 vs,重新打开项目。 打包后引入就不会出现这种问题了。

打包

对应项目的 Csproj 中需要添加一下节点配置

<ItemGroup>
		
		<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
	</ItemGroup>
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
1.0.0 247 7/1/2022