MEF Programming Guide VIII: Part lifecycle (Parts lifetimeime) Hosting

Source: Internet
Author: User
Tags hosting

Each of the exported parts that can be dynamically assembled in the MEF is lifecycle-the lifecycle is generally not managed without special requirements, and in fact MEF has the default lifecycle management for each component, and the MEF's lifecycle is divided into three categories: any, Shared and nonshared are defined in the System.ComponentModel.Composition.CreationPolicy enumeration object.

namespace System.ComponentModel.Composition
{
     public enum CreationPolicy
     {
          Any = 0,
          Shared = 1,
          NonShared = 2,
     }
}

Any representation that can be shared or unshared, where instances of a part are automatically controlled according to different request requirements using the MEF container; shared represents a shared part, and a plug-in part that is of the same type can be used in multiple MEF combination containers, followed by the nonshared type, which indicates that the part instance is not shared. A new object instance is created whenever a new request is made. In MEF, the life cycle configuration of a part is implemented through the Partcreationpolicyattribute attribute.

public interface IBookService
{
     string GetBookName();
}
[PartCreationPolicy(CreationPolicy.Any)]
[Export(typeof(IBookService))]
public class MEFBookService : IBookService
{
     public string GetBookName()
     {
         return "《MEF程序设计指南》";
     }
}
[PartCreationPolicy(CreationPolicy.NonShared)]
[Export(typeof(IBookService))]
public class ASPNETBookService : IBookService
{
     public string GetBookName()
     {
         return "《ASP.NET项目案例》";
     }
}
[PartCreationPolicy(CreationPolicy.Shared)]
[Export(typeof(IBookService))]
public class SLBookService : IBookService
{
     public string GetBookName()
     {
         return "《Silverlight高级编程》";
     }
}

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.