• Using MEF to Set Up Dependency Injection


    The Managed Extensibility Framework (MEF) is a built-in set of elements that allows you to “export” and “import” objects across projects that allows you to not rely on hard dependencies.
    
    From Microsoft:
    
    The Managed Extensibility Framework or MEF is a library for creating lightweight, extensible applications. It allows application developers to discover and use extensions with no configuration required. It also lets extension developers easily encapsulate code and avoid fragile hard dependencies. MEF not only allows extensions to be reused within applications, but across applications as well.
    
    To use MEF you need to include the following reference in your project:
    
    System.ComponentModel.Composition
    
    We need to define a container. This is where all the exported values will be stored. One simple way to do that is to create a class such as this one:
    
    public static class Mef    
    {    
        private static CompositionContainer container;    
         
        public static CompositionContainer Container    
        {    
            get    
            {    
                if (container == null)    
                {    
                    var catalog =    
                        new DirectoryCatalog(".", "MyProjectNamespace.*");    
         
                    container = new CompositionContainer(catalog);    
                }    
         
                return container;    
            }    
        }    
    }   
    This will grab all the exported values from all the assemblies in the same directory starting with “MyProjectNamespace”.
    
    And then I can annotate the classes I want to export as in the following:
    [Export]    
    public class Logger    
    {    
    }    
         
    [Export]    
    public class GameService    
    {    
        [Import]    
        private Logger log;    
    }   
    Whenever I need a GameService I can request it from the container as in the following:
    GameService gameService = Mef.Container.GetExportedValue<GameService>();   
    Notice that on GameService class I have a field with an [Import] attribute. This means MEF will resolve the value for me while it is retrieving the exported value for GameService.
    
    I can also export a class identified by an interface:
    [Export(typeof(IGameService))]    
    public class GameService    
    {    
        [Import]    
        private Logger log;    
    }   
    And I could use it as:
    GameService gameService = Mef.Container.GetExportedValue<IGameService>();  
    MEF will get whatever it has in its container for IGameService.
    
    If you have more than one export for IGameService and you attempt to resolve it with GetExportedValue you will get an exception.
    
    You can have multiple exports, but the way you need to handle it is different.
    
    For example, I can have a screen with several tabs that are exported as in the following:
    public interface ITab { }    
         
    [Export(typeof(ITab))]    
    public class HomeTab : ITab { }    
         
    [Export(typeof(ITab))]    
    public class GamesTab : ITab { }    
         
    [Export(typeof(ITab))]    
    public class WishlistTab : ITab { }    
    And I can import them using the ImportMany attribute as in the following: 
    [Export]    
    public class Home    
    {    
        [ImportMany]    
        private List<ITab> tabs;    
    }    
    ImportMany 
    
    And there is also the ImportingConstructor attribute that allows me to import objects while I am creating the instance.
    [Export]    
    public class GameService    
    {    
        private Logger log;    
         
        [ImportingConstructor]    
        public GameService(Logger log)    
        {    
            this.log = log;    
        }    
    }    
         
    [Export]    
    public class Logger { }   
    ImportingConstructor 
    View Code
    Top
    收藏
    关注
    评论
  • 相关阅读:
    Ad hoc access to OLE DB provider 'Microsoft.ACE.OLEDB.12.0' has been denied. You must access this provider through a linked server.
    阻塞问题:会话是sleeping的,但是open_tran 不是0
    windows Server DNS服务器配置
    内存缺页
    "ros::NodeHandle"的用法:全局vs.私有
    python 判断当前执行用户是否是 root 用户
    docker 安装及启动 postgresql 及navicat 连接
    Mac 在环境变量中隐藏密码或者密钥等信息
    磁盘空间不足导致虚拟机无法启动
    VirtuaBox 下安装 Centos8 无法上网
  • 原文地址:https://www.cnblogs.com/hualiu0/p/5264297.html
Copyright © 2020-2023  润新知