namespace Model
{
public class MapperFactory
{
//聲明一個(gè)ISqlMapper接口類型的數(shù)據(jù)映射器 _mapper,其初始值為null
private static volatile ISqlMapper _mapper = null;
//private static log4net.Appender.AdoNetAppender adoApd = null;
static MapperFactory()
{
RefreshMapperSetting();
}
/// <summary>
/// 監(jiān)視變化,并提供變化后的處理方法
/// </summary>
/// <param name="obj"></param>
protected static void Configure(object obj)
{
//將數(shù)據(jù)映射器初始值置空
_mapper = null;
}
/// <summary>
/// 定義InitMapper方法-用于創(chuàng)建sql數(shù)據(jù)映射器
/// </summary>
protected static void InitMapper()
{
RefreshMapperSetting();
}
public static void RefreshMapperSetting()
{
//調(diào)用委托ConfigureHandler,創(chuàng)建委托變量handler
//用于SqlMap.config改變后的處理方法
ConfigureHandler handler = new ConfigureHandler(Configure);
//新建一個(gè)DomSqlMapBuilder類的實(shí)例builder
//此變量用于配置和監(jiān)視SqlMap.config的變化
DomSqlMapBuilder builder = new DomSqlMapBuilder();
//加入程序中自定義的鍵值對(duì)
//你可以在配置監(jiān)視之前加上自己的一些鍵值對(duì)內(nèi)容
NameValueCollection nvcProperties = new NameValueCollection();
nvcProperties.Add("DataSource", "PACTERA_GZF-PC");
nvcProperties.Add("DataBase", "TestOne");
nvcProperties.Add("UserName", "sa");
nvcProperties.Add("Password", "sa");
nvcProperties.Add("Timeout", "60");
builder.Properties = nvcProperties;
//啟用在配置之前的檢查
builder.ValidateSqlMapConfig = true;
//使用builder的ConfigureAndWatch生成一個(gè)ISqlMapper類型的數(shù)據(jù)映射器
_mapper = builder.ConfigureAndWatch("SqlMap.config", handler);
}
/// <summary>
/// 定義一個(gè)返回值為接口類型的方法Instance,用于實(shí)現(xiàn)sqlMapper實(shí)例化
/// </summary>
/// <returns></returns>
public static ISqlMapper Instance()
{
if (_mapper == null)
{
lock (typeof(SqlMapper))
{
if (_mapper == null) // double-check
{
//引用InitMapper,創(chuàng)建sqlMapper實(shí)例
InitMapper();
}
}
}
return _mapper;
}
/// <summary>
/// 定義一個(gè)Get方法,返回_mapper
/// </summary>
/// <returns></returns>
public static ISqlMapper Get()
{
return Instance();
}
}
}
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
