Search code examples
c#mef

Looking for the use of MEF in winform c#


i saw MEF become very popular now but i have no concept about MEF and its usage. so anyone tell me what is MEF & what kind of situation a developer use MEF. i was reading a article but things was not clear to me what is MEF. they use lots of attribute like import & export....what it does. what will be import & export by apps.

i read this article like http://blogs.msdn.com/b/brada/archive/2009/07/20/simple-example-using-managed-extensibility-framework-in-silverlight.aspx

it would be very helpful if some one discuss what is MEF and also its usage....where to use it. give me a tiny sample code for winform as a result i can understand its usage. thanks


Solution

  • Mef is dependency injection framework

    Your differences with Unity

    It discovers assemblies in a directory.

    It uses XAP file download and assembly discovery.

    It recomposes properties and collections as new types are discovered. Page 31

    It automatically exports derived types.

    It is deployed with the .NET Framework.

    First Step : export your module

    [Export]
    public class YourService : IYourService
    {
    .....
    
    }
    

    If you want inject this service in your clien, you can inject with (property injection)

    public class Client
    {
    
    [Import]
    public IYourService YouService;
    
    
    ......
    }
    

    Th third step is to register to MEF in catalog.