close
文章出處
{
int Add(Model.Example model);
{
public ExampleService()
{
this.exampleDAL = AbstractFactory.DALFactory.GetExampleDAL();
}
/// <summary>
/// 數據表數據訪問層接口
/// </summary>
private IDAL.IExample exampleDAL;
public IDAL.IExample ExampleDAL
{
set { exampleDAL = value; }
get { return exampleDAL; }
}
public int Add(Model.Example model)
{
return exampleDAL.Add(model);
}
/// 接口層 IExample
/// </summary>
public interface IExample
{
#region 成員方法
/// <summary>
/// 增加一條記錄
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
int Add(Model.Example model);
#endregion
/// Example:實體類
/// </summary>
[Serializable]
public class Example
{
private string _id = "";
private string _name = "";
/// <summary>
/// ID
/// </summary>
public string ID
{
set { _id = value; }
get { return _id; }
}
/// <summary>
/// Name
/// </summary>
public string Name
{
set { _name = value; }
get { return _name; }
}
一、架構圖
二、解決方案中的項目設計
AbstractFactory(抽象工廠)
IBLL(業務邏輯層)
BLL
IDAL(數據訪問層)
SqlServerDAL(SqlServer數據訪問)
````
Model(數據模型)
ToolsLibrary(工具類)
WebMVCApplication(Web MVC應用程序)
三、具體的實現
1、AbstractFactory
(1)Cache.cs 反射出實例很耗費性能,這里使用緩存來減輕負擔
/// <summary>
/// 抽象工廠的實現,因為反射出實例很耗費性能,所以運用了緩存來減輕負擔
/// </summary>
public class Cache
{
public Cache()
{
}
/// <summary>
/// 獲取緩存數據
/// </summary>
/// <param name="key">key</param>
/// <returns></returns>
public static object Get(string key)
{
System.Web.Caching.Cache cache = HttpRuntime.Cache;
return cache.Get(key);
}
/// <summary>
/// 添加緩存數據
/// </summary>
/// <param name="key">key</param>
/// <param name="value">value</param>
public static void InsertCache(string key, object value)
{
if (Get(key) == null)
{
System.Web.Caching.Cache cache = HttpRuntime.Cache;
cache.Insert(key, value);
}
}
}
/// 抽象工廠的實現,因為反射出實例很耗費性能,所以運用了緩存來減輕負擔
/// </summary>
public class Cache
{
public Cache()
{
}
/// <summary>
/// 獲取緩存數據
/// </summary>
/// <param name="key">key</param>
/// <returns></returns>
public static object Get(string key)
{
System.Web.Caching.Cache cache = HttpRuntime.Cache;
return cache.Get(key);
}
/// <summary>
/// 添加緩存數據
/// </summary>
/// <param name="key">key</param>
/// <param name="value">value</param>
public static void InsertCache(string key, object value)
{
if (Get(key) == null)
{
System.Web.Caching.Cache cache = HttpRuntime.Cache;
cache.Insert(key, value);
}
}
}
(2)DALFactory.cs 反射DAL,使用IOC依賴注入
public class DALFactory
{
/// <summary>
/// DLL全路徑
/// </summary>
public static readonly string DALPath = System.Configuration.ConfigurationManager.AppSettings["DALPath"];
public static object CreateDAL(string assemblyPath, string objType)
{
var cacheDAL = Cache.Get(objType);
if (cacheDAL == null)
{
cacheDAL = Assembly.Load(assemblyPath).CreateInstance(objType);
Cache.InsertCache(objType, cacheDAL);
}
return cacheDAL;
}
public static IDAL.IExample GetExampleDAL()
{
return CreateDAL(DALPath, string.Format("{0}.Example", DALPath)) as IDAL.IExample;
}
}
{
/// <summary>
/// DLL全路徑
/// </summary>
public static readonly string DALPath = System.Configuration.ConfigurationManager.AppSettings["DALPath"];
public static object CreateDAL(string assemblyPath, string objType)
{
var cacheDAL = Cache.Get(objType);
if (cacheDAL == null)
{
cacheDAL = Assembly.Load(assemblyPath).CreateInstance(objType);
Cache.InsertCache(objType, cacheDAL);
}
return cacheDAL;
}
public static IDAL.IExample GetExampleDAL()
{
return CreateDAL(DALPath, string.Format("{0}.Example", DALPath)) as IDAL.IExample;
}
}
2、IBLL
public interface IExampleService{
int Add(Model.Example model);
}
3、BLL
public class ExampleService : IBLL.IExampleService{
public ExampleService()
{
this.exampleDAL = AbstractFactory.DALFactory.GetExampleDAL();
}
/// <summary>
/// 數據表數據訪問層接口
/// </summary>
private IDAL.IExample exampleDAL;
public IDAL.IExample ExampleDAL
{
set { exampleDAL = value; }
get { return exampleDAL; }
}
public int Add(Model.Example model)
{
return exampleDAL.Add(model);
}
}
4、IDAL
/// <summary>/// 接口層 IExample
/// </summary>
public interface IExample
{
#region 成員方法
/// <summary>
/// 增加一條記錄
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
int Add(Model.Example model);
#endregion
}
5、SqlServerDAL
public class Example:IDAL.IExample
{
public int Add(Model.Example model)
{
return 0;
}
}
{
public int Add(Model.Example model)
{
return 0;
}
}
6、Model
/// <summary>/// Example:實體類
/// </summary>
[Serializable]
public class Example
{
private string _id = "";
private string _name = "";
/// <summary>
/// ID
/// </summary>
public string ID
{
set { _id = value; }
get { return _id; }
}
/// <summary>
/// Name
/// </summary>
public string Name
{
set { _name = value; }
get { return _name; }
}
}
7、Web.config
<appSettings>
<!--DAL反射配置-->
<add key="DALPath" value="SqlServerDAL" />
</appSettings>
<!--DAL反射配置-->
<add key="DALPath" value="SqlServerDAL" />
</appSettings>
![]() |
不含病毒。www.avast.com |
全站熱搜