namespace Model
{
public class MapperFactory
{
//聲明一個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);
//新建一個DomSqlMapBuilder類的實例builder
//此變量用于配置和監(jiān)視SqlMap.config的變化
DomSqlMapBuilder builder = new DomSqlMapBuilder();
//加入程序中自定義的鍵值對
//你可以在配置監(jiān)視之前加上自己的一些鍵值對內(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生成一個ISqlMapper類型的數(shù)據(jù)映射器
_mapper = builder.ConfigureAndWatch("SqlMap.config", handler);
}
/// <summary>
/// 定義一個返回值為接口類型的方法Instance,用于實現(xiàn)sqlMapper實例化
/// </summary>
/// <returns></returns>
public static ISqlMapper Instance()
{
if (_mapper == null)
{
lock (typeof(SqlMapper))
{
if (_mapper == null) // double-check
{
//引用InitMapper,創(chuàng)建sqlMapper實例
InitMapper();
}
}
}
return _mapper;
}
/// <summary>
/// 定義一個Get方法,返回_mapper
/// </summary>
/// <returns></returns>
public static ISqlMapper Get()
{
return Instance();
}
}
}
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

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