Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 680265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:21:10+00:00 2026-05-14T01:21:10+00:00

I’ve begun experimenting with dependency injection (in particular, MEF) for one of my projects,

  • 0

I’ve begun experimenting with dependency injection (in particular, MEF) for one of my projects, which has a number of different extensibility points. I’m starting to get a feel for what I can do with MEF, but I’d like to hear from others who have more experience with the technology. A few specific cases:

  1. My main use case at the moment is exposing various singleton-like services that my extensions make use of. My Framework assembly exposes service interfaces and my Engine assembly contains concrete implementations. This works well, but I may not want to allow all of my extensions to have access to all of my services. Is there a good way within MEF to limit which particular imports I allow a newly instantiated extension to resolve?

  2. This particular application has extension objects that I repeatedly instantiate. I can import multiple types of Controllers and Machines, which are instantiated in different combinations for a Project. I couldn’t find a good way to do this with MEF, so I’m doing my own type discovery and instantiation. Is there a good way to do this within MEF or other DI frameworks?

I welcome input on any other things to watch out for or surprising capabilities you’ve discovered that have changed the way you architect.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:21:11+00:00Added an answer on May 14, 2026 at 1:21 am

    Is there a good way within MEF to
    limit which particular imports I allow
    a newly instantiated extension to
    resolve?

    Load the extension code in a separate container, and make sure that the restricted parts are not available in that container. Let’s simplify the situation to these classes to construct an example:

    [Export]
    public class MyExtension
    {
       [Import]
       public PublicService Service { get; set; }
    
    }
    
    [Export]
    public class PublicService
    {
    }
    
    [Export]
    public class InternalService
    {
    }
    
    [Export]
    public class Program
    {
       [Import]
       public MyExtension Extension { get; set; }
    
       [Import]
       public PublicService Service1 { get; set; }
    
       [Import]
       public InternalService Service2 { get; set; }
    }
    

    The goal is to allow MyExtension to import PublicService, but not InternalService. Internal code like Program should be able to import anything. You can achieve that like this:

    var publicCatalog = new TypeCatalog(typeof(PublicService), typeof(MyExtension));
    var publicContainer = new CompositionContainer(publicCatalog);
    
    var internalCatalog = new TypeCatalog(typeof(Program), typeof(InternalService));
    var internalContainer = 
        new CompositionContainer(internalCatalog, publicContainer);
    
    var program = internalContainer.GetExport<Program>();
    

    This code will not throw a composition exception. If you now change the import on MyExtension to the forbidden InternalService, you will get a composition exception as desired.

    A side effect of this set-up is that PublicService cannot import any private services either, just like MyExtension. This kind of makes sense, because otherwise nothing would stop PublicService from exposing a private service via a property.

    I have used TypeCatalog for the example, but in practice you should probably use something like the FilteredCatalog sample.

    This particular application has
    extension objects that I repeatedly
    instantiate. I can import multiple
    types of Controllers and Machines,
    which are instantiated in different
    combinations for a Project. I couldn’t
    find a good way to do this with MEF,
    so I’m doing my own type discovery and
    instantiation. Is there a good way to
    do this within MEF or other DI
    frameworks?

    You might just be after the PartCreationPolicy attribute, which controls whether a part is shared (as in, created only once per container) or instantiated multiple times for each import. You can also specify the RequiredCreationPolicy parameter in an import attribute.

    If that doesn’t solve your problem, take a look at the PartCreator sample in the MEF sources (though note that it will probably soon be renamed to ExportFactory, it already has been in the silverlight edition of MEF).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an array which has BIG numbers and small numbers in it. I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
In my XML file chapters tag has more chapter tag.i need to display chapters
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.