- VisualStudio2022插件的安装及使用-编程手把手系列文章
- pprof-在现网场景怎么用
- C#实现的下拉多选框,下拉多选树,多级节点
- 【学习笔记】基础数据结构:猫树
肉夹馍(https://github.com/inversionhourglass/Rougamo),一款编译时AOP组件,无需在应用启动时进行初始化,也无需繁琐的配置;支持所有种类方法(同步和异步、静态和实例、构造方法/属性/普通方法);提供了简单易上手的Attribute应用方式,同时还提供了类AspectJ表达式的批量应用规则.
在 4.0版本发布的文章评论 中,有朋友反馈了一个调试时无法查看方法内部变量值的问题。本次更新就是修复这个问题的,4.0.1不包含其他修改,对调试时禁用肉夹馍的朋友没有任何影响,可以酌情升级.
4.0.1本来是不准备发博客的,内容一句话就结束了,不过又想到前段时间还发布了IoC扩展,索性就合在一起写一篇博客吧.
各位在使用肉夹馍时,最常遇到的问题可能就是如何与IoC交互了。现在主流的动态代理本身就需要IoC才能完成,所以动态代理在IoC交互方面具有天然的优势,而肉夹馍编译时完成不依赖IoC,所以与IoC的交互也不是很方便。但不方便并不是不能。此前已经有朋友在自己的项目中实现了IoC的访问,比如Rougamo.OpenTelemetry, FreeSql。考虑到IoC的使用在现在已经非常普遍,所以新增了几个常用IoC的扩展包.
目前只对最常用的两个IoC组件提供了支持,一个是微软官方的Microsoft.Extensions.DependencyInjection,另一个是Autofac,主要包含四个NuGet:
Rougamo.Extensions.DependencyInjection.AspNetCore
Rougamo.Extensions.DependencyInjection.GenericHost
Rougamo.Extensions.DependencyInjection.Autofac.AspNetCore
Rougamo.Extensions.DependencyInjection.Autofac
其中AspNetCore结尾的两个NuGet专用于AspNetCore(废话了哦),另外两个NuGet用于通用主机(Generic Host)和Framework等场景.
在引用这些NuGet包时,你会发现他们都包含很多个版本,这并不是版本迭代更新快或者版本号设置错了导致的,版本号有相应的规则,它们的主版本号跟随对应IoC组件的NuGet主版本号。微软官方的两个扩展包的主版本号跟随Microsoft.Extensions.*的主版本号(也是.NET SDK的版本),Autofac的两个扩展包的主版本号跟随Autofac的主版本号.
下面直接用代码快速展示如何使用对应的扩展包.
// 注册Rougamo(注:如果你不使用IoC/DI功能,Rougamo默认是不需要注册操作的)
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// ...省略其他步骤
builder.Services.AddRougamoAspNetCore();
// ...省略其他步骤
}
// 在切面类型中获取IServiceProvider实例并使用
public class TestAttribute : MoAttribute
{
public override void OnEntry(MethodContext context)
{
// 使用扩展方法GetServiceProvider获取IServiceProvider实例
var services = context.GetServiceProvider();
// 使用IServiceProvider
var xxx = services.GetService<IXxx>();
}
}
// 注册Rougamo(注:如果你不使用IoC/DI功能,Rougamo默认是不需要注册操作的)
public static void Main(string[] args)
{
var builder = Host.CreateDefaultBuilder();
// ...省略其他步骤
builder.ConfigureServices(services => services.AddRougamoGenericHost());
// ...省略其他步骤
}
// 在切面类型中获取IServiceProvider实例并使用
public class TestAttribute : MoAttribute
{
public override void OnEntry(MethodContext context)
{
// 使用扩展方法GetServiceProvider获取IServiceProvider实例
var services = context.GetServiceProvider();
// 使用IServiceProvider
var xxx = services.GetService<IXxx>();
}
}
// 注册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(注:如果你不使用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项目以及WinForm、WPF等项目可能并没有使用通用主机(Generic Host),此时使用Rougamo.Extensions.DependencyInjection.Autofac将更加直接,初始化时创建ContainerBuilder后直接调用RegisterRougamo扩展方法即可.
var builder = new ContainerBuilder();
builder.RegisterRougamo();
肉夹馍IoC/DI扩展更多的信息请访问 Rougamo.DI (https://github.com/inversionhourglass/Rougamo.DI),欢迎反馈建议和提交PR. 。
最后此篇关于肉夹馍(Rougamo)4.0.1异步方法变量调试修复与IoC系列扩展的文章就讲到这里了,如果你想了解更多关于肉夹馍(Rougamo)4.0.1异步方法变量调试修复与IoC系列扩展的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
最近在看项目,看到别人使用Rougamo框架,好奇花了点时间仔细研究了,在这里记录一下。 0. 静态编织 Aop 首先,我们先了解什么是Aop? Aop 是指面向切面编程 (Aspect
我是一名优秀的程序员,十分优秀!