Last time, we secretly read the locator in Microsoft. Practices. objectbuilder (hereinafter referred to as ob). This time we will look at this framework as a whole. If we really want to know how to use OB, we must have a general understanding of him. We want to learn her, and of course we want to use it, but the most important thing is that we can consolidate our knowledge by analyzing its mechanism. But for a class library that is not very small, it is impossible to understand it all at once, not to mention that the documentation of OB is null, so it is only possible to split it step by step. In fact, we don't need to split it, because the source code has put all the relevant items in a separate folder. Okay, I don't have to talk about it anymore. Come on together!
There are several very important classes in ob. Let's take a look!
Strategy and strategy ies: OB is based on policies and policies. You can search for registered policies through the rule chain (the responsibility chain design mode is used here). The policy uses the policy to indicate how to create an object. The policy registers a type with OB. It is worth mentioning that every registered object in ob contains an ID and type, so that the object can be retrieved through ID;
Locator: The locator is used to search for registered objects. when an object is created, it is registered with the locator for later retrieval. You can see the registerobject method in the creationstrategy class;
Lifecontainer: literally, we can see that it should be a survival container (but I don't know if there is a better saying). Every object has its own survival, in ob, lifeiner iner contains a list of objects of a series of objects. Because the object is contained in lifecontainer, if the object is released (disposed) lifecontainer, all objects in the list will be released.
Buildercontext: the context runs through the entire process of object creation (that is, from buildup to teardown of an object). The context includes strategies, policies, and locator and also provides an iterative policy chain method.
Having understood the above several heavyweight classes, it is not a big problem to create an object. Of course, we still need to have a deeper understanding of building complex objects, I will slowly introduce some examples and methods. According to my understanding and summary, the following steps are generally required to create a method. Of course, these steps may not be required. Let's take a look at these steps!
1. to create an object, we need to know how many elements are required to create the object. In general, we need a locator, a rule chain builderstrategychain, and a series of policies and policies Policies; in fact, we are all preparing for creating the context buildercontext.
2. with the strategy chain, we need to instill a policy in it. Each policy corresponds to a corresponding policy (not necessarily one or more, but more in-depth research is required here, if not, please advise), and creating an object usually includes a creationstrategy.
3. With a policy, we need to set the corresponding policy for each policy. Generally, the policy corresponding to the creationstrategy policy uses the Default policy.Defaultcreationpolicy, of course, can have other policies.
4. When all the above conditions are met, we need to create a context without a locator. We also need a lifecontainer to save the object.
5. Create the context buildercontext, which has been basically solved here. Use the buildup method in the Policy chain to solve the problem.
The policy is to tell us which policy is used to complete the process, and the policy is to register an object type. With the locator, We can get registered (retrieve registered objects) or an object that is not registered (then a new object is created. Let's talk about it today. If there is anything wrong, please give me more advice.