实例讲解设计模式之单件模式应用和简单注入实现形式(3)
/// <summary>
/// 获取服务
/// </summary>
/// <param name="serviceType">服务类型</param>
/// <param name="throwExceptionIfNotFound"></param>
/// <returns></returns>
public object GetService(Type serviceType)
{
if (ServiceTypes[serviceType.FullName] == null)
{
throw new Exception(serviceType.FullName + " 服务未找到!");
}
return ServiceTypes[serviceType.FullName];
}
/// <summary>
/// 加载服务
/// </summary>
public void Load()
{
if (this.ServiceTypes.Count > 0)
{
this.ServiceTypes.Clear();
}
lock (typeof(ServiceManager))
{
this.AllServiceAdd();
}
}
/// <summary>
/// 所有服务加载
/// </summary>
private void AllServiceAdd()
{
XmlDocument document = new XmlDocument();
Type type = null;
Type serviceType = null;
// boot.config启动文件
document.Load(@"D:\RayGu\Visual Studio 2008\WebSites\WebSite3\boot.config");
XmlElement documentElement = document.DocumentElement;
if (!((documentElement != null) && string.Equals(documentElement.Name, "Boot")))
{
throw new Exception(" boot.config服务配置失败!");
}
foreach (XmlNode node in documentElement.SelectNodes("/Boot/Services/Service"))
{
if (node.NodeType == XmlNodeType.Element)
{
XmlElement element2 = (XmlElement)node;
// 驱动
string attribute = element2.GetAttribute("Driver");
// 服务类型
string typeFullName = element2.GetAttribute("Type");
if (attribute == null)
{
throw new Exception(serviceType.FullName + " 服务驱动加载失败!");
}
type = ServiceManager.GetType(attribute);
serviceType = ServiceManager.GetType(typeFullName);
if (((type != null) && (serviceType != null)))
{
this.AcquireObject(type, serviceType.FullName);
}
else
{
throw new Exception(serviceType.FullName + " 服务加载失败!");
}
}
}
}
public object AcquireObject(Type type, string serviceTypeFullname)
{
if (this.ServiceTypes.Contains(serviceTypeFullname))
{
return this.ServiceTypes[serviceTypeFullname];
}
lock (this.ServiceTypes.SyncRoot)
{
this.ServiceTypes.Add(serviceTypeFullname, Activator.CreateInstance(type));
return this.ServiceTypes[serviceTypeFullname];
}
}
public static Type GetType(string typeFullName)
{
Type type = Type.GetType(typeFullName);
if (type != null)
{
return type;
}
string name = typeFullName.Substring(0, typeFullName.IndexOf(","));
string str2 = typeFullName.Substring(typeFullName.IndexOf(",") + 1);
string assemblyName = (str2.IndexOf(",") >= 0) ? str2.Substring(0, str2.IndexOf(",")) : str2;
name = name.Trim();
assemblyName = assemblyName.Trim();
if ((assemblyName != null) && (name != null))
{
Assembly assemblyByPartialName = GetAssemblyByPartialName(assemblyName);
if (assemblyByPartialName != null)
{
return assemblyByPartialName.GetType(name);
}
}
return null;
}
public static Assembly GetAssemblyByPartialName(string assemblyName)
{
string str = assemblyName + ",";
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly assembly in assemblies)
{
if (assembly.FullName.ToLower().StartsWith(str.ToLower()))
{
return assembly;
}
}
return null;
}
通过配置加载
<?xml version="1.0" encoding="utf-8" ?>
<Boot>
<Services>
<Service Type="IService.IUserService, IService" Driver="Service.UserService, Service" />
</Services>
</Boot>
页面实现过程
public static IUserService UserService
{
get
{
return (IUserService)ServiceManager.Instance.GetService(typeof(IUserService));
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Users users = UserService.Load();
string output = "";
output += "<table><tr><th>编号</th><th>用户名</th><th>密码</th></tr>";
foreach (User user in users)
{
output += "<tr><td>" + user.Id + "</td><td>" + user.Name + "</td><td>" + user.Password + "</td></tr>";
}
output += "</table>";
this.PlaceHolder1.Controls.Add(new LiteralControl(output));
}
}
当然在实际开发过程中,通过多线程加载服务,控制多线程的单实例。
最后附上下载实例代码。
/Files/guguangye/WebSite3.rar
下次再介绍下工厂模式在实际应用中的示例。