Castle.DynamicProxy.Extensions
基于Castle.DynamicProxy的AOP框架
Install / Use
/learn @softlgl/Castle.DynamicProxy.ExtensionsREADME
Castle.DynamicProxy.Extensions
基于Castle.DynamicProxy的扩展支持Filter方式AOP
使用方式
控制台程序
IServiceCollection services = new ServiceCollection();
services.AddTransient<IFoo, Foo>()
.AddSingleton(new FooConfig { Name = "redis" });
//使用BuildCastleDynamicProxyProvider
IServiceProvider serviceProvider = services.BuildCastleDynamicProxyProvider();
IFoo foo = serviceProvider.GetService<IFoo>();
bool addFlag = foo.Add(new FooModel { Id =1, Name = "test",Date = DateTime.Now });
foo.Delete(1);
FooModel fooModel = foo.Get(1);
Web程序
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.UseServiceProviderFactory(new CastleDynamicProxyServiceProviderFactory());
定义拦截
public class CacheAttribute : AbstractInterceptorAttribute
{
[FromServices]
public FooConfig FooConfig { get; set; }
public override async Task InvokeAsync(AspectContext context, AspectDelegate next)
{
Debug.WriteLine($"CacheAttribute执行前,param.length=[{context.Parameters.Length}],FooModel.Name=[{FooConfig.Name}]");
await next(context);
Debug.WriteLine($"CacheAttribute执行后,returnValue=[{context.ReturnValue}]");
}
}
业务类
public interface IFoo
{
bool Add(FooModel fooModel);
FooModel Get(int id);
void Delete (int id);
}
[Logger]
public class Foo : IFoo
{
private readonly IBar _bar;
public Foo(IBar bar)
{
_bar = bar;
}
public bool Add(FooModel fooModel)
{
_bar.Add(fooModel);
return true;
}
public void Delete(int id)
{
}
[Limit]
[Cache]
public FooModel Get(int id)
{
_bar.Get(id);
return new FooModel { Id = id, Name = "foo" + id, Date = DateTime.Now };
}
}
Related Skills
node-connect
341.6kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
84.6kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
341.6kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
84.6kCommit, push, and open a PR
