. Net Component Programming context

Source: Internet
Author: User

. Net Component Programming context

In the subsequent articles on remote calls, we have mentioned the application domain, which is the logic of a Large-granularity control assembly. Who will take the lead in controlling objects?
That's right. It's the context. CLR refined the application domain and added the context concept to the application, context is the existence of such a logic that has a set of constraints and is responsible for managing access to all objects in it.
For example, it may be a bit inappropriate, but I will understand it.
For example, society is the application domain, the residential community we live in is the context container, the Community guard is the context behavior, and the guard + community = context. We are the object. The Community guard will ask strangers in and out of the community: Which one are you looking? Who are you looking? What?
The real context is also doing this, but it is actually much more busy (this article describes the meaning of AOP)

Figure 1

Is a preliminary example structure. Let's look at it.

In the system, we can use the static attribute DefaultContext of the Context class to obtain the Context of the current object,

Context context = Context.DefaultContext;

Of course, you can also obtain the current context through the static attribute CurrentContext of the Thread class.

Context context = Thread.CurrentContext;

Figure 2

(The start of the left-side red line indicates the thread starts, and the end of the red line indicates the thread introduction)

In the context system, ContextBoundObject, IContextAttrbitute, IContextProperty, and message receiver are the context binding objects.

This article only covers the first three knowledge points in the system, that is, the custom component service. How is it actually implemented?

Before starting, let's talk about the context and object type. There is a default context in the application domain, and common objects can be used in any context, such an object is called a context agile object, and some objects can only be used in the context of a feature, that is, objects inherited from the ContextBoundObject type, such objects are called context oriented objects.

How can we understand custom component services?

1. Context member attributes (IContextProperty)

1 /// <summary> 2 // context Member attributes 3 /// </summary> 4 public class ContextWriterService: IContextProperty 5 {6 7 public void Freeze (System. runtime. remoting. contexts. context newContext) 8 {9 10} 11 12 public bool IsNewContextOK (System. runtime. remoting. contexts. context newCtx) 13 {14 return true; 15} 16 17 public string Name18 {19 get {return "ContextService ";} 20} 21 22 /// <summary> 23 // The provided service is 24 /// </summary> 25 /// <param name = "meg"> </param> 26 public void WriterMessage (string meg) 27 {28 Console. writeLine (meg); 29} 30}

Several members of the IContextProperty interface: Name read-only attribute, member attribute Name, used as a keyword in the context Member attribute set, can also be treated as a keyword and Name of a custom service. Freeze: Freeze the context IsNewContextOK: determines whether the current context meets the requirement and provides an opportunity to discard context creation if the function returns False.

Objects of the IContextProperty type will exist in the ContextProperties Of the context. Of course, they are not directly added. When we get the context object, the context object has been frozen.

 

2. Context attributes (IContextAttrbitute)

 1     using System.Runtime.Remoting; 2     using System.Runtime.Remoting.Contexts; 3     using System.Runtime.Remoting.Activation; 4  5  6     [AttributeUsage(AttributeTargets.Class)] 7     public class ContextWriterAttribute :Attribute, IContextAttribute 8     { 9 10         public void GetPropertiesForNewContext(IConstructionCallMessage msg)11         {12             msg.ContextProperties.Add(new ContextWriterService());13         }14 15         public bool IsContextOK(System.Runtime.Remoting.Contexts.Context ctx, IConstructionCallMessage msg)16         {17             ContextWriterService contextService =18                 ctx.GetProperty("ContextService") as ContextWriterService;19             20             if (contextService != null)21             {22                 return true;23             }24             return false;25         }26     }

IContextAttribute interface member:
IsContextOK function: Call this method at runtime to determine whether the current context has the required custom attributes, that is, whether the current type is activated in the current context.
GetPropertiesForNewContext: This method is called after the IsContextOK method is determined not OK. The IConstructionCallMessage interface type object here will be passed to the new context, the newly created context is based on
ContextProperties is the only opportunity to add a custom service to the context.
I also have a question here. I hope you can tell me how to add a Custom Service in the current default context?

3. Context binding object (ContextBoundObject)

1 [ContextWriter ()] 2 public class MyContextObject: ContextBoundObject 3 {4 public void CallCurrentContextWriterService () 5 {6 Context context = Thread. currentContext; 7 ContextWriterService contextWriterService = 8 context. getProperty ("ContextService") as ContextWriterService; 9 if (contextWriterService! = Null) 10 {11 contextWriterService. WriterMessage ("current context ID:" + context. ContextID + "-test information"); 12} 13} 14}

Obviously, MyContextObject can obtain and use the ContextWriterService type.

These are part of the context environment, and of course the most important part: the message receiver. This article will not be introduced, but will be discussed in the future.

 

 

 

Author: Jin Yuan

Source: http://www.cnblogs.com/jin-yuan/

The copyright of this article is shared by the author and the blog Park. You are welcome to reprint this article. However, you must keep this statement without the author's consent and go to the Article Page.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.