Quantcast
Channel: Managed Extensibility Framework
Viewing all articles
Browse latest Browse all 265

New Post: Use MEF to allow only 2 instances at max per application

$
0
0
Hi there,

Creation policy (e.g. sharing) applies to parts, while one part may have many exports. This is just how the MEF component model works - in most situations this turns out to be useful.

To achieve what you want in a more MEF-like way:
  1. Don't make MyClass a part at all:
public class MyClass { }
  1. Create a separate class that holds and exports the two instances as properties:
public class MyClassConfig
{
   MyClass _inst1 = new MyClass();
   MyClass _inst2 = new MyClass();

   [Export("Condition-1")]
   public MyClass Inst1 { get { return _inst1; } }

   [Export("Condition-2")]
   public MyClass Inst2 { get { return _inst2; } }
}
This is the basic approach, anyway - you can tinker with MyClassConfig to implement lazy construction etc. if this is needed.

Hope this simplifies things!
Nick

Viewing all articles
Browse latest Browse all 265

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>