Skip to content

Rougamo dependency injection extension packages. Currently it supports Microsoft Dependency Injection and Autofac.

License

Notifications You must be signed in to change notification settings

inversionhourglass/Rougamo.DI

Folders and files

NameName
Last commit message
Last commit date
Dec 14, 2024
Sep 18, 2024
Dec 14, 2024
Dec 7, 2024
Aug 13, 2024
Dec 14, 2024
Aug 13, 2024
Sep 18, 2024
Sep 18, 2024
Sep 18, 2024
Dec 14, 2024
Aug 16, 2024
Aug 16, 2024

Repository files navigation

Rougamo.DI

中文 | English

Rougamo.DI提供了一系列 Rougamo(https://github.com/inversionhourglass/Rougamo) 的IoC/DI扩展,使你在使用 Rougamo 时获得更好的IoC/DI交互体验。

当前扩展一览

包名 用途
Rougamo.Extensions.DependencyInjection.Microsoft 使用官方DependencyInjection,结合当前HttpContext,返回正确scope的IServiceProvider
Rougamo.Extensions.DependencyInjection.Autofac.AspNetCore 使用Autofac,结合当前HttpContext,返回正确scope的ILifetimeScope
Rougamo.Extensions.DependencyInjection.Autofac 使用Autofac,适用于非AspNetCore项目
Rougamo.Extensions.DependencyInjection.Abstractions 基础抽象包
Rougamo.Extensions.DependencyInjection.AspNetCore.Abstractions AspNetCore基础抽象包

版本号说明

所有版本号格式都采用语义版本号(SemVer)

  1. 两个基础抽象包的版本从1.0.0开始增加
    • Rougamo.Extensions.DependencyInjection.Abstractions
    • Rougamo.Extensions.DependencyInjection.AspNetCore.Abstractions
  2. 微软官方DI扩展包,主版本号与官方包(Microsoft.Extensions.*)相同
    • Rougamo.Extensions.DependencyInjection.Microsoft
  3. Autofac扩展包,主版本号与官方包(Autofac)相同
    • Rougamo.Extensions.DependencyInjection.Autofac.AspNetCore
    • Rougamo.Extensions.DependencyInjection.Autofac

Rougamo.Extensions.DependencyInjection.Microsoft

快速开始

Rougamo.Extensions.DependencyInjection.Microsoft依赖于DependencyInjection.StaticAccessor,启动项目初始化部分通过DependencyInjection.StaticAccessor完成,不同类型项目的初始化方式请跳转DependencyInjection.StaticAccessor查看

启动项目引用DependencyInjection.StaticAccessor.Hosting

dotnet add package DependencyInjection.StaticAccessor.Hosting

非启动项目引用Rougamo.Extensions.DependencyInjection.Microsoft

dotnet add package Rougamo.Extensions.DependencyInjection.Microsoft

// 注册Rougamo(注:如果你不使用IoC/DI功能,Rougamo默认是不需要注册操作的)
public static void Main(string[] args)
{
    // 1. 初始化。这里用通用主机进行演示,其他类型项目请查看DependencyInjection.StaticAccessor项目的readme
    var builder = Host.CreateDefaultBuilder();

    builder.UsePinnedScopeServiceProvider(); // 仅此一步完成初始化

    var host = builder.Build();

    host.Run();
}

// 在切面类型中获取IServiceProvider实例并使用
public class TestAttribute : MoAttribute
{
    public override void OnEntry(MethodContext context)
    {
        var xxx = context.GetService<IXxx>();
    }
}

Rougamo.Extensions.DependencyInjection.Autofac.AspNetCore

快速开始

// 注册Rougamo(注:如果你不使用IoC/DI功能,Rougamo默认是不需要注册操作的)
public static void Main(string[] args)
{
    var builder = WebApplication.CreateBuilder(args);
    builder.Host
            .UseServiceProviderFactory(new AutofacServiceProviderFactory())
            .ConfigureContainer<ContainerBuilder>(builder =>
            {
                builder.RegisterRougamoAspNetCore();
            });
    
    // 注册IHttpContextAccessor也是必须的
    builder.Services.AddHttpContextAccessor();
}

// 在切面类型中获取ILifetimeScope实例并使用
public class TestAttribute : MoAttribute
{
    public override void OnEntry(MethodContext context)
    {
        // 使用扩展方法GetAutofacCurrentScope获取ILifetimeScope实例
        var scope = context.GetAutofacCurrentScope();

        // 使用ILifetimeScope
        var xxx = scope.Resolve<IXxx>();
    }
}

Rougamo.Extensions.DependencyInjection.Autofac

// 注册Rougamo(注:如果你不使用IoC/DI功能,Rougamo默认是不需要注册操作的)
public static void Main(string[] args)
{
    var builder = Host.CreateDefaultBuilder();
    
    builder
        .UseServiceProviderFactory(new AutofacServiceProviderFactory())
        .ConfigureContainer<ContainerBuilder>(builder =>
        {
            builder.RegisterRougamo();
        });
}

// 在切面类型中获取IServiceProvider实例并使用
public class TestAttribute : MoAttribute
{
    public override void OnEntry(MethodContext context)
    {
        // 使用扩展方法GetAutofacCurrentScope获取ILifetimeScope实例
        var scope = context.GetAutofacCurrentScope();

        // 使用ILifetimeScope
        var xxx = scope.Resolve<IXxx>();
    }
}

在Framework项目中使用

如果你的项目是老的WebForm或WinForm、WPF等项目,没有使用Microsoft.Extensions.*系列包,那么在应用初始化创建ContainerBuilder时直接调用扩展方法RegisterRougamo即可。

var builder = new ContainerBuilder();
builder.RegisterRougamo();

About

Rougamo dependency injection extension packages. Currently it supports Microsoft Dependency Injection and Autofac.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages