DIAttribute 1.0.0
.NET 5.0
This package targets .NET 5.0. The package is compatible with this framework or higher.
.NET Core 3.1
This package targets .NET Core 3.1. The package is compatible with this framework or higher.
.NET Standard 2.0
This package targets .NET Standard 2.0. The package is compatible with this framework or higher.
.NET Framework 4.6.2
This package targets .NET Framework 4.6.2. The package is compatible with this framework or higher.
dotnet add package DIAttribute --version 1.0.0
NuGet\Install-Package DIAttribute -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="DIAttribute" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DIAttribute" Version="1.0.0" />
<PackageReference Include="DIAttribute" />
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 DIAttribute --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: DIAttribute, 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.
#:package DIAttribute@1.0.0
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DIAttribute&version=1.0.0
#tool nuget:?package=DIAttribute&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
DIAttribute
Replace service registration and acquisition with attributes to simplify code writing.
用特性替代服务注册和获取,简化代码写法。
- [DIRegister]
Used to initialise the registration service. 用于初始化注册服务。 - [DIInject]
Used in place of the injection write method to obtain services directly. 用于代替注入写法直接获取服务。
English Description
1. [DIRegister]
native writing style
- registering for a service list:
var services = new ServiceCollection();
services.AddSingleton<TestService1>();
services.AddSingleton<ITestService2,TestService2>();
//...
var provider = services.BuildServiceProvider();
- services:
class TestService1
{
//...
}
class TestService2
{
//...
}
interface ITestService2 {}
current writing
- registering for a service list:
var services = new ServiceCollection();
services.RegisterCurrentAssembly(); // just add this, or services.RegisterAssembly(assmbly);
var provider = services.BuildServiceProvider();
- services:
[DIRegister] //add attribute if need register
class TestService1
{
//...
}
[DIRegister(typeof(ITestService2))] // if use interface
class TestService2
{
//...
}
interface ITestService2 {}
2.[DIInject]
native writing style
class TestService1
{
ITestService2 _service2 { get; set; }
public TestService1(ITestService2 service2 /*,TestService3 service3...*/)
{
_service2 = service2;
//_service3 = service3;
_service2.Method();
}
}
current writing
- registering for a service list:
var services = new ServiceCollection();
//...
var provider = services.BuildServiceProvider().ForDIInject(this); //add method
- class property: You can get the service value even if the current class is not involved in the injection
class TestService1 : ServiceSetter // Inheriting this class automatically injects property values
{
[DIInject] //add attribute if need inject
ITestService2 _service2 { get; set; }
public TestService1()
{
_service2.Method(); // can get service in constructor
}
}
If you don't want to inherit from this base class, you can also call the property injection method manually:
class TestService1
{
[DIInject] //add attribute if need inject
ITestService2 _service2 { get; set; }
public TestService1()
{
ServiceSetter.InjectServices(this);// manually call without inherit class
_service2.Method(); // get service after InjectServices
}
}
中文介绍
1.[DIRegister]
原生写法
- 初始化注册服务到集合:
var services = new ServiceCollection();
services.AddSingleton<TestService1>();
services.AddSingleton<ITestService2>();
//...
var provider = services.BuildServiceProvider();
- service服务示例:
class TestService1
{
//...
}
class TestService2
{
//...
}
interface ITestService2 {}
现在写法
- 初始化注册服务到集合:
var services = new ServiceCollection();
services.RegisterCurrentAssembly(); // 只要添加这行, 或者指定 services.RegisterAssembly(assmbly);
var provider = services.BuildServiceProvider();
- service服务示例:
[DIRegister] // 如果要注册的服务就添加特性
class TestService1
{
//...
}
[DIRegister(typeof(ITestService2))] // 如果有接口则指定
class TestService2
{
//...
}
interface ITestService2 {}
2.[DIInject]
原生写法
class TestService1
{
ITestService2 _service2 { get; set; }
public TestService1(ITestService2 service2 /*,TestService3 service3...*/)
{
_service2 = service2;
//_service3 = service3;
_service2.Method();
}
}
现在写法
- 初始化注册服务到集合:
var services = new ServiceCollection();
//...
var provider = services.BuildServiceProvider().ForDIInject(this); //补充调用方法用于后续注入获取
- 类写法: 即使当前类不参与注入也可以获取到服务值
class TestService1 : ServiceSetter //继承这个类会自动给有特性的属性赋值
{
[DIInject] //如果需要注入添加这个特性
ITestService2 _service2 { get; set; }
public TestService1()
{
_service2.Method();//服务可以在构造函数中获取到
}
}
如果不想要继承这个基类,也可以手动调用属性注入方法:
class TestService1
{
[DIInject] //如果需要注入添加这个特性
ITestService2 _service2 { get; set; }
public TestService1()
{
ServiceSetter.InjectServices(this);// 手动调用注入属性服务
_service2.Method(); //服务在注入后可以获取到
}
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 is compatible. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 is compatible. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETCoreApp 3.1
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
-
.NETFramework 4.6.2
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
-
.NETStandard 2.0
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
-
net5.0
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
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 | 262 | 3/11/2024 |