一、架构图
二、解决方案中的项目设计
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>