.Net之美读书笔记13
.Net应用程序配置
.Net中的配置文件与读取
.Net中Config文件有web.config和App.config文件,这里用App.config举例(需要引用程序集:System.Configuration)。
<appSettings>
<add key="SiteName" value="WWW.baidu.com"/>
</appSettings>
string siteName = ConfigurationManager.AppSettings["SiteName"];
Console.WriteLine(siteName);
使用内置节点和.Net内置处理程序
应用配置Appconfig跟节点下
<configSections>
<section name="mailServer" type="System.Configuration.SingleTagSectionHandler,System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<mailServer address="mail.163.com" userName="LoveTomato" password="123456"></mailServer>
程序读取
Hashtable mailServer = (Hashtable)ConfigurationManager.GetSection("mailServer");
string address = mailServer["address"].ToString();
string userName = mailServer["userName"].ToString();
string password = mailServer["password"].ToString();
Console.WriteLine("Address:{0},UserName:{1},PassWord:{2}",address,userName,password);
使用自定义节点和自定义处理
这种功能更灵活强大,自定义处理可以通过实现接口IConfigurationSectionHandler
或继承类ConfigurationSection
,这里演示实现接口
新建配置文件
<configSections>
<section name="mailServerGroup" type="CustomConfig.MailServerConfigurationHandler,CustomConfig"/>
</configSections>
<mailServerGroup provider="www.baidu.com">
<mailServer client="163">
<address>mail.163.com</address>
<userName>Jerry</userName>
<passWord>123456</passWord>
</mailServer>
<mailServer client="qq">
<address>mail.qq.com</address>
<userName>Jerry</userName>
<passWord>123456</passWord>
</mailServer>
</mailServerGroup>
新建类库,其下文件MailServer.cs
对应 节点对象,MailServerConfig.cs
存储节点对象集合,MailServerConfigurationHandler.cs
实现接口IConfigurationSectionHandler
功能读取配置文件保存到集合。
public class MailServer
{
private Hashtable serverNode;
public MailServer()
{
serverNode = new Hashtable();
}
public Hashtable ServerNode
{
get
{
return serverNode;
}
}
public string Client
{
get
{
return serverNode["client"] as string;
}
}
public string Address
{
get
{
return serverNode["address"] as string;
}
}
public string UserName
{
get
{
return serverNode["userName"] as string;
}
}
public string Password
{
get
{
return serverNode["passWord"] as string;
}
}
}
public class MailServerConfig:List<MailServer>
{
public string Provider { get; set; }
}
public class MailServerConfigurationHandler : IConfigurationSectionHandler
{
public object Create(object parent, object configContext, XmlNode section)
{
MailServerConfig config = new MailServerConfig();
config.Provider = section.Attributes["provider"] == null ? string.Empty : section.Attributes["provider"].Value;
foreach(XmlNode child in section.ChildNodes)
{
MailServer server = new MailServer();
if (child.Attributes["client"] != null)
server.ServerNode.Add("client", child.Attributes["client"].Value);
foreach(XmlNode grandChild in child.ChildNodes)
{
server.ServerNode.Add(grandChild.Name, grandChild.InnerText);
}
config.Add(server);
}
return config;
}
}
主程序中调用
//该方法会调用MailServerConfigurationHandler类的Create方法,从而实现映射
MailServerConfig serverGroup = (MailServerConfig)ConfigurationManager.GetSection("mailServerGroup");
Console.WriteLine("Provider:".PadRight(12) + serverGroup.Provider);
foreach(MailServer config in serverGroup)
{
string str = string.Format("Client:{0},Address:{1},UserName:{2},PassWord:{3}",config.Client,config.Address,config.UserName,config.Password);
Console.WriteLine(str);
}
存储实例
存储实例应用比较广,Ioc
依赖倒置也是在其基础上实现。这里简单说下实现思想
- 接口化方式编程,定义接口和实现接口的类
- 通过配置文件指定采用实现接口的那个类型
- 自定义处理返回指定类型的实例
- 返回的实例赋值给接口
总结
本节讲了.Net中的配置,这里的目的不是自己编写配置,而是便于类型中用到这样配置易于理解(如EntityFrameWork)。如果向自己编写还需进一步研究