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 8823465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:21:44+00:00 2026-06-14T06:21:44+00:00

I have hit a wall. I have added a reference of another assembly that

  • 0

I have hit a wall. I have added a reference of another assembly that has Service classes.
So instead of creating a new instance i though that MEF would help(but wont).. I do something similar to HelloScreens sample.The following viewmodel accepts a property too that holds the model.(Staff)

public class StaffFormViewModel : Screen
{
    [Import]
    public StaffService staffService { get; set; }

Obviously this doesnt work as the class isnt constructed by MEF. But even i wanted to construct it with mef i should create multiple instances so i should select NonShared.But people say its slow. Caliburn micro example adds something like this in order to create new instances. This requires 5 lines of code for each thing.

batch.AddExportedValue<Func<PreferencesCategoriesFormViewModel>>(
    () => container.GetExportedValue<PreferencesCategoriesFormViewModel>());

The problem is that although i can use this then if i want to provide a model class at constructor I cant because it is constructed by MEF. I should set the property by hand.
All i wanted is to inject the needed service. Isnt this possible with mef? Should i have public static the container so i can reference it and call compose? Please help 🙂

  • 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-06-14T06:21:45+00:00Added an answer on June 14, 2026 at 6:21 am

    The sample you’ve given is just creating a factory that returns new instances of PreferencesCategoriesViewModel. They are using the built in delegate type Func in order to save them creating a new factory interface, and an implementation of the interface which would require a reference to the container.

    So if you wish to instantiate view models via MEF, then you can either:

    1. If you only need a single instance of the view model, then you could inject it via constructor injection to the parent view model, and leave MEF to instantiate it
    2. If you need control over the instantiation of the view model, or you need to instantiate multiple instances of the view model, then you could inject a view model factory type to the parent view model

    If you are going to use a view model factory then you could:

    1. Use the built in Func delegate type as shown above, you can always use one of the other Func types that take one or more input parameter as your child view model requires data
    2. Create a specific factory interface and concrete implementation, and register that with the container
    3. Create a generic view model factory and inject that

    For an example of option 3, see http://pglazkov.blogspot.co.uk/2011/04/mvvm-with-mef-viewmodelfactory.html.

    You’ll notice that his view model factory has a reference to the MEF container, which he actually resolves via MEF. Therefore your container will need to register itself.

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

Sidebar

Related Questions

I'm starting out with classes in Javascript and have hit a wall. I've looked
I have hit another wall. After getting my key input working, I have been
I have hit another wall... I have the following script, which appears to work
I am very new to javascript and hit a wall. Basically I have a
Still new to Haskell, I have hit a wall with the following: I am
I'm new to programming (just started!) and have hit a wall recently. I am
I am in the process of creating a server but I have hit wall
I'm trying to do a cross domain POST request and have hit a wall
So I just started Android development, and I've already hit a wall. I have
I'm new to python and have hit a problem with an SQL query I'm

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.