Vane.Container.Prism 1.0.0

dotnet add package Vane.Container.Prism --version 1.0.0
                    
NuGet\Install-Package Vane.Container.Prism -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="Vane.Container.Prism" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Vane.Container.Prism" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Vane.Container.Prism" />
                    
Project file
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 Vane.Container.Prism --version 1.0.0
                    
#r "nuget: Vane.Container.Prism, 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 Vane.Container.Prism@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=Vane.Container.Prism&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Vane.Container.Prism&version=1.0.0
                    
Install as a Cake Tool

Vane.Container

.NET 依赖注入容器。核心程序集只依赖 BCL,提供生命周期、keyed 服务、集合解析、作用域、AOP、装饰器与 MEF 形态的组件导出;另有编译期源生成器,以及 Prism 与 ASP.NET Core 适配工程。

版本 1.0.0。目标框架 net8.0 与 net10.0。

README 只给可执行的入口,概念与设计取舍见 Vane-DI-容器入门书.md。

安装

dotnet add package Vane.Container            # 核心容器
dotnet add package Vane.Container.Prism      # Prism 8 适配(net8.0)
dotnet add package Vane.Container.AspNetCore # HostBuilder 接线(net10.0)

SourceGen 随主包以 analyzers/dotnet/cs 资产分发,装包即生效,无需手工引用生成器。适配包只在目标框架匹配时才可用:Vane.Container.Prism 为 net8.0 资产,net10.0 应用同样可加载;Vane.Container.AspNetCore 为 net10.0,依赖 Microsoft.Extensions.* 10.0.0。适配包把主包作为普通依赖引入,analyzer 资产不随传递依赖生效,需要源生成器时请显式安装 Vane.Container。

源码仓库直接引用:

dotnet add reference src/Vane.Container/Vane.Container.csproj

快速开始

using Vane.Container;

var services = new ContainerBuilder();
services.AddSingleton<IClock, SystemClock>();
services.AddTransient<IGreeter, ChineseGreeter>();

var container = services.BuildMiniContainer();
var greeter = container.GetRequiredService<IGreeter>();

using var scope = container.CreateScope();
var session = scope.ServiceLocator.GetRequiredService<ISession>();

构造函数注入默认生效,无需特性。BuildMiniContainer() 返回 MiniContainer(完整能力),BuildServiceProvider() 返回 IServiceLocator(MS-DI 兼容视图),new MiniContainer(registry) 创建可动态注册的容器。

主要能力

  • 生命周期 Singleton / Transient / Scoped
  • 注入:[Inject](构造器、属性、字段、方法)、[InjectMany](集合)
  • 集合:T[] / IEnumerable<T> / IList<T> / ImmutableArray<T>,以及 Lazy<T> 与元数据视图
  • Keyed 服务、开放泛型、AOP、装饰器、实例生命周期观察者
  • 组件及组件成员(字段、属性、方法)导出与动态组合

适配工程

Prism

using Prism.Ioc;
using Vane.Container.Prism;

public partial class App : PrismApplication
{
    protected override IContainerExtension CreateContainerExtension()
        => VaneContainerExtension.CreateContainer();

    protected override Window CreateShell() => Container.Resolve<MainWindow>();

    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
        containerRegistry.RegisterSingleton<IMessage, Message>();
        containerRegistry.Register<IDialog, Dialog>("main");   // 命名注册映射为 keyed 服务
    }
}

Instance 属性暴露底层 ContainerBuilder,可与原生 API 混用。脱离 Prism 单独使用(样例、单测)必须自行调用一次 FinalizeExtension(),否则 Resolve 失败。

IContainerExtension extension = VaneContainerExtension.CreateContainer();
extension.RegisterSingleton<IMessage, Message>();
extension.FinalizeExtension();          // 缺这一步 Resolve 会失败
var message = extension.Resolve<IMessage>();

ASP.NET Core

using Vane.Container.AspNetCore;

var builder = WebApplication.CreateBuilder(args);
builder.Host.UseVaneContainer();

builder.Services.AddSingleton<IClock, SystemClock>();
builder.Services.AddScoped<IRequestId, RequestId>();

var app = builder.Build();
app.MapGet("/", (IClock clock, IRequestId id) => $"{clock.Now} / {id.Value}");
app.Run();

传统主机同样用 .UseVaneContainer(),自定义主机可直接传 new VaneServiceProviderFactory()。不要自行注册 IServiceProvider:它会遮蔽核心的作用域处理,让请求作用域拿到根实例。

两份适配的完整说明见入门书第十七章与第十八章。

构建与验证

dotnet build Vane.Container.sln -c Release
dotnet test tests/Vane.Container.Tests/Vane.Container.Tests.csproj -c Release
dotnet test tests/Vane.Container.PortedTests/Vane.Container.PortedTests.csproj -c Release

# 样例
dotnet run --project samples/Vane.Container.Samples -c Release
tests/Vane.Container.Tests         # 510 个 NUnit 用例
tests/Vane.Container.PortedTests   # 97 个 NUnit 端口一致性用例
tests/Vane.Container.Prism.Tests   # 控制台断言程序,不计入用例数

性能与驻留对比见 benchmarks/RESULTS.md。

文档

Product Compatible and additional computed target framework versions.
.NET 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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 0 11/16/2026