Contexts Presented by Ramaswamy Krishnan Chittur Contexts Contents
Contexts Presented by: Ramaswamy Krishnan Chittur Contexts
Contents 1] App. Domain 2] Contexts 3] Context Attributes 4] Context Properties 5] Context sinks 6] Reflection 7] Contexts and Interception 8] Sample code 9] Reference Contexts 2
1] App. Domain Many programming technologies and environments define their own unique models for scoping the execution of code and the ownership of resources: -- For Java VM, it is based on Class loaders -- For IIS and ASP, the scoping model is Virtual directory. -- For the CLR, the fundamental scope is an App. Domain. [1] Contexts 3
1] App. Domain n App. Domains are divided into one or more contexts. Contexts 4
2] Contexts n n Every CLR application is divided into one or more contexts. Contexts are themselves objects that are instances of System. Runtime. Remoting. Contexts. Context type. Contexts help to group together objects that have similar execution requirements. Normally, the runtime creates contexts as needed. Contexts 5
2] Contexts: cross-context architecture n Cross-context member access Ø is message – based. supports interception. is completely pluggable. Ø Ø Contexts 6
2] Contexts: cross-context access n Cross-Context access requires a proxy Contexts 7
2] Contexts: cross-context access n Objects in an App. Domain are either Context-agile or Context-bound. Contexts 8
2] Contexts: Context-agile objects n n n Context-agile objects can be accessed directly from anywhere in an App. Domain. Types derived from Marshal. By. Ref. Object are contextagile. ‘Context-agile’ implies pass-by-ref, if between App. Domains. Contexts 9
2] Contexts: Context-bound objects n n n Context-bound objects are accessed through proxies from outside the home context. Types derived from Context. Bound. Object are contextbound. Context-bound implies pass-by-ref between contexts. Contexts 10
3] Context Attributes n n provide a facility to explicitly request contextbased services are instantiated at the runtime, just before the type that is using it is instantiated. Participate in the decision to create a new context ( or not) At instantiation time, install context properties and context sinks to provide service. Contexts 11
4] Context Properties n n Provide on-demand services to the developers. Can contribute the instantiation of interceptors (sinks), that can intercept the messages intended to the target object. Can be programmatically accessed from within a type’s methods. Provide an interface that allows developers to control sink behavior. Contexts 12
5] Context Sinks (Interceptors) n n n The context-properties can be programmed to provide sinks, on-demand. Sinks provide services by intercepting calls into and out of the context. . NET supports context-wide and object-specific interception. Method calls on context-bound object types are converted into messages that pass through sinks, where interception can be employed if needed. By employing interception at the sinks, we can modify the messages, or can even create a returnvalue thereby “short-circuiting” the method call. Contexts 13
6] Reflection n A powerful feature of. NET, which Ø Provides access to type-metadata. Supports late-binding. Provides facility to create types at run-time! Ø Ø Contexts 14
7] Contexts and Interception Contexts 15
7] Contexts and Interception Contexts 16
8] Sample code: custom attribute class // made applicable to classes, interfaces and structures only. [Attribute. Usage( Attribute. Targets. Class | Attribute. Targets. Struct | Attribute. Targets. Interface)] public class Debugger. Context. Attribute : Context. Attribute { public Debugger. Context. Attribute() : base ("Debugger. Interception") { } public override bool Is. Context. OK(Context M 1, IConstruction. Call. Message M 2) { return false; } public override void Get. Properties. For. New. Context(IConstruction. Call. Message M) { My. Constructor. Context. Properties. Add(new Debugger. Context. Property()); } } Contexts 17
8] Sample code: custom property class public class Debugger. Context. Property : IContext. Property, IContribute. Object. Sink { public bool Is. New. Context. OK (Context My. New. Context) { return true; } public void Freeze (Context My. New. Context) { } public string Name { get { return "Interception"; } } public IMessage. Sink Get. Object. Sink(Marshal. By. Ref. Object obj, IMessage. Sink N) { return new Debugger. Sink ( N ); } } Contexts 18
8] Sample code: custom sink class public class Debugger. Sink : IMessage. Sink { private IMessage. Sink _next. Sink; public Debugger. Sink(IMessage. Sink next. Sink) { _next. Sink = next. Sink; } We may choose to do custom processing on the method call over here. public IMessage Sync. Process. Message(IMessage msg) { return _next. Sink. Sync. Process. Message(msg); } public IMessage. Ctrl Async. Process. Message(IMessage msg, IMessage. Sink reply. Sink) { return _next. Sink. Async. Process. Message(msg, reply. Sink); } public IMessage. Sink Next. Sink { get { return _next. Sink; } } } Contexts 19
8] Sample code: using Reflection Exposing metadata via Reflection Contexts 20
8] Sample code: Client The Custom Attribute that we developed to enable interception. The client class has to derive from this class to be context bound, thus enabling interception when operating between contexts. [Debugger. Context ( )] public class My. Client : System. Context. Bound. Object { // class implementation } Contexts 21
9] Reference 1. 2. 3. 4. 5. 6. http: //msdn. microsoft. com/msdnmag/issues/02/03/AOP Dharma Shukla, Simon Fell, and Chris Sells Essential. NET, volume 1 - Don Box, Chris Sells. Context – Mike Woodring. Advanced. NET Remoting - Ingo Rammer. MSDN documentation. Microsoft. NET Remoting - Scott Mc. Lean, James Naftel, Kim Williams. 7. 8. Remoting with C# and. NET - David Conger. Visual C#. NET – Jason Price, Mike Gunderloy. Contexts 22
- Slides: 22