c# - Managed Extensibility Framework (MEF), why are Imports needed? -


i wonder use of import/importmany decorations for? played mef , manage through compositioncontainer. decorated exports custom metadataattribute derives exportattribute. when try access plugin instances can lazily access meta data , plugin implementation through container.getexports<t, imetadataattribute>().

so, why need bother import decorations? understand core of mef compositioncontainer , should care about. examples on web pass in import decorated object instances. why , added value provide?

here example of how access meta data , actual plugins:

    public static ienumerable<ipluginattributeview> getmetadata<t>()     {         return container.getexports<t, ipluginattributeview>().select(e => e.metadata);     }      public static t getplugin<t>(string pluginname) t : class     {         var plugins = container.getexports<t, ipluginattributeview>();         var pluginbyname = plugins.where(e => e.metadata.pluginname.equals(pluginname)).firstordefault();          return pluginbyname.value;     } 

am missing or lack understanding? please me understand.

the import attributes (and equivalent registrationbuilder methods in mef2) useful because offer automatic dependency injection. container handles you. can have many types decorated export attributes , wiht members decorated 1 of import attributes , let container compose them when need them. use container exports no other type importing. rest handled mef. if avoid import attributes have inject dependencies yourself.

if @ of mef examples out there note of them use import attributes extensively. there people use mef , don't know getexportxxx methods provided compositioncontainer. of course decide best suited application(s). in case (plug-in manager), getexports method might enough. if decide add dependencies between plug-ins import attributes become valuable , plug-in manager simpler. nice feature of mef plug-in managers recomposition, far know available via import attributes (or mef2's registrationbuilder). can add great value plug-in aware app. either way still have extensibility feature mef supposed provide.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -