开发者

One framework for Dependency injection (in MVC app) and for simple AOP task (using attributes)

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. 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
    }
    

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.

I have some additional requirements:

  • small footprint, 1 or 2 assemblies
  • integration to VS - I just want to press Ctr开发者_如何转开发l+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 ?


EDIT: 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.

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

The following DI Containers support interception out of the box:

  • Castle Windsor
  • Unity
  • Spring.NET

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜