MEF for Beginner (Part Creation Policy) – part 6

2010/01/09

3 comments

MEF

MEF for Beginner (Part Creation Policy) – part 6

this is the 6th post of the MEF for Beginner series, the series TOC is available here.

 

this post and a few that will follow will cover some of the policies options available with MEF.

we will start with the instantiation policy.

 

How do we define policy?

we will define policy as instruction that will guide behavioral decisions that related

to the compose-able parts (the imported and exported parts behaviors).

 

What is instantiation policy (part creation policy)?

the instantiation policy, is the instruction for how parts should be instantiate.

there is 2 instantiation options:

  • Shared: is a singleton model, which mean that exported part instantiation will instantiate once
    (not matters how many time this part was requested).
  • Non Shared: will create new instance each time we are asking for exportable part.

 

What is the default instantiation?

the default instantiation model of MEF is the singleton model.

 

Best practice

the best-practice is to use the Shared (singleton) model.

this bring us to design consideration which suggest that you should design your

exportable parts as stateless and thread safe, so they won’t be affected

by multiple calls (maybe on different threads) upon the same instance.

 

in cases that the singleton model is not suitable for you, it is recommended

to use builder pattern (to separate the exported part from the actual instantiation).

you should remember that using the non shared model is quite costly because

it is using reflection for the actual instantiation (by using the builder pattern you

can gain the same result with less pain).

 

How to define our instantiation model?

out of the box we having the [PartCreationPolicy] attribute which make it fairly simple

to define instantiation policy.

here is code snippet that using the [PartCreationPolicy] attribute, which I will immediately discuss:

Code Snippet
  1. [InheritedExport]
  2. public abstract interface IPerson { … }
  3.     
  4. [PartCreationPolicy (CreationPolicy.Shared)]
  5. public class Person1 : IPerson { … }
  6.  
  7. [PartCreationPolicy (CreationPolicy.NonShared)]
  8. public class Person2 : IPerson { … }
  9.  
  10. [PartCreationPolicy (CreationPolicy.Any)]
  11. public class Person3 : IPerson { … }

at lines 1,2: we can see the contract definition (the contract does not dictate the creation policy)

the InheritedExport attribute means that any type derived from the contract will be automatically exported.

line 4: is decorating Person1 class to apply to the shared (singleton) model.

so no matter how many times Person1 will be imported it will always be the same instance.

line 7: is decorating Person2 class to apply to the non shared model.

so different instance of Person2 will be assigned for each import.

line 10: at the moment this could be somewhat puzzling but we will shortly clear this out.

in terms of instantiation this is the default and it is equivalent to the shared declaration

(unless the import decoration is explicitly asking for non shared policy).

so you may ask yourself why should they add this option if it equivalent to the shared one?

the answer for this question can be found in the next paragraph.

 

How to restrict our import discovery for specific instantiation model?

the default setting of Import / ImportMany is equivalent to the next snippet:

Code Snippet
  1. // [Import] equals to the below decoration
  2. [Import (RequiredCreationPolicy=CreationPolicy.Any)]
  3. public IPerson PersonDefault { get; private set;}
  4.  
  5. // [ImportMany] equals to the below decoration
  6. [ImportMany (RequiredCreationPolicy=CreationPolicy.Any)]
  7. public IEnumerable<IPerson> PersonsDefault { get; private set;}

CreationPolicy.Any means that our import does not care about the instantiation model

of the parts, so the MEF discovery can assign any parts that match the IPerson contract.

the PersonsDefault property will be assigned with Person1, Person2 and Person3.

 

sometimes we want to be more explicit about what type of instantiation allowed for the import.

for those scenarios we can use the following decorations:

Code Snippet
  1. [ImportMany(RequiredCreationPolicy = CreationPolicy.Shared)]
  2. public IEnumerable<IPerson> PersonsShared { get; private set; }
  3.  
  4. [ImportMany(RequiredCreationPolicy = CreationPolicy.NonShared)]
  5. public IEnumerable<IPerson> PersonsNonShared { get; private set; }

line 1: restrict the MEF assignment of PersonShared property to exported parts that created

under the shared policy, which is means parts that decorated with CreationPolicy.Shared or CreationPolicy.Any.

PersonsShared property will be assigned with Person1 and Person3.

line2: restrict the MEF assignment of PersonNonShared property to exported parts that created

under the non shared policy, which is means parts that decorated with CreationPolicy.NonShared or CreationPolicy.Any

(in this case the part that decorated with the CreationPolicy.Any will not instantiate as a singleton).

PersonsShared property will be assigned with Person2 and Person3.

 

Summary

MEF instantiation is singleton by default, but this policy can be altered.

 

you can find code sample that demonstrate the different creation policies here.

 

תגים של Technorati:‏ ,,,,,,


Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

3 comments

  1. StefanoS2013/11/21 ב 01:31

    I noticed that it’s hard to find your site in google, i found it on 26th spot, you should get some quality backlinks to rank it in google and increase traffic. I had the same problem with my site, your should search in google for – insane google ranking boost – it helped me a lot

    Reply
  2. new bingo sites 20132013/12/25 ב 06:03

    Нmm it appears like your wеbsite ate &X6D;у &X66;iгѕ&X74; c&X6f;mment
    (it ωaѕ super long) ѕo Ι guess &X49;’ll jus&X74; sum it up &X77;hаt Ι hаԁ written
    and sa&X79;, I’m thoroug&X68;ly enjoуіng y&X6F;ur blog.
    I too am an aspir&X69;&X6e;g blog writеr but
    I’m still new to the whole &X74;hin&X67;. Do you have any
    recommendations for beginn&X65;r blog wrіterѕ?
    I’d definitely appr&X65;cіa&X74;e it.

    Reply
  3. Subbu2014/10/11 ב 01:09

    I am not able to find the samples. Can you share the samples? I got stuck in this part as it is returning same instance every time.

    Reply