Posts tagged with 'c'
OnActionExecuted method in an action filter will be run after the controller action it is filtering is executed. The context object that gets passed in of type ActionExecutedContext, which is very similar to ActionExecutingContext in the types of information you can get with these exceptions:
- There is no ActionParameters dictionary property.
- You will get some information related to an unhandled exception:
- Exception - the exception that was thrown by the action
- ExceptionHandled - set this to true if you've handled the exception in OnActionExecuted (otherwise it will be bubbled up)
- Canceled - this can get a bit confusing, but if the process was canceled by setting a different result in OnActionExecuting this will be set to 'true', or you can set this to 'true' yourself to cancel further processing. For more info, see this post on how MVC handles filters and this discussion on the Canceled property.
I did another "kitchen sink" view of an ActionExecutedContext object:
Paired with a slightly different View, which results in:
Note that one of the most interesting things you can do is to change the result of the action (like I did to make the Kitchen Sink example possible). For instance, if you wanted to redirect to another action, show a different view, etc,
I continue to try and cut through some of the confusion around AOP by providing simple definitions and examples of often obtuse terminology (see also: other terminology posts).
Today's term: advice.
An advice is simply the code that could be executed at a pointcut. For instance, if you want to do some logging, then your advice is the code that performs the logging. If you are wrapping methods inside of a transaction, then your advice is whatever implementation of begin/commit/rollback you are using. And so on...
Here's a quick example of a tracing aspect I borrowed from the PostSharp site:
There's two advices in that aspect:
- One to write "Entering" and the method name to the trace, using Trace.TraceInformation(...)
- One to write "Exiting" and the method name to the trace, using Trace.TraceInformation(...)
Those are both very trivial advices, of course. A more complex advice could reference services, perform logic, use a lot more context information than just method name, etc.
So now that you know what a join point, a pointcut, and an advice is, you have all the puzzle pieces. The final step is to put them all together. And guess what it's called when you put them all together? (spoiler alert: it's the "A" in "AOP")
PostSharp 2.1 SP 1 is now released. This is pretty much a maintenance release, addressing bugs.
There are some new features, including integration with decompiler tools (dotPeek, ILSpy, and Reflector are supported).
What's intriguing is the new features for and allusions to the PostSharp Toolkit, which could be a very interesting release from SharpCrafters.
Anyway, PostSharp is perhaps the most popular and stable tool for AOP in .NET. I'm not saying it's the end-all-be-all, but if you are interested in AOP for .NET, PostSharp is where I think you should start. You can download 2.1 SP1 (aka 2.1.6.4) at SharpCrafters.com, or just do it the easy way and get it via NuGet.
Thanks to Scott Hanselman, John Sonmez, and Pluralsight, you can get a free week of Pluralsight video training.
But Pluralsight has so much content; where to begin?
- Since this is an AOP blog, obviously I'm going to recommend Donald Belcham's Aspect Oriented Programming in .NET. (Donald Belcham is a fellow Postsharp MVP).
- Speaking of fellow Postsharp MVP's, Dustin Davis's T4 Templates course is another good choice. T4 is a powerful tool, and even though code generation is sometimes blamed for some coding atrocities, there is a right way and wrong way to do it, and lots of great use-cases that can make your application easier to maintain.
- I've watched a few MVC Pluralsight courses from Scott Allen. And since I've been talking about MVC ActionFilters, I think it would also be worthwhile to watch ASP.NET MVC 3.0 Fundamentals.
Anything else you'd recommend at Pluralsight?
Week #2 is coming to an end here at CrossCuttingConcerns, which means it's time for another link dump post.
- LIDNUG (Linked-in .NET user group) recently posted video of Gael Fraiteur at their user group meeting. This was recorded in November 2010, so it's a bit behind the times as far as PostSharp goes, but it's still an informative video.
- Here's a white paper comparing AOP to composition filters (CF). I've not heard of CF before this paper, but it seems that like AOP, CF aims to address the same limitations with cross-cutting concerns that AOP does. (PDF link)
- Taking the Single Responsibility Principle (SRP) seriously. A deep dive into SRP, and just what is a "responsibility" anyway?
- Another white paper, but this one is about an interesting use case of AOP. It's based on a project to "trace" an application: not in the traditional developer sense of tracing through the code, but rather tracing a user's actions as she uses the interface to a system. And then using that information for later analysis (perhaps a usability study?).
- A blog post from Kristijan Rebernišak about using AOP to check user permissions: he uses Google Guice with AOPAlliance.
- Chad England has started a blog post series on AOP with PostSharp.
Thanks for reading, and please leave a comment or use the contact link above if there's something you'd like to see covered here on CrossCuttingConcerns.com (or if you have something to say and want it published here).