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

  • SEARCH
  • Home
  • 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 6653831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:19:02+00:00 2026-05-26T01:19:02+00:00

We are creating an application which supports plugins using MEF. We are determining what

  • 0

We are creating an application which supports plugins using MEF. We are determining what type of plugins the user is able to create, and want to use dependency injection to provide this type of plugin with the data it needs.

For example, we make a plugin that is able to display a list. To achieve this, it needs the existing instance of the IRepository for the type of data the list will display.

The IRepository is created somewhere else in a datacontext class, so we are unable to let MEF itself create an instance of the IRepository.

My idea is to inject the existing instance of the IRepository into the plugin via the importingconstructor, however for this to work I need to make the already instantiated IRepository known to MEF, and I haven’t been able to figure out how to do it. Any help would be appreciated.

  • 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-26T01:19:03+00:00Added an answer on May 26, 2026 at 1:19 am

    The easiest way is to compose an existing value in the container, e.g.:

    var repo = // Create repo
    container.ComposeExportedValue<IRepository>(repo);
    

    But this will only allow 1 instance of the IRepository to exist, because it doesn’t give you direct control over the ComposablePart that is created. If you want more fine grained control, you can use a CompositionBatch to great effect:

    var batch = new CompositionBatch();
    var repo = // Create repo
    
    var repoPart = batch.AddExportedValue<IRepository>(repo);
    container.Compose(batch);
    
    // repo will now be injected on any matching [Import] or [ImportingConstructor]
    

    And later on:

    var batch2 = new CompositionBatch(null, new[] { repoPart });
    var repo2 = // Get new repo
    
    var repo2Part = batch2.AddExportedValue<IRepository>(repo2);
    container.Compose(batch2);
    

    Because I have access to the ComposablePart instance provided by the batch, I can remove it later on. There are other ways of importing attribute-less parts, generally through property exports:

    [Export(typeof(IRepository))]
    public IRepository Repository
    {
        get { return CreateRepository(); }
    }
    

    But that of course would require you to be able to create an instance of your repository at composition time, which may or may not be possible.

    Lastly, there is the option to use an alternative programming model. The default (and most common) in MEF is the attributed programming model, whereby you utilise [Export] and [Import] attributes to control your composition, but in MEFContrib (and forthcoming in MEF2) is the ability to use a registration programming model whereby parts are composed based on a mechanism similar to most other IoC containers.

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

Sidebar

Related Questions

I am creating an application using Grails Framework for which I plan to use
For a web application, when creating the user which will connect to the MySQL
I'm creating a Windows application which use FileSystemWatcher. FileSystemWatcher watches some catalog for changes.
I am currently creating an application which signals readiness to other devices using a
i am creating an application in which i want set different background images in
I am creating a chat application which supports chat, group chat, file sharing, whiteboard(not
I want to create chat application in iPhone. Are there any framework(s) which support
I'm creating an application which lets you define events with a time frame. I
I'm creating an AIR application which connects to a SQLite database. The database balks
I am creating an application in which I have two mxml components, MainPanel.mxml and

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.