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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:55:45+00:00 2026-05-22T00:55:45+00:00

I just start study DI (I’m working on WPF/Silverlight but I have a plan

  • 0

I just start study DI (I’m working on WPF/Silverlight but I have a plan to move to ASP.NET). After I read some DI articles from internet there are two Frameworks that I’m interested in, MEF and Unity. I want to know what is the real-world different between them and which one is good to go.

  • 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-22T00:55:45+00:00Added an answer on May 22, 2026 at 12:55 am

    The main difference is that with unity you will explicitly register each class you want to use in the composition:

    var container = new UnityContainer();
    container.RegisterType<IFoo,Foo>();
    container.RegisterType<IBar,Bar>();
    ...
    var program = container.Resolve<Program>();
    program.Run();
    

    In MEF on the other hand, you mark classes with attributes instead of registering them somewhere else:

    [Export(typeof(IFoo))]
    public Foo
    {
       ...
    }
    

    At first sight this looks like a minor syntactic difference, but it is actually more important than that. MEF is designed to allow for the dynamic discovery of parts. For example, with a DirectoryCatalog you can design your application in such a way that you can extend it by simply dropping new DLLs in the application folder.

    In this example, MEF will find and instantiate all classes with an [Export(typeof(IPlugin))] attribute in the given directory and passes those instances to the Program constructor:

    [Export]
    public class Program
    {
        private readonly IEnumerable<IPlugin> plugins;
    
        [ImportingConstructor]
        public Program(
           [ImportMany(typeof(IPlugin))] IEnumerable<IPlugin> plugins)
        {
            this.plugins = plugins;
        }
    
        public void Run()
        {
            // ...
        }
    }
    

    Entry point:

    public static void Main()
    {
        using (var catalog = new DirectoryCatalog(".","*"))
        using (var container = new CompositionContainer(catalog))
        {
            var program = container.GetExportedValue<Program>();
            program.Run();
        }
    }
    

    To accommodate such dynamic composition scenarios, MEF has a concept of “stable composition”, which means that when it runs into a missing dependency somewhere it will simply mark the part as unavailable and will continue the composition anyway.

    Stable composition can be quite useful, but it also makes it very difficult to debug a failed composition. So if you don’t need dynamic discovery of parts and “stable composition”, I would use a regular DI container instead of MEF. Unlike MEF, regular DI containers will give you clear error messages when a dependency is missing.

    It might also be possible to get the best of both worlds by using a DI container which integrates with MEF, like Autofac. Use Autofac to compose the core application, and MEF for the parts which need to be dynamically extensible.

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

Sidebar

Related Questions

Im trying to stop using TcpTrace and start working with Fiddler. But i just
I'm new to ASP.NET and just start with Beginning ASP.NET 4 in C# and
I just start learning WPF, is that possible I can create an IE toolbar
I'm working on a project which is just about to start, and since I
I have been working as a .net developer following the waterfall model. When working
I just start a project where I need to have a WCF services that
I just start using Mercurial yesterday (I don't have much programming experiences). I noticed,
I have read a lot of books about Microsoft Dot Net and I have
I just start using jqgrid, but when I try the most simple example (loading
I just start playing with GWT I'm having a really hard time to make

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.