插件树(AddinTree)
模式:
模式是决定架构,还是架构决定模式?
常用模式
Singleton
Factory
Decorator
Strategy
Memento
Proxy
AddinTree
的建立
A=>
加载服务
ServiceManager
.Services.AddService(new IconService());
A.1=>
public
static ServiceManager Services {
get {
return defaultServiceManager;
}
}
public
ServiceManager()//bjarne private -> public
{
// add 'core' services
AddService(new PropertyService());
AddService(new StringParserService());
AddService(new FileUtilityService());
}
public
void AddService(IService service)
{
serviceList.Add(service);
//
装载服务的容器
}
B=>
ServiceManager
.Services.InitializeServicesSubsystem("/Workspace/Services");
B.1=>
public
void InitializeServicesSubsystem(string servicesPath)
{
处理插件树
// add add-in tree services"/Workspace/Services"
AddServices((IService[])AddInTreeSingleton.AddInTree.GetTreeNode(servicesPath).BuildChildItems(this).ToArray(typeof(IService)));
// B.1.1
//B.1.2//B.1.3
// initialize all services
foreach (IService service in serviceList) {
DateTime now = DateTime.Now;
service.InitializeService();
}
}
B.1.1=>
public static IAddInTree AddInTree {
get {
if (addInTree == null) {
CreateAddInTree();
创建插件树
}
return addInTree;
}
}
static
void CreateAddInTree()
创建插件树
{
addInTree = new DefaultAddInTree();
FileUtilityService fileUtilityService = (FileUtilityService)ServiceManager.Services.GetService(typeof(FileUtilityService));
//B.1.1.1
StringCollection addInFiles = null;
StringCollection retryList = null;
//
条件成立
if (ignoreDefaultCoreDirectory == false) {
addInFiles = fileUtilityService.SearchDirectory(defaultCoreDirectory, "*.addin");
//
取得所有插件文件//B.1.1.2
retryList = InsertAddIns(addInFiles);
//
加载插件//B.1.1.3
}
else
retryList = new StringCollection();
if (addInDirectories != null) {
foreach(string path in addInDirectories) {
addInFiles = fileUtilityService.SearchDirectory(path, "*.addin");
StringCollection partialRetryList = InsertAddIns(addInFiles);
//
加载插件
if (partialRetryList.Count != 0) {
string [] retryListArray = new string[partialRetryList.Count];
partialRetryList.CopyTo(retryListArray, 0);
retryList.AddRange(retryListArray);
}
}
}
//
重试加载插件
while (retryList.Count > 0) {
StringCollection newRetryList = InsertAddIns(retryList);
// break if no add-in could be inserted.
if (newRetryList.Count == retryList.Count) {
break;
}
retryList = newRetryList;
}
if (retryList.Count > 0) {
throw new ApplicationException("At least one AddIn uses an undefined codon or condition: " + retryList[0]);
}
// tree.ShowCodonTree();
}
B.1.1.1=>
取得服务
public
IService GetService(Type serviceType)
{
IService s = (IService)servicesHashtable[serviceType];
if (s != null) {
return s;
}