PocoEmit.ServiceProvider
0.8.1.3-alpha
.NET 8.0
This package targets .NET 8.0. 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.
This is a prerelease version of PocoEmit.ServiceProvider.
dotnet add package PocoEmit.ServiceProvider --version 0.8.1.3-alpha
NuGet\Install-Package PocoEmit.ServiceProvider -Version 0.8.1.3-alpha
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="PocoEmit.ServiceProvider" Version="0.8.1.3-alpha" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PocoEmit.ServiceProvider" Version="0.8.1.3-alpha" />
<PackageReference Include="PocoEmit.ServiceProvider" />
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 PocoEmit.ServiceProvider --version 0.8.1.3-alpha
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: PocoEmit.ServiceProvider, 0.8.1.3-alpha"
#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 PocoEmit.ServiceProvider@0.8.1.3-alpha
#: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=PocoEmit.ServiceProvider&version=0.8.1.3-alpha&prerelease
#tool nuget:?package=PocoEmit.ServiceProvider&version=0.8.1.3-alpha&prerelease
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Emit映射集成IOC服务
一、注入IOC容器
- 注入IOC容器需要安装nuget包PocoEmit.ServiceProvider
1. 包含IOC容器的实体
class UserWithServiceProvider
{
public int Id { get; set; }
public string Name { get; set; }
public IServiceProvider ServiceProvider { get; set; }
}
2. 注册、转化并注入的代码
- UseSingleton是把容器作为唯一容器注入
- UseScope是使用当前Scope的子容器
- UseContext是在Mvc下,使用当前HttpContext的RequestServices子容器
var services = new ServiceCollection();
var serviceProvider = services.BuildServiceProvider();
var mapper = Mapper.Create();
mapper.UseSingleton(serviceProvider);
var dto = new UserDTO { Id = 1, Name = "Jxj" };
UserWithServiceProvider user = mapper.Convert<UserDTO, UserWithServiceProvider>(dto);
Assert.NotNull(user.ServiceProvider);
二、当然还可以注入容器内的服务
1. UserDomain多出来的UserDomain需要注入
- 这次我们用IOC来管理UserRepository
- 这样才能更好的利用依赖注入
- UserRepository可能还会依赖其他的
- 手动维护对象可能会很麻烦,IOC容器擅长维护这些复杂关系
class UserDomain(UserRepository repository, int id, string name)
{
private readonly UserRepository _repository = repository;
public UserRepository Repository
=> _repository;
public int Id { get; } = id;
public string Name { get; } = name;
// ...
}
class UserRepository
{
void Add(UserDomain user) { }
void Update(UserDomain entity) { }
void Remove(UserDomain entity) { }
}
2. 注册、转化并注入的代码
- 通过UseScope注入IOC容器
- 通过UseDefault告知这个类型从IOC容器中注入
var services = new ServiceCollection()
.AddScoped<UserRepository>();
var serviceProvider = services.BuildServiceProvider();
var mapper = Mapper.Create();
mapper.UseScope(serviceProvider)
.UseDefault<UserRepository>();
var dto = new UserDTO { Id = 1, Name = "Jxj" };
UserDomain user = mapper.Convert<UserDTO, UserDomain>(dto);
Assert.NotNull(user.Repository);
三、支持IOC容器的特性
- 支持FromKeyedServices
- 支持FromServices
1. FromKeyedServices标记注入点和服务键
class UserDomain1([FromKeyedServices("User1")]UserRepository repository, int id, string name)
: UserDomain(repository, id, name)
{
}
class UserDomain2([FromKeyedServices("User2")] UserRepository repository, int id, string name)
: UserDomain(repository, id, name)
{
}
class UserDomain(UserRepository repository, int id, string name)
{
private readonly UserRepository _repository = repository;
public UserRepository Repository
=> _repository;
public int Id { get; } = id;
public string Name { get; } = name;
// ...
}
class UserRepository(string tableName)
{
private readonly string _tableName = tableName;
public string TableName
=> _tableName;
void Add(UserDomain user) { }
void Update(UserDomain entity) { }
void Remove(UserDomain entity) { }
}
2. 注册、转化并注入的代码
- 由于识别出FromKeyedServices,就不需要UseDefault
- 这样简洁由优雅
string table1 = "User1";
string table2 = "User2";
var services = new ServiceCollection()
.AddKeyedScoped(table1, (_, _) => new UserRepository(table1))
.AddKeyedScoped(table2, (_, _) => new UserRepository(table2));
var serviceProvider = services.BuildServiceProvider();
var mapper = Mapper.Create();
mapper.UseScope(serviceProvider);
var dto = new UserDTO { Id = 1, Name = "Jxj" };
UserDomain user = mapper.Convert<UserDTO, UserDomain1>(dto);
Assert.NotNull(user.Repository);
UserDomain user2 = mapper.Convert<UserDTO, UserDomain2>(dto);
Assert.NotNull(user2.Repository);
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. 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 is compatible. 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 is compatible. 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 is compatible. 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 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
| .NET Framework | net461 was computed. net462 was computed. 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.
-
.NETStandard 2.0
- Hand.AutoCache (>= 0.2.1-alpha)
- Hand.Core (>= 0.2.1-alpha)
- Hand.Reflection (>= 0.2.0-alpha)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- PocoEmit (>= 0.8.1.2-alpha)
- PocoEmit.Builders (>= 0.8.1.2-alpha)
- PocoEmit.Mapper (>= 0.8.1.2-alpha)
-
.NETStandard 2.1
- Hand.AutoCache (>= 0.2.1-alpha)
- Hand.Core (>= 0.2.1-alpha)
- Hand.Reflection (>= 0.2.0-alpha)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- PocoEmit (>= 0.8.1.2-alpha)
- PocoEmit.Builders (>= 0.8.1.2-alpha)
- PocoEmit.Mapper (>= 0.8.1.2-alpha)
-
net10.0
- Hand.AutoCache (>= 0.2.1-alpha)
- Hand.Core (>= 0.2.1-alpha)
- Hand.Reflection (>= 0.2.0-alpha)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- PocoEmit (>= 0.8.1.2-alpha)
- PocoEmit.Builders (>= 0.8.1.2-alpha)
- PocoEmit.Mapper (>= 0.8.1.2-alpha)
-
net8.0
- Hand.AutoCache (>= 0.2.1-alpha)
- Hand.Core (>= 0.2.1-alpha)
- Hand.Reflection (>= 0.2.0-alpha)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- PocoEmit (>= 0.8.1.2-alpha)
- PocoEmit.Builders (>= 0.8.1.2-alpha)
- PocoEmit.Mapper (>= 0.8.1.2-alpha)
-
net9.0
- Hand.AutoCache (>= 0.2.1-alpha)
- Hand.Core (>= 0.2.1-alpha)
- Hand.Reflection (>= 0.2.0-alpha)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- PocoEmit (>= 0.8.1.2-alpha)
- PocoEmit.Builders (>= 0.8.1.2-alpha)
- PocoEmit.Mapper (>= 0.8.1.2-alpha)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on PocoEmit.ServiceProvider:
| Package | Downloads |
|---|---|
|
PocoEmit.Mvc
Mapper支持Mvc注入 |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.8.1.3-alpha | 601 | 12/3/2025 |
| 0.8.1.2-alpha | 604 | 12/3/2025 |
| 0.8.1-alpha | 102 | 11/28/2025 |
| 0.8.0-alpha | 179 | 11/16/2025 |
| 0.7.0-alpha | 83 | 10/3/2025 |