C# – Would aspect oriented programming be a positive addition to the C# language

aopcnetprogramming-languages

I've had some interesting debates with colleagues about the merits if incorporating aspect oriented programming as a native paradigm to the C# language.

The debate seems to be divided into three camps:

  1. Those folks who think that C# is already too complicated as it is, and another major feature like AOP would only muddy the waters further.
  2. Those who think that it would be a great addition because anything that can increase the expressiveness of the language without breaking existing is a good thing.
  3. Those who don't think it's necessary because libraries like PostSharp that perform post-compilation IL weaving already allow it in a language neutral way.

I'm curious what the community of C#/.NET developers out there think.

Best Answer

It would be great if languages would make it easier to develop and use AOP extensions.

For instance:

  • It would be nice if one could give a delegate (or anonymous method, or lambda) as a parameter to a custom attribute. It's not a lot of work to implement this in C#, it's quite easy to implement it in the CLR (since it supports types, why not methods?). And it would allow to express 'pointcuts' in an elegant way.

  • Support for 'fieldof' and 'methodof'. It is somewhat supported by the CLR (with bugs), not by C#. The same for 'eventof' and 'propertyof' (they have currently no support in the CLR).

  • Better debugging symbols could make it easier for an aspect weaver to report error messages and give the location in code.

  • It would be great to have a modular compiler; it would be less expensive to implement some features like source code generation based on aspects (for method and interface introductions).

That said, I don't think that the language should provide AOP extensions. This is too large (I think PostSharp 2.0 is more complex than the C# compiler itself, at least than C# 2.0). Let's face it: AOP is still rather experimental in the sense that we still don't know exactly what we want from it. There is still little experience. But we want the specification of a language to be stable and to address well-understood problems (imagine the Entity Framework were a part of the language).

Additionally, there are different ways to achieve AOP, and build-time is only one of them. There is nothing wrong in using runtime technologies, like JIT-emitted proxies (Spring/Castle); these are just for different use cases and have their own pros and cons.

So my opinion in one sentense: yes for limited and well-defined language extensions that make it easier to develop AOP frameworks; no for a full AOP implementation in the language.

Related Topic