Service:服务层分析 总纲
Service在SharpDevelop中的用途:需要一些静态辅助类,协助主流程完成一些额外扩展的细小琐碎的功能,例如OpenFile功能,例如获取编译器输出位置。原先SharpDevelop使用只含静态成员的类,但是后来发现需要定义多于10个以上的静态类,不易维护;而且难以替换,难以扩展。现在0.92版本使用ServiceManager单件访问这些辅助功能。
Service的结构在Core/Service中:
1.IService接口:
注意,SharpDevelop在InitializeService()方法中进行初始化,而不是在构造函数中。这是因为有些服务在初始化之前就其作用了,所以先加载Core后加载Base,而且在各自的Initialize()方法中要判断IsInitialized才可以执行
2.AbstractService,为IService的抽象实现,略过。
3.ServiceManager单件:
仔细地看GetService()方法,发现这是一个注册工厂。
这里,服务并行存储于ArrayList和HashTable。执行GetService()方法时,先在HashTable中查找,如果找不到再去ArrayList中查找,找到了在返回值之前同时插入HashTable,这样就提高了查询速度。
注意,HashTable只在这个方法里面使用。
InitializeServicesSubsystem()方法用于Main()函数中,用于添加4个Core服务以及11个Base服务,同时对每个服务初始化(调用IService的InitializeService()方法)。
UnloadAllServices()方法也用于Main()函数中,用于卸载所有服务(调用IService的UnloadService()方法)。
Core中的Service有4个
1.File:FileUtilityService.cs
用于通用文件操作,并提供文件图标操作。
2.Property
访问SharpDevelop全局属性的服务。
3.Resource
本地化管理器。
4.StringParser
在字符串中定义属性的服务。
Base中的Service定义在SharpDevelop.Addin中:32-55行,共计11个, 注意,path为"/Workspace/Services"
接下来,分析所有的15个服务。
Service的结构在Core/Service中:
1.IService接口:
注意,SharpDevelop在InitializeService()方法中进行初始化,而不是在构造函数中。这是因为有些服务在初始化之前就其作用了,所以先加载Core后加载Base,而且在各自的Initialize()方法中要判断IsInitialized才可以执行
2.AbstractService,为IService的抽象实现,略过。
3.ServiceManager单件:
仔细地看GetService()方法,发现这是一个注册工厂。
public IService GetService(Type serviceType)
{
IService s = (IService)servicesHashtable[serviceType];
if (s != null)
{
return s;
}
foreach (IService service in serviceList)
{
if (serviceType.IsInstanceOfType(service))
{
servicesHashtable[serviceType] = service;
return service;
}
}
return null;
}
{
IService s = (IService)servicesHashtable[serviceType];
if (s != null)
{
return s;
}
foreach (IService service in serviceList)
{
if (serviceType.IsInstanceOfType(service))
{
servicesHashtable[serviceType] = service;
return service;
}
}
return null;
}
这里,服务并行存储于ArrayList和HashTable。执行GetService()方法时,先在HashTable中查找,如果找不到再去ArrayList中查找,找到了在返回值之前同时插入HashTable,这样就提高了查询速度。
注意,HashTable只在这个方法里面使用。
InitializeServicesSubsystem()方法用于Main()函数中,用于添加4个Core服务以及11个Base服务,同时对每个服务初始化(调用IService的InitializeService()方法)。
UnloadAllServices()方法也用于Main()函数中,用于卸载所有服务(调用IService的UnloadService()方法)。
Core中的Service有4个
1.File:FileUtilityService.cs
用于通用文件操作,并提供文件图标操作。
2.Property
访问SharpDevelop全局属性的服务。
3.Resource
本地化管理器。
4.StringParser
在字符串中定义属性的服务。
Base中的Service定义在SharpDevelop.Addin中:32-55行,共计11个, 注意,path为"/Workspace/Services"
<Extension path = "/Workspace/Services">
<Class id = "ProjectService"
class = "ICSharpCode.SharpDevelop.Services.DefaultProjectService"/>
<Class id = "FileService"
class = "ICSharpCode.SharpDevelop.Services.DefaultFileService"/>
<Class id = "ParserService"
class = "ICSharpCode.SharpDevelop.Services.DefaultParserService"/>
<Class id = "TaskService"
class = "ICSharpCode.SharpDevelop.Services.TaskService"/>
<Class id = "StatusBarService"
class = "ICSharpCode.SharpDevelop.Services.DefaultStatusBarService"/>
<Class id = "ToolbarService"
class = "ICSharpCode.SharpDevelop.Services.ToolbarService"/>
<Class id = "LanguageService"
class = "ICSharpCode.SharpDevelop.Services.LanguageService"/>
<Class id = "ClassBrowserIconsService"
class = "ICSharpCode.SharpDevelop.Services.ClassBrowserIconsService"/>
<Class id = "LanguageBindingService"
class = "ICSharpCode.SharpDevelop.Services.LanguageBindingService"/>
<Class id = "DisplayBindingService"
class = "ICSharpCode.SharpDevelop.Services.DisplayBindingService"/>
<Class id = "AmbienceService"
class = "ICSharpCode.SharpDevelop.Services.AmbienceService"/>
</Extension>
Base中的每个Service都相应的有一个接口和一个抽象类。<Class id = "ProjectService"
class = "ICSharpCode.SharpDevelop.Services.DefaultProjectService"/>
<Class id = "FileService"
class = "ICSharpCode.SharpDevelop.Services.DefaultFileService"/>
<Class id = "ParserService"
class = "ICSharpCode.SharpDevelop.Services.DefaultParserService"/>
<Class id = "TaskService"
class = "ICSharpCode.SharpDevelop.Services.TaskService"/>
<Class id = "StatusBarService"
class = "ICSharpCode.SharpDevelop.Services.DefaultStatusBarService"/>
<Class id = "ToolbarService"
class = "ICSharpCode.SharpDevelop.Services.ToolbarService"/>
<Class id = "LanguageService"
class = "ICSharpCode.SharpDevelop.Services.LanguageService"/>
<Class id = "ClassBrowserIconsService"
class = "ICSharpCode.SharpDevelop.Services.ClassBrowserIconsService"/>
<Class id = "LanguageBindingService"
class = "ICSharpCode.SharpDevelop.Services.LanguageBindingService"/>
<Class id = "DisplayBindingService"
class = "ICSharpCode.SharpDevelop.Services.DisplayBindingService"/>
<Class id = "AmbienceService"
class = "ICSharpCode.SharpDevelop.Services.AmbienceService"/>
</Extension>
接下来,分析所有的15个服务。