网站整体架构建议采用工厂模式
分别包括:数据访问层DAL,数据访问接口层IDAL,工厂层DALFactory,业务逻辑层,显示层这样的架构方式
在WebConfig配置采用何种数据库的数据访问层
<appSettings>
<add key="DAL" value="SQLServerDAL"/><!--数据访问层-->
</appSettings>
定义接口层
public interface ILocal_TopicDAL
{
/// <summary>
/// 根据问题ID获取问题详细
/// </summary>
/// <param name="LT_ID">问题ID</param>
/// <returns></returns>
Local_TopicModel getTopicById(int LT_ID, string sCityCode, string sLanguage);
}
数据访问层继承接口层
public class Local_TopicDAL : ILocal_TopicDAL
{
#region 根据问题ID获取问题详细
/// <summary>
/// 根据问题ID获取问题详细
/// </summary>
/// <param name="LT_ID">问题ID</param>
/// <param name="sCityCode">城市代码</param>
/// <param name="sLanguage">语言</param>
/// <returns></returns>
public Local_TopicModel getTopicById(int LT_ID, string sCityCode, string sLanguage)
{
string sql = "select LT_ID,LT_Title,LT_ThanksInfo,LT_Content,LT_ViewCount,LT_CommentCount,LTC_ID,LT_UpdateDate,LT_LastReplyDate,"+
"LT_Checked,isnull(LT_Status,0) as LT_Status,isnull(LM_ID,0) as LM_ID,LM_NickName,LT_IsEffectiveDate,LT_EffectiveDate,LT_CreateDate," +
"LTT_ID"+
",LTK_IDs,LT_AboutIds"+
" from Local_Topic"+
" where LT_Checked=1 and LT_ID=@LT_ID";
string ConnectionString = ConnectionManage.getConnectionString(sCityCode, sLanguage);//EdushiDataCenter.Current.CityConfigProvider.GetCitySetting(sCityCode, sLanguage).ECS_DBConnectString;
SqlParameter[] parameters = { new SqlParameter("@LT_ID", SqlDbType.Int, ) };
parameters[].Value = LT_ID;
DataTable dt = DbHelperSQL.Query(sql, ConnectionString, parameters).Tables[];
if (dt.Rows.Count > )
{
return DatableToList.CreateItem<Local_TopicModel>(dt.Rows[]);
}
else
{
return new Local_TopicModel();
}
}
#endregion
工厂层根据DAL路径通过反射创建数据访问实例
private static readonly string AssemblyPath = ConfigurationManager.AppSettings["DAL"];//数据访问层的程序集 #region 创建对象(不使用缓存)
/// <summary>
/// 创建对象(不使用缓存)
/// </summary>
/// <param name="AssemblyPath"></param>
/// <param name="ClassNamespace"></param>
/// <returns></returns>
private object CreateObjectNoCache(string AssemblyPath, string ClassNamespace)
{
try
{
object objType = Assembly.Load(AssemblyPath).CreateInstance(ClassNamespace);
return objType;
}
catch(Exception ex)
{
LogOperate.WriteErrorLog("创建反射异常(不使用缓存)", ex);
return null;
}
}
业务逻辑层通过调用反射的数据访问实例来执行具体方法
#region 根据问题ID获取问题详细
/// <summary>
/// 根据问题ID获取问题详细
/// </summary>
/// <param name="LT_ID">问题ID</param>
/// <returns></returns>
public Local_TopicModel getTopicById(int LT_ID)
{
CacheConfigModel ccm = new CacheConfigHelper().getCacheConfigModelByCacheId(, ConfigurationManager.AppSettings["sectionName"].ToString());
string key = string.Format(ccm.CacheKey, _cityCode, LT_ID);
object obj = cache.Get(key);
if (obj == null)
{
object objType = new DataAccess().CreateObject("Local_TopicDAL");//创建Local_Topic的实例
obj = objType == null ? new Local_TopicModel() : ((ILocal_TopicDAL)objType).getTopicById(LT_ID, _cityCode, _language);
cache.Add(key, obj, DateTimeOffset.Now.AddSeconds(ccm.CacheTime));
}
return obj as Local_TopicModel;
}
#endregion