views:

117

answers:

2

Recently I've started learning about Contexts in .NET (context-bound, context-agile, message sinks, etc.). Several alarm bells started ringing:

  1. All context-bound classes are derived from ContextBoundObject, which in turn is derieved from MarshalByRefObject. MarshalByRefObject is a part of the .NET Remoting architecture. The .NET Remoting architecture is considered obsolete.
  2. Most .NET-related books pay little to no attention to the concept of Contexts.
  3. MSDN barely provides any information on Contexts: classes are explained, but sattelite-articles (such as "How To"s, "Overview"s, "Tutorials" etc.) are not provided.
  4. Most articles and forums threads discussing Contexts are a few years old.
  5. Aside from the Synchronization context, it seems that the .NET framework makes little-to-no use of the Contexts architecture.

Thus, my question is: Is the concept of Contexts in .NET obsolete and should be avoided in new developments? If it is obsolete, what alternatives are recommended?

+3  A: 

Yes, avoid MarshalByRefObject and ContextBoundObject for AOP.

I rolled a custom AOP solution about four years ago using the context-bound MarshalByRefObject method. I knew at the time I was going down a rabbit hole but did it anyway because many of the AOP frameworks for .NET were still very young.

The fact that .NET supports AOP with MarshalByRefObject is a happy accident. MBO was never intended for that purpose, and using MBO for AOP is a hijacking of the original intent of .NET remoting and MBO.

None of this means that .NET can't support AOP, just that it should be done in a less "hacky" way, using a framework that supports AOP, like Spring.NET, PostSharp, and others.

Here is a good, if a little dated, article on AOP frameworks for .NET.

Dave Swersky
Dave, thank you very much for your strict answer. Yo've prolly saved me many hours of study, design and implementation. It always makes me a bit sad to see good ideas implemented poorly inside the framework, and having to use 3rd party frameworks to cover the gaps, but I shall adhere to your advice.
M.A. Hanin
I don't personally hold to the idea that .NET is somehow deficient because it doesn't "natively" support AOP, so I wouldn't limit consideration of AOP frameworks only to Unity. The .NET remoting bits were never originally designed to support AOP. AOP is, in most cases, a programming technique/convention layered on top of frameworks (like .NET) that are capable of supporting it.
Dave Swersky
I agree, my point is that Contexts are a nice idea, and if it wasn't bound to the MarshalByRef/Remoting architecture, I would have used it.
M.A. Hanin
+2  A: 

I stand to be corrected but if you are restricted to using "Vendor" (re:Microsoft) code, I believe there is a degree of AoP in Unity and/or the MS Enterprise Library Policy block.

Doobi
+1 This is true.
Dave Swersky
Very true, both the Unity and Enterprise libraries contain AoP-mechanisms. I've found that through Dave's link, and I'm currently investigating both libraries to determine which better suits my needs. You're correct about my (psychological) restriction to vendor code. +1, your answer is appreciated.
M.A. Hanin