4

2 Questions:

  1. Are Action Filters in MVC considered Aspect Oriented Programming (AOP)?
  2. If so, does .NET provide something similar for non MVC code (i.e. regular class library?)

The context is I want to add logging to a application. Instead of sprinkling logging code all over the place, I like the idea of being able to define rules for logging in one central location.

It seems that I can achieve this using an AOP technology (PostSharp). However, if there is something build in to .NET I would probably end up using that.

hatcyl
  • 2,190
  • 2
  • 21
  • 24
  • If you use DI your container may support interception on method level - so you can add logging to many methods without touching any code. I.e. for Unity check http://stackoverflow.com/questions/11186918/how-to-configure-a-logging-interceptor-for-all-registered-type-in-unity – Alexei Levenkov Jan 04 '14 at 16:58

1 Answers1

12

Generally, AOP is a programming paradigm and has nothing to do with specific technologies like MVC or PostSharp in the first place. This said: Yes, Action Filters are an incarnation of this paradigm.

In plain .NET, AOP can be done by using custom attributes and reflection (meaning that the entire work is done at runtime). PostSharp on the contrary is an aspect weaver and does its magic at compile time, modifying the IL output of the C# compiler. Therefore, no runtime performance penalty whatsoever will be involved. Moreover, PostSharp has many things (like e.g. logging) pre-built out of the box, and it is much more type-safe and very extensible.

You can use plain .NET. But this would be the same as preferring a stagecoach over a racing car.

Thomas Weller
  • 11,631
  • 3
  • 26
  • 34
  • Great, thanks for the answer, it is exactly what I was looking for. I will be using PostSharp. + Thanks for telling me how you would theoretically do it in plain .NET. – hatcyl Jan 04 '14 at 08:22