一种用于依赖注入(在 MVC 应用程序中)和用于简单 AOP 任务(使用属性)的框架 [英] One framework for Dependency injection (in MVC app) and for simple AOP task (using attributes)

查看:19
本文介绍了一种用于依赖注入(在 MVC 应用程序中)和用于简单 AOP 任务(使用属性)的框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 ASP.NET 应用程序中完成两个任务,这两个任务都可以使用一些 AOP 框架来完成,但它们的性质不同:

I have come to the point where I want to accomplish 2 tasks in my ASP.NET application that both can be done using some AOP framework but they are both of different nature:

  1. 我的控制器和服务的依赖注入
  2. 我创建了自定义属性 NotNullAttribute 并用它标记了一些方法的参数或属性,而不是在这些参数或属性为 null 时抛出 ArgumentNullException.我希望 AOP 框架在编译时检查这些属性并添加抛出原因.示例如下:

  1. Dependency injection for my controllers and services
  2. I have created custom attribute NotNullAttribute and marked some method's parameters or properties with it instead of of throwing ArgumentNullException if those parameters or properties were null. I want AOP framework to check for those attributes in compile time and add throw causes instead. Example follows:

//original code that I write
public void MyMethod([NotNull]string param1){
    //do something
}

.

//code actually being compiled - after AOF processing/weaning  
public void MyMethod(string patam1){
    if(param1 == null){
        throw new ArgumentNullException("param1");
    }
    //do something
}

所以我想要框架(甚至不一定是 AOP,但我想它必须是)可以让我简单地完成这两项任务.

So I want framework (does not even have to be AOP necessarily but I guess it would have to be) that will allow me to do both those task simply.

我有一些额外的要求:

  • 占地面积小,1 或 2 个组件
  • 与 VS 的集成 - 我只想按 Ctrl+F5 进行编译,并且框架可以运行,注入依赖项,添加异常抛出代码 我什至不知道它.我不想从命令行或类似的东西运行预编译.
  • 对于异常抛出代码生成,我想编写类.喜欢常规方面.不是 XML,没有配置(约定是可以接受的).对于依赖注入,我也更喜欢类,但 XML 或其他配置文件是可以接受的,但它应该足够简单,可供不真正了解 XML 并且不太喜欢它的人使用.
  • small footprint, 1 or 2 assemblies
  • integration to VS - I just want to press Ctrl+F5 to compile and the framework does it work, injects dependencies, adds exception throwing code without me even knowing about it. I don't want to run pre-compilation from command line or anything like that.
  • for exception throwing code generation I'd like write classes. Like regular aspects. Not XML, no configuration (conventions are acceptable). For dependency injection I also prefer classes but XML or another config file is acceptable but it should be simple enough to use by somebody who don't really know XML and don't really like it.

有这样的框架吗?如果有更多,它们的优点/缺点是什么?

Is there some such framework ? If there are more what are they pros/cons ?

哦,是的,我忘记了一个非常重要的想法:该框架应该是免费的.

Oh yes I forgot very important think: The framework should be for free.

推荐答案

我没有任何个人经验,但我认为 临府 符合您的描述.

I don't have any personal experience with it, but I think Linfu fits your description.

除此之外,您还可以通过动态拦截从 DI 容器中获得类似 AOP 的行为 - 下面是一个示例:http://blog.ploeh.dk/2010/09/20/InstrumentationWithDecoratorsAndInterceptors.aspx

Apart from that, you can get AOP-like behavior from DI Containers with dynamic interception - here's an example: http://blog.ploeh.dk/2010/09/20/InstrumentationWithDecoratorsAndInterceptors.aspx

以下 DI 容器支持开箱即用的拦截:

The following DI Containers support interception out of the box:

可以使用 PostSharp 获得纯 AOP(无 DI).

Pure AOP (without DI) can be had with PostSharp.

这篇关于一种用于依赖注入(在 MVC 应用程序中)和用于简单 AOP 任务(使用属性)的框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆