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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:45:08+00:00 2026-05-27T12:45:08+00:00

I have the following classes: public class Foo { [Import] public IFirstInterface firstImport; [Import]

  • 0

I have the following classes:

public class Foo
{
    [Import]
    public IFirstInterface firstImport;

    [Import]
    public ISecondInterface secondImport;

    // ...
}

public class A : ISecondInterface {}

public class B : ISecondInterface {}

I would like to able in class Bar, when calling

var foo = compositionContainer.GetExportedValue<Foo>();

To specify wether I want to use class A or B.

I know, I can specify a contract name in the GetExportedvalue method. But as I understand it, this would mean having two class Foo. I would like to reuse the same class but control which class (A or B) is used to satisfy the ISecondInterface dependency.

EDIT :
“What are the rules that will decide whether you use class A or B?”

A:
Foo is called by a wcf service.
Class A and B differ in that they are working on different entities. These entities have different fields and can’t be queried the same way.

My classes are not in english, but I will try to translate to more meaningful terms than Foo and Bar.

Foo could be a RecordAssigner. The RecordAssigner assigns records to users. The logic to do this is contained in this class. The ISecondInterface is an interface to a data layer class that works with records.

It’s implementation expose methods to work on different kinds of records. While both kind of records need can expose the necessary methods for them to be assigned, they are manipulated differently at the data level.

At the service level, I want to be able to have methods called
AssignRecordTypeA and AssignRecordTypeB without duplicating all the code in RecordAssigner.

  • 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-27T12:45:08+00:00Added an answer on May 27, 2026 at 12:45 pm

    What are the rules that will decide whether you use class A or B? You can write your own resolver for MEF exports.

    This blog post http://randomactsofcoding.blogspot.com/2010/01/configurable-type-catalog-for-mef.html mentions one approach and there are a few other ones that you can find if you search for “mef custom export provider” or “mef custom catalog”

    EDIT:
    Your scenario reminds me a little bit of a conversation with some colleagues a while ago. In my view MEF is for “extensions” to your application rather than dependencies. The case you have looks more like dependencies. So you would be better off using an IoC container to manage your dependencies. You can have multiple ISecondInterface instances injected in with different keys and then choose which one to use.

    If you feel MEF is more suited to your requirements you can still do this by changing

    public class Foo
    {
        [Import]
        public IFirstInterface firstImport;
    
        [ImportMany]
        public Collection<ISecondInterface> secondImport;
    
        // ...
    }
    

    and then deciding which one to use.

    Having said that, the case for having one service that deals with two different entities talking to two different repositories is debatable.
    If you have some common logic you could refactor that out, but it would be better to have two distinct services each with the dependency it requires to do it’s work.

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

Sidebar

Related Questions

I have the following two classes. public class Foo { public Bar Args {
I have a class that is something like the following: public class Foo {
Assume I have a class like this: public class Foo { public Bar RequiredProperty
Assume you have several arbitrary classes like below: class Foo { $foovar; public function
Suppose I have classes Foo and Bar as follow: public class Foo { public
I have the following classes: public class Person { public String FirstName { set;
I have the following classes: Public Class Email Private Shared ReadOnly EMAIL_REGEX = \b[a-zA-Z]+[a-zA-Z0-9._+-]+@
I have the following classes public interface InterfaceBase { } public class ImplementA:InterfaceBase {
I have the following two classes: public class Address { public string AddressLine1 {
I have the following classes [XmlRoot] public class AList { public List<B> ListOfBs {get;

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.