Create policies (Creation policy) and Lifecycle (life cycle)

Source: Internet
Author: User

Objective

As we have already learned in the previous introduction: The successful matching of import and export requires contracttype,contractname,metadata matching, here we also introduce a new thing: Create a policy (creation). Sometimes our instances in containers are shared between each import, which is a singleton; sometimes we need to have a separate instance of each import, which is determined by the creation strategy in MEF.

Lifecycle (life cycle), each MEF component has its own lifecycle in the container, when it is created, when it is released, and so on. This article focuses on the creation of policies and lifecycles in the MEF.

Create a policy

Creating a policy is actually a combination of containers deciding how to create a part. When combining a container assembly, if the import and export match succeeds, the combined container sets the value of the imported member as the exported instance. Therefore, the creation policy of the exported part determines where the part originates: an existing instance or a new instance.

The MEF's creation strategy is shared and nonshared (unshared).

Parts that use GKFX to create policies share instances in each imported part. A new instance is created only if there is no instance of the part in the container. Parts created with a sharing policy can be parts that provide services, and parts that are more memory-intensive. Their internal state should be as little as possible from outside influence.

Parts that use the nonshared policy will have new instances when matching each import. The internal state of these components is independent of each other and can be used when certain parts need to remain in a particular state.

There are 3 features that MEF provides for creating a policy: shared,nonshared,any (note the difference between any and Shared, there are only two creation policies). The default values for both import and export are any. When the import export is created by default, or by default, MEF will default to the creation of the policy as shared. The matching of the creation policy also determines whether the exported part and the import can successfully match. The following conditions can be considered a matching success:

1. The exported parts marked as any are successfully matched with the import of shared and nonshared;

2. Create an export with a policy of shared or nonshared and an import match marked as successful;

3. Create a policy for shared exports only with the creation of a policy shared and marked for any match success;

4. Create an export with a policy of nonshared can only be successful for shared and imported imports marked as any.

Attention:

This is how MSDN describes it:
The default values for import and export are any.
The export that specifies Shared or nonshared will match only the import that specified the same value or specified any.
Similarly, an import that specifies Shared or nonshared will match only the exports that specify the same value or specify any.
If both the import and export specify any, or if the create policy is not specified, then the Create policy defaults to shared.

To create the use of policy attributes:

Importing parts

[Import (Requiredcreationpolicy = creationpolicy.shared)]
Exporting parts
[Partcreationpolicy (creationpolicy.nonshared)]

Take a look at a simple example:

//define export parts, create policy for unshared[Export][partcreationpolicy (creationpolicy.nonshared)] Public classfilelog{ Public voidWritelog () {Console.WriteLine (" This isfilelog!&Amp;quot;); }}//define export parts, create policy for unshared[Export][partcreationpolicy (creationpolicy.shared)] Public classdblog{ Public voidWritelog () {Console.WriteLine (" This isdblog!&Amp;quot;); }} Public classmyclass{//successful Match[Import (Requiredcreationpolicy =creationpolicy.nonshared)] PublicFileLog MyFileLog1; //Match failed[Import (Requiredcreationpolicy =creationpolicy.shared)] PublicFileLog MyFileLog2; //Match Success[Import] PublicDBLog Mydblog;}
View Code

Note: So far we can see that the main factors that affect the successful matching of the exported part with the imported part are: Contracttype (contract type), Contractname (contract name), Metadata (metadata), Cetationpolicy (Create policy).

Life cycle

In the MEF, the life cycle is more complex and is only used in one sentence on MSDN:

Because parts are hosted in a composite container, their life cycles may be more complex than ordinary objects. The components implement two important life cycle related interfaces: IDisposable and ipartimportssatisfiednotification.

The container itself implements the IDisposable, and when the container Dispose method is called, the container calls the Dispose method on each part of the container. In general, a component frees resources only when the container is released. Lenovo to the above-described creation strategy, someone cannot help but ask if creating a policy for non-shared parts too much, it is a lot of resources, but do not worry: The container provides the Releaseexport method. This method frees the exported part and frees the resources that the part occupies.

If the part implements interface IPartImportsSatisfiedNotification, when the combination is complete and the import of the part is available for use, the composite window will onimportssatisfied the method in the assembly call interface.

The ipartimportssatisfiednotification contains a method named Onimportssatisfied. When the combination is complete and the import of the part is available for use, the composite window calls this method on any part that implements the interface. A part is a combination engine created to satisfy the import of other parts. Before you set up the import of a part, you cannot perform any initialization that relies on the imported values in the part constructor or operations on those values, unless you have specified them as prerequisites by using the Importingconstructor attribute. This method is typically the preferred method, but in some cases, constructor injection may not be available. In these cases, the initialization can be performed in Onimportssatisfied, and the part should implement IPartImportsSatisfiedNotification.

The flexible use of the part's lifecycle can be very helpful in using MEF in particular, such as: we may make wcf,ef into components and then use MEF to integrate these components, then after the combination succeeds, we may need to read the WCF Configuration Publishing Service, or read db.config to establish a database connection. It may be better to do these things in onimportssatisfied.

Http://www.cnblogs.com/pszw/archive/2011/12/14/2286895.html

Create policies (Creation policy) and Lifecycle (life cycle)

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.