最近在做PACS的项目中想利用插件来加载各个不同的SCP的操作实现。比如Worklist的查询数据库,可以有多个实现。 比如MPPS的更新,也可以有多个实现。 为了统一弹性处理插件模块,增加了类型输入,用来只加载特定的服务的实现。
  [InheritedExport(typeof(ISCPBase))]
public interface ISCPBase
{
ISCPCfg SCPCfg
{
get;
set;
} string CustomModuleName
{
get;
}
}


  public class CustomExtensionManager
{
[ImportMany(typeof(ISCPBase), AllowRecomposition = true)]
public IEnumerable<ISCPBase> CustomModules { get; set; } public CompositionContainer Container; private Type[] _filterTypes; private ModulesManagerCfg _mgrCfg;
public ModulesManagerCfg ModulesManagerConfig
{
get
{
if (_mgrCfg == null)
{
try
{
_mgrCfg = XmlSerializeHelper<ModulesManagerCfg>.LoadFromFile(@"ModulesManagerCfg.xml");
}
catch (Exception ex)
{
Logger.ErrorWithFormat("Failed to load ModulesManagerCfg from ModulesManagerCfg.xml. {0}", ex.Message);
} if (_mgrCfg == null)
Logger.Error("ModulesManagerCfg is null, please check whether ModulesManagerCfg.xml exists.");
} return _mgrCfg;
}
} /// <summary>
/// Initialize the custom modules by interface type. It is well you can spec types.
/// If you do not care types, you can pass null. It will load all modules if inherited from ISCPBase.
/// </summary>
/// <param name="scpTypes">The type that inherited from ISCPBase. Like IWorklistQuery...</param>
public void Initialize(params Type[] scpTypes)
{
try
{
if (null != scpTypes)
{
_filterTypes = scpTypes; var catalog = new AggregateCatalog(new TypeCatalog(scpTypes), new DirectoryCatalog(AppDomain.CurrentDomain.BaseDirectory)); Container = new CompositionContainer(catalog);
}
else
{
var catalog = new AggregateCatalog(new TypeCatalog(scpTypes), new DirectoryCatalog(AppDomain.CurrentDomain.BaseDirectory)); Container = new CompositionContainer(catalog);
} Container.ComposeParts(this);
}
catch (Exception ex)
{
Logger.ErrorWithFormat("Failed to initialize CustomExtensionManager. {0}", ex.Message);
}
} public ISCPBase GetCurrentUsedModule()
{
if (null == ModulesManagerConfig)
{
throw new ArgumentNullException("ModulesManagerCfg is null, please check whether ModulesManagerCfg.xml exists.");
} if (String.IsNullOrEmpty(ModulesManagerConfig.CurrentUsedModuleName))
{
throw new Exception("CurrentUsedModuleName is empty, please check ModulesManagerCfg.xml.");
} if (null == Container)
{
throw new Exception("Modules container is null, you can check your custom module and call Initialize() function to load modules.");
} //var m = _container.GetExports<ISCPBase>()
// .Where(e => e.Value.CustomModuleName.Equals(ModulesManagerConfig.CurrentUsedModuleName))
// .FirstOrDefault(); //if (null != m)
//{
// return m.Value;
//} //return null; if (null == _filterTypes)
{
var m = CustomModules.FirstOrDefault(e => e.CustomModuleName.Equals(ModulesManagerConfig.CurrentUsedModuleName));
return m;
}
else
{
var m = from c in CustomModules
                         where _filterTypes.All(f => f.IsAssignableFrom(c.GetType()))
                         select c;

                return m.FirstOrDefault();
}
}
}

最新文章

  1. js给DropdownList赋值
  2. DataTable常用代码
  3. 彻底理解JavaScript原型
  4. [Leetcode] Recover Binary Search Tree
  5. 通过命令名称查询进程id
  6. [js高手之路]html5 canvas动画教程 - 下雪效果
  7. UML 类图基础
  8. [ Java学习基础 ] Java的封装性与访问控制
  9. Dubbo支持的协议的详解
  10. jQuery 图片查看插件 Magnify 开发简介(仿 Windows 照片查看器)
  11. windows下用XShell远程控制ubuntu时连接失败
  12. 20175314 《Java程序设计》第八周学习总结
  13. Java桌面程序打包成exe可执行文件
  14. centos关机、重启、图形界面与命令行界面切换命令
  15. 第02章:MongoDB安装
  16. java 实现多重继承
  17. mui中图片手势缩放功能的实现
  18. [框架模式]经典的模型视图控制器模式MVC
  19. JAVA Spring JdbcTemplate ( 以 SQLSERVER 为例 ) 的简单使用
  20. org-mode入门教程

热门文章

  1. log4j:WARN Please initialize the log4j system properly.
  2. HDFS源码分析心跳汇报之整体结构
  3. WebStorm 常用功能
  4. Appium python自动化测试系列之使用HTMLTestRunner生成测试报告(十三)
  5. erlang中判断进程是否存活
  6. HTTP POST请求数据提交格式(转)
  7. 创业做移动互联网App的4个注意事项
  8. HDU 2473 Junk-Mail Filter(并查集的删除操作)
  9. 【BZOJ4166】月宫的符卡序列 Manacher+hash
  10. 使用 Django1.11搭建blog项目