MEF framework learning journey (7) Part Creation Rules (policies)

Source: Internet
Author: User

When the component is specified to be imported and combined, the combined container will try to find the matching export. If it matches the import and export, the import member is set to the instance of the exported object. The creation policy of the exported part controls where the instance comes from.

Two possible creation policies are shared and non-shared ". In a container that has a part with this Protocol, parts with a policy of "share" will be shared between each import. When the engine finds a match and must set the import attribute, it will instantiate a new copy of the part only when the part does not exist; otherwise, it will provide an existing copy. This means that many objects may reference the same part. Such parts should not depend on internal states that may be changed from many places. This policy applies to static parts, components that provide services, and components that consume a large amount of memory or other resources.

Each time you find a matched import of one of the exported parts, a part with a non-shared policy will be created. Therefore, a new copy is instantiated for each import in a container that matches one of the exported protocols of the component. The internal status of these replicas will not be shared. This policy applies to the parts whose internal status is required for each import.

The part creation policy in MEF can be one of the following three values: creationpolicy. Shared, creationpolicy. nonshared or creationpolicy. Any. To specify a part creation policy, use the partcreationpolicy feature to modify the part. By setting the requiredcreationpolicy attribute for the import, you can also specify the partcreationpolicy on the import program side.

Both the import and export policies can be set from shared, nonshared, or any. The default value for import and export is any. The export of the specified shared or nonshared will only match the specified value or the specified any import. Similarly, the specified shared or nonshared import will only match the specified value or the specified any export. Just like the import and export of their protocol names or protocol types that do not match, the import and export of incompatible creation policies will not be considered as a match. If any is specified for both import and export, or the creation policy is not specified and the default value is any, the creation policy is "shared" by default ".

[Creationpolicy] enumeration value: [Shared] indicates a shared part, that is, a single instance. All imports use one instance. If this instance is not in the combined engine, it is created, once there is, it will not be created; [nonshared] corresponds to [Shared], that is, each import creates a new instance, all the imported instances have their own unique status, data is not shared; [any] is only used to match the import and export, there is a matching table below

Export (export): creationpolicy

Import: creationpolicy

Any

Any, nonshared, and shared

Noneshared

Noneshared, any

Shared

Shared, any

Only when the preceding table is satisfied will the Import and Export match. The following is a simple example:

Code segment

Class program {Private Static compositioncontainer container; static void main (string [] ARGs) {var catalog = new assemblycatalog (typeof (Program ). assembly); Container = new compositioncontainer (Catalog); var studentmanager1 = container. getexportedvalue <studentmanager> (); var studentmanager2 = container. getexportedvalue <studentmanager> (); console. writeline (object. referenceequals (studentmanager1, studentmanager2); console. writeline (object. referenceequals (studentmanager1.student, studentmanager2.student); console. readline () ;}// export [export, partcreationpolicy (creationpolicy. shared)] public class student {public string name {Get; set;} public int age {Get; Set ;}// non-singleton export [export, partcreationpolicy (creationpolicy. nonshared)] public class studentmanager {// default any [import] public student {Get; Set ;}}

The final output is false and true. The studentmanager class of the student management class is nonshared, so it will be instantiated every time it is exported. The student class is shared, so it is in singleton mode and only exports one instance.

Note:

[Export, partcreationpolicy (creationpolicy. nonshared)]

Equivalent

[Export]

[Partcreationpolicy (creationpolicy. nonshared)]

In addition, we can define the export in the container in advance. This definition does not require the [Export] attribute description type, and the output will always be a singleton:

Code segment

 

  container.ComposeExportedValue<DateTime>(DateTime.Now);  Console.WriteLine(container.GetExportedValue<DateTime>());

 

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.