Enterprise Library 是一個為了由創建復雜企業級應用的開發人員使用的應用程序塊的集合。這些應用通常部署廣泛且與其他應用和系統相互依賴。另外,他們通常有嚴格的安全、可靠性和性能需求。
1、安裝 Enterprise Library
從官網下載 Enterprise Library 並安裝,將類庫引用到項目中。
下載地址:https://www.microsoft.com/en-us/download/details.aspx?id=15104
2、定義配置類
WeilogSettings 類:
using System;
using System.Configuration;
using System.ComponentModel;
using System.Security.Permissions;
using System.Configuration.Provider;
// 引用 Weilog 公共程序塊公共類庫屬性空間。
using Weilog.Common.Properties;
// 引用微軟企業類庫公共應用程序塊配置空間。
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
namespace Weilog.Common.Configuration
{
/// <summary>
/// 定義配置設置以支持用來配置和管理 Weilog 的基礎結構。無法繼承此類。
/// </summary>
public class WeilogSettings : SerializableConfigurationSection
{
#region Fields...
/// <summary>
/// 初始化配置節屬性集合。
/// </summary>
private static ConfigurationPropertyCollection properties = new ConfigurationPropertyCollection();
/// <summary>
/// 初始化 Weilog 類庫應用程序名稱。
/// </summary>
private static readonly ConfigurationProperty propApplicationName = new ConfigurationProperty("applicationName", typeof(String), "Weilog", null, StdValidatorsAndConverters.NonEmptyStringValidator, ConfigurationPropertyOptions.IsRequired);
/// <summary>
/// 初始化 Weilog 類庫應用程序名稱。
/// </summary>
private static readonly ConfigurationProperty propName = new ConfigurationProperty("name", typeof(String), "Weilog", null, StdValidatorsAndConverters.NonEmptyStringValidator, ConfigurationPropertyOptions.IsRequired);
/// <summary>
/// 初始化指定在 <connectionStrings> 元素中定義的連接字符串的名稱。
/// </summary>
private static readonly ConfigurationProperty propConnectionStringName = new ConfigurationProperty("connectionStringName", typeof(String), "WeilogConnectionString", null, StdValidatorsAndConverters.NonEmptyStringValidator, ConfigurationPropertyOptions.IsRequired);
/// <summary>
/// 初始化數據源類型的名稱。
/// </summary>
private static readonly ConfigurationProperty propType = new ConfigurationProperty("type", typeof(String), "Weilog.Data.SqlServer", null, StdValidatorsAndConverters.NonEmptyStringValidator, ConfigurationPropertyOptions.IsRequired);
/// <summary>
/// 初始化允許的無效密碼或無效密碼提示問題答案嘗試的次數。
/// </summary>
private static readonly ConfigurationProperty propInterval = new ConfigurationProperty("interval", typeof(Int32), "60000", null, StdValidatorsAndConverters.PositiveIntegerValidator, ConfigurationPropertyOptions.None);
/// <summary>
/// Weilog 塊配置節名稱。
/// </summary>
public const string SectionName = "WeilogConfiguration";
#endregion
#region Constructors...
/// <summary>
/// 初始化 <see cref="WeilogSettings"/> 靜態類。
/// </summary>
static WeilogSettings()
{
// 添加配置節屬性對象到集合。
properties.Add(propApplicationName);
properties.Add(propConnectionStringName);
properties.Add(propType);
properties.Add(propName);
properties.Add(propInterval);
}
/// <summary>
/// 初始化 <see cref="WeilogSettings"/> 類的新實例。
/// </summary>
private WeilogSettings()
{
}
#endregion
#region Methods...
#endregion
#region Properties...
/// <summary>
/// 獲取或設置要存儲和檢索其成員資格信息的應用程序的名稱。
/// </summary>
/// <value>應用程序的名稱,將存儲和檢索該應用程序的成員資格信息。</value>
[StringValidator(MinLength = 1), ConfigurationProperty("applicationName", DefaultValue = "Weilog", IsRequired = true)]
public string ApplicationName
{
get
{
return (string)base[propApplicationName];
}
set
{
if (String.IsNullOrEmpty(value))
{
throw new ArgumentNullException("ApplicationName");
}
if (value.Length > 0x100)
{
throw new ProviderException("Provider_application_name_too_long");
}
base[propApplicationName] = value;
}
}
/// <summary>
/// 獲取或設置要存儲和檢索其成員資格信息的應用程序的名稱。
/// </summary>
/// <value>應用程序的名稱,將存儲和檢索該應用程序的成員資格信息。</value>
[StringValidator(MinLength = 1), ConfigurationProperty("name", DefaultValue = "Weilog", IsRequired = true)]
public string Name
{
get
{
return (string)base[propName];
}
set
{
if (String.IsNullOrEmpty(value))
{
throw new ArgumentNullException("Name");
}
if (value.Length > 0x100)
{
throw new ProviderException("Provider_name_too_long");
}
base[propName] = value;
}
}
/// <summary>
/// 獲取或設置數據庫的連接名稱。
/// </summary>
/// <value>一個字符串,用於指定 connectionStrings 配置節內的數據庫連接字符串的名稱。</value>
[StringValidator(MinLength = 1), ConfigurationProperty("connectionStringName", DefaultValue = "WeilogConnectionString", IsRequired = true)]
public string ConnectionStringName
{
get
{
return (string)base[propConnectionStringName];
}
set
{
base[propConnectionStringName] = value;
}
}
/// <summary>
/// 獲取要連接的數據源類型的名稱。
/// </summary>
/// <value>要連接的數據源類型的名稱。</value>
[StringValidator(MinLength = 1), ConfigurationProperty("type", DefaultValue = "Weilog.Data.SqlServer", IsRequired = true)]
public string Type
{
get
{
return (string)base[propType];
}
set
{
base[propType] = value;
}
}
/// <summary>
/// 獲取鎖定成員資格用戶前允許的無效密碼或無效密碼提示問題答案嘗試次數。
/// </summary>
/// <value>鎖定成員資格用戶之前允許的無效密碼或無效密碼提示問題答案嘗試次數。</value>
[ConfigurationProperty("interval", DefaultValue = "60000"), TypeConverter(typeof(Int32))]
public int Interval
{
get
{
return (int)base[propInterval];
}
set
{
base[propInterval] = value;
}
}
/// <summary>
/// 獲取屬性的集合。
/// </summary>
/// <value><see cref="ConfigurationPropertyCollection"/> 元素的屬性集合。</value>
/// <remarks>Properties 屬性 (Property),也被稱為屬性包,包含應用於該元素的所有屬性 (Property) 或屬性 (Attribute)。</remarks>
protected override ConfigurationPropertyCollection Properties
{
get
{
return properties;
}
}
#endregion
}
}
WeilogConfigurationManager 類:
using System;
using System.IO;
using System.Configuration;
using System.Collections.Specialized;
// 引用 Weilog 公共程序塊公共類庫屬性空間。
using Weilog.Common.Properties;
// 引用微軟企業類庫公共應用程序塊配置空間。
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
namespace Weilog.Common.Configuration
{
/// <summary>
/// 提供對客戶端應用程序配置文件的訪問。無法繼承此類。
/// </summary>
public static class WeilogConfigurationManager
{
#region Fields...
#endregion
#region Constructors...
/// <summary>
/// 初始化 <see cref="WeilogConfigurationManager"/> 靜態類。
/// </summary>
static WeilogConfigurationManager()
{
}
#endregion
#region Methods...
/// <summary>
/// 檢索當前應用程序默認配置的指定配置節。
/// </summary>
/// <param name="sectionName">配置節的路徑和名稱。</param>
/// <returns>指定的 <see cref="ConfigurationSection"/> 對象,或者,如果該節不存在,則為 空引用(在 Visual Basic 中為 Nothing)。</returns>
private static ConfigurationSection GetSection(string sectionName)
{
// 聲明配置節點類引用。
ConfigurationSection _section = null;
// 聲明配置源引用。
SystemConfigurationSource _systemConfSource = null;
try
{
// 初始化配置源對象。
_systemConfSource = (SystemConfigurationSource)ConfigurationSourceFactory.Create();
// 初始化配置信息。
_section = _systemConfSource.GetSection(sectionName);
}
catch
{
throw;
}
finally
{
// 清理對象。
_systemConfSource = null;
}
return _section;
}
#endregion
#region Properties...
/// <summary>
/// 獲取 Weilog 應用程序塊 <see cref="WeilogSettings"/> 配置信息。
/// </summary>
/// <value>一個 <see cref="WeilogSettings"/> 對象。</value>
public static WeilogSettings WeilogConfiguration
{
get
{
// 初始化 Weilog 配置節信息。
WeilogSettings weilogSetting = (WeilogSettings)GetSection(WeilogSettings.SectionName);
return weilogSetting;
}
}
#endregion
}
}
StdValidatorsAndConverters 類:
using System;
using System.ComponentModel;
using System.Configuration;
namespace Weilog.Common.Configuration
{
/// <summary>
/// 提供一組對成員資格配置節的驗證和轉換標准方法。
/// </summary>
internal static class StdValidatorsAndConverters
{
#region Fields...
/// <summary>
/// 聲明無窮 <see cref="TimeSpan"/> 值的類型轉換器的引用。
/// </summary>
private static TypeConverter _infiniteTimeSpanConverter = null;
/// <summary>
/// 聲明非空字符串配置驗證器的引用。
/// </summary>
private static ConfigurationValidatorBase _nonEmptyStringValidator = null;
/// <summary>
/// 聲明非零負整數驗證器的引用。
/// </summary>
private static ConfigurationValidatorBase _nonZeroPositiveIntegerValidator = null;
/// <summary>
/// 聲明負整數驗證器的引用。
/// </summary>
private static ConfigurationValidatorBase _positiveIntegerValidator = null;
/// <summary>
/// 聲明負的 <see cref="TimeSpan"/> 值驗證器的引用。
/// </summary>
private static ConfigurationValidatorBase _positiveTimeSpanValidator = null;
/// <summary>
/// 聲明 <see cref="TimeSpan"/> 值的分鍾數轉換器的引用。
/// </summary>
private static TypeConverter _timeSpanMinutesConverter = null;
/// <summary>
/// 聲明 <see cref="TimeSpan"/> 值的無窮分鍾數的轉換器的引用。
/// </summary>
private static TypeConverter _timeSpanMinutesOrInfiniteConverter = null;
/// <summary>
/// 聲明 <see cref="TimeSpan"/> 值的秒鍾數轉換器的引用。
/// </summary>
private static TypeConverter _timeSpanSecondsConverter = null;
/// <summary>
/// 聲明 <see cref="TimeSpan"/> 值的無窮秒鍾數轉換器的引用。
/// </summary>
private static TypeConverter _timeSpanSecondsOrInfiniteConverter = null;
/// <summary>
/// 聲明字符串白空格轉換器的引用。
/// </summary>
private static TypeConverter _whiteSpaceTrimStringConverter = null;
#endregion
#region Constructors...
/// <summary>
/// 初始化 <see cref="StdValidatorsAndConverters"/> 靜態類。
/// </summary>
static StdValidatorsAndConverters()
{
}
#endregion
#region Methods...
#endregion
#region Properties...
/// <summary>
/// 獲取無窮大的 <see cref="TimeSpan"/> 值轉換器實例。
/// </summary>
/// <value>一個無窮大的 <see cref="TimeSpan"/> 值轉換器實例。</value>
internal static TypeConverter InfiniteTimeSpanConverter
{
get
{
if (_infiniteTimeSpanConverter == null)
{
_infiniteTimeSpanConverter = new InfiniteTimeSpanConverter();
}
return _infiniteTimeSpanConverter;
}
}
/// <summary>
/// 獲取非空字符串驗證器實例。
/// </summary>
/// <value>一個非空字符串驗證器實例。</value>
internal static ConfigurationValidatorBase NonEmptyStringValidator
{
get
{
if (_nonEmptyStringValidator == null)
{
_nonEmptyStringValidator = new StringValidator(1);
}
return _nonEmptyStringValidator;
}
}
/// <summary>
/// 獲取非零負整數驗證器實例。
/// </summary>
/// <value>一個非零負整數驗證器實例。</value>
internal static ConfigurationValidatorBase NonZeroPositiveIntegerValidator
{
get
{
if (_nonZeroPositiveIntegerValidator == null)
{
_nonZeroPositiveIntegerValidator = new IntegerValidator(1, 0x7fffffff);
}
return _nonZeroPositiveIntegerValidator;
}
}
/// <summary>
/// 獲取負整數驗證器實例。
/// </summary>
/// <value>一個負整數驗證器實例。</value>
internal static ConfigurationValidatorBase PositiveIntegerValidator
{
get
{
if (_positiveIntegerValidator == null)
{
_positiveIntegerValidator = new IntegerValidator(0, 0x7fffffff);
}
return _positiveIntegerValidator;
}
}
/// <summary>
/// 獲取負的 <see cref="TimeSpan"/> 值的驗證器實例。
/// </summary>
/// <value>一個負的 <see cref="TimeSpan"/> 值的驗證器實例。</value>
internal static ConfigurationValidatorBase PositiveTimeSpanValidator
{
get
{
if (_positiveTimeSpanValidator == null)
{
_positiveTimeSpanValidator = new PositiveTimeSpanValidator();
}
return _positiveTimeSpanValidator;
}
}
/// <summary>
/// 獲取 <see cref="TimeSpan"/> 值分鍾數轉換器實例。
/// </summary>
/// <value>一個 <see cref="TimeSpan"/> 值分鍾數轉換器實例。</value>
internal static TypeConverter TimeSpanMinutesConverter
{
get
{
if (_timeSpanMinutesConverter == null)
{
_timeSpanMinutesConverter = new TimeSpanMinutesConverter();
}
return _timeSpanMinutesConverter;
}
}
/// <summary>
/// 獲取 <see cref="TimeSpan"/> 值無窮分鍾數轉換器實例。
/// </summary>
/// <value>一個 <see cref="TimeSpan"/> 值無窮分鍾數轉換器實例。</value>
internal static TypeConverter TimeSpanMinutesOrInfiniteConverter
{
get
{
if (_timeSpanMinutesOrInfiniteConverter == null)
{
_timeSpanMinutesOrInfiniteConverter = new TimeSpanMinutesOrInfiniteConverter();
}
return _timeSpanMinutesOrInfiniteConverter;
}
}
/// <summary>
/// 獲取 <see cref="TimeSpan"/> 值秒鍾數轉換器實例。
/// </summary>
/// <value>一個 <see cref="TimeSpan"/> 值秒鍾數轉換器實例。</value>
internal static TypeConverter TimeSpanSecondsConverter
{
get
{
if (_timeSpanSecondsConverter == null)
{
_timeSpanSecondsConverter = new TimeSpanSecondsConverter();
}
return _timeSpanSecondsConverter;
}
}
/// <summary>
/// 獲取 <see cref="TimeSpan"/> 值無窮秒鍾數轉換器實例。
/// </summary>
/// <value>一個 <see cref="TimeSpan"/> 值無窮秒鍾數轉換器實例。</value>
internal static TypeConverter TimeSpanSecondsOrInfiniteConverter
{
get
{
if (_timeSpanSecondsOrInfiniteConverter == null)
{
_timeSpanSecondsOrInfiniteConverter = new TimeSpanSecondsOrInfiniteConverter();
}
return _timeSpanSecondsOrInfiniteConverter;
}
}
/// <summary>
/// 獲取字符串白空格轉換器實例。
/// </summary>
/// <value>一個字符串白空格轉換器實例。</value>
internal static TypeConverter WhiteSpaceTrimStringConverter
{
get
{
if (_whiteSpaceTrimStringConverter == null)
{
_whiteSpaceTrimStringConverter = new WhiteSpaceTrimStringConverter();
}
return _whiteSpaceTrimStringConverter;
}
}
#endregion
}
}
3、定義數據庫基類
DbProviderBase 類:
using System;
using System.Collections.Specialized;
using System.Configuration;
//引用 Weilog 公共程序塊數據庫提供程序屬性空間。
using Weilog.DbProvider.Properties;
//引用 Weilog 公共程序塊配置空間。
using Weilog.Common.Configuration;
namespace Weilog.DbProvider
{
/// <summary>
/// 可擴展的數據庫提供程序模型的基類。
/// </summary>
public abstract class DbProviderBase
{
#region Fields...
/// <summary>
/// 聲明數據服務名字符串引用。
/// </summary>
private string _dbservicestr = null;
/// <summary>
/// 聲明應用程序名稱的字符串引用。
/// </summary>
private string _name = null;
/// <summary>
/// 聲明全局應用程序名稱的字符串引用。
/// </summary>
private string _applicationName = null;
#endregion
#region Constructors...
/// <summary>
/// 初始化 <see cref="DbProviderBase"/> 類的新實例。
/// </summary>
protected DbProviderBase()
{
//引用企業安全類庫配置節類引用。
WeilogSettings _secSection = null;
try
{
// 初始化企業安全類庫配置節信息。
_secSection = WeilogConfigurationManager.WeilogConfiguration;
// 判斷企業安全類庫配置節是否初始化成功。
if (_secSection == null)
{
// 配置系統未能初始化。
throw new Exception("Config_client_config_init_error");
}
// 初始化數據服務名稱字符串。
this._dbservicestr = _secSection.ConnectionStringName;
// 初始化應用程序名。
this._name = _secSection.Name;
// 初始化全局應用程序名稱。
this._applicationName = _secSection.ApplicationName;
}
catch
{
throw;
}
finally
{
// 清理對象。
_secSection = null;
}
}
#endregion
#region Properties...
/// <summary>
/// 獲取全局應用程序名稱。
/// </summary>
/// <value>全局應用程序名稱。</value>
public string ApplicationName
{
get
{
return this._applicationName;
}
}
/// <summary>
/// 使用自定義成員資格提供程序的應用程序的名稱。
/// </summary>
/// <value>使用自定義成員資格提供程序的應用程序的名稱。</value>
protected string Name
{
get
{
return this._name;
}
}
/// <summary>
/// 獲取數據訪問服務名稱。
/// </summary>
/// <value>數據訪問服務名稱。</value>
public string DbServiceName
{
get
{
return this._dbservicestr;
}
}
#endregion
}
}
4、配置 Web.config
<configSections>
<section name="weilogConfiguration" type="Weilog.Common.Configuration.WeilogSettings, Weilog.Common"/>
<weilogConfiguration name="Weilog.Web" applicationName="Weilog" connectionStringName="weilogConnectionString"
type="Weilog.Web.Repository" interval="60000"/>
<connectionStrings>
<add name="weilogConnectionString"
connectionString="Data Source=192.168.1.25;Initial Catalog=Weilog;User Id=db_weilog_user;Password=weilog123;"
providerName="System.Data.SqlClient"/>
</connectionStrings>
</configSections>
5、使用 Enterprise Library
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
//引用微軟企業類庫數據訪問應用程序塊。
using Microsoft.Practices.EnterpriseLibrary.Data;
//引用公共程序塊數據庫提供程序類庫。
using Weilog.DbProvider;
//引用業務實體類庫。
using Weilog.Web.Domain.Entity.CityArea;
namespace Weilog.Web.Repository.CityArea
{
/// <summary>
/// 區域信息 SQLServer 數據訪問類。無法繼承此類。
/// </summary>
public sealed class AreaRepository : DbProviderBase
{
#region Fields...
#endregion
#region Constructors...
/// <summary>
/// 初始化 <see cref="AreaRepository"/> 類的新實例。
/// </summary>
public AreaRepository()
: base()
{
}
#endregion
#region Methods...
#region 獲取指定編碼集合的區域信息...
/// <summary>
/// 獲取指定編碼集合的區域信息。
/// </summary>
/// <param name="cityId">指定城市唯一編號。</param>
/// <param name="areaCodes">指定區域編號集合。</param>
/// <returns>區域信息實體對象。</returns>
public List<AreaInfo> GetAreaByCodes(int cityId, string areaCodes)
{
// 初始化 Database 對象。
Database db = DatabaseFactory.CreateDatabase(base.DbServiceName);
// 初始化 DbCommand 對象。
DbCommand dbcmd = db.GetStoredProcCommand("proc_Web_Area_GetAreaByCodes");
// 初始化輸入參數。
db.AddInParameter(dbcmd, "@CityId", DbType.Int32, cityId);
db.AddInParameter(dbcmd, "@AreaCodes", DbType.String, areaCodes.Trim());
// 聲明 IDataReader 數據讀取器接口引用。
IDataReader idr = null;
// 實例化區域信息實體列表。
List<AreaInfo> infoList = new List<AreaInfo>();
try
{
// 初始化數據讀取器。
idr = db.ExecuteReader(dbcmd);
if (idr != null)
{
// 判斷是否讀取數據。
while (idr.Read())
{
// 實例化區域信息實體對象。
AreaInfo areaInfo = new AreaInfo();
// 填充字段。
areaInfo.Code = idr["Code"] == DBNull.Value ? String.Empty : idr["Code"].ToString().Trim();
string text = idr["Text"] == DBNull.Value ? String.Empty : idr["Text"].ToString().Trim();
if (string.IsNullOrEmpty(text))
{
areaInfo.Name = idr["Name"] == DBNull.Value ? String.Empty : idr["Name"].ToString().Trim();
}
else
{
areaInfo.Name = text;
}
// 添加對象到列表。
infoList.Add(areaInfo);
}
}
}
catch (Exception ee)
{
// 寫入異常日志表。
// 拋出異常。
throw ee;
}
finally
{
if (idr != null)
{
idr.Close();
}
}
return infoList;
}
#endregion
#endregion
}
}
以上就是在 DotNet 項目中集成 Enterprise Library 數據庫訪問模塊的全部內容了。
