范型类型工厂模式
工厂模式是我们常用的设计模式,工厂模式的好处我们都很清楚,以前在 dotnet1.1的时候我们应用工厂模式的时候同样也要写不少的代码每增加一个抽象基类都要去维护代码代码如下:
namespace Sms.DataProvider.ProviderBase
{
/// <summary>
/// DataRepository 的摘要说明。
/// </summary>
public sealed class DataRepository
{
private static readonly string path = ConfigurationManager.AppSettings["SmsShtxDal"];
public DataRepository()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
private static object CreateObject(string path,string CacheKey)
{
object objType = DataCache.GetCache(CacheKey);
if (objType == null)
{
try
{
objType = Assembly.Load(path).CreateInstance(CacheKey);
DataCache.SetCache(CacheKey, objType);
}
catch(Exception e)
{
string w = e.ToString();
}
}
return objType;
}
/// <summary>
/// 得到AccountReportProvider对象
/// </summary>
/// <returns></returns>
public static AccountReportProviderBase CreateAccountReportProvider()
{
string CacheKey = path+".ProviderBase.AccountReportProvider";
object objType=CreateObject(path,CacheKey);
return (AccountReportProviderBase)objType;
}
}
}
{
/// <summary>
/// DataRepository 的摘要说明。
/// </summary>
public sealed class DataRepository
{
private static readonly string path = ConfigurationManager.AppSettings["SmsShtxDal"];
public DataRepository()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
private static object CreateObject(string path,string CacheKey)
{
object objType = DataCache.GetCache(CacheKey);
if (objType == null)
{
try
{
objType = Assembly.Load(path).CreateInstance(CacheKey);
DataCache.SetCache(CacheKey, objType);
}
catch(Exception e)
{
string w = e.ToString();
}
}
return objType;
}
/// <summary>
/// 得到AccountReportProvider对象
/// </summary>
/// <returns></returns>
public static AccountReportProviderBase CreateAccountReportProvider()
{
string CacheKey = path+".ProviderBase.AccountReportProvider";
object objType=CreateObject(path,CacheKey);
return (AccountReportProviderBase)objType;
}
}
}
这种方式只是我个人的应用,可能还有别的朋友把所有的抽象类写成一个文件这样就不需要更新上面的代码。
现在用上了2.0有了范型我应用范型类型改写了1.1版本的工厂代码,如下:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Reflection;
using System.Text;
using Shoucao.Provider.Data;
namespace Shoucao.Provider.Factory
{
public sealed class DataFactory<T>
{
private static readonly string path = ConfigurationManager.AppSettings["ShoucaoProviderDal"];
private static object _sysLock = new object();
private static T CreateObject(string path, string CacheKey)
{
T objType = DataCache<T>.GetCache(CacheKey);
if (objType == null)
{
lock (_sysLock)
{
try
{
objType = (T) Assembly.Load(path).CreateInstance(CacheKey);
DataCache<T>.SetCache(CacheKey, objType);
}
catch (Exception e)
{
string w = e.ToString();
}
}
}
return objType;
}
public static T CreateProvider
{
get
{
string CacheKey = typeof (T).FullName;
return CreateObject(path, CacheKey);
}
}
}
}
using System.Collections.Generic;
using System.Configuration;
using System.Reflection;
using System.Text;
using Shoucao.Provider.Data;
namespace Shoucao.Provider.Factory
{
public sealed class DataFactory<T>
{
private static readonly string path = ConfigurationManager.AppSettings["ShoucaoProviderDal"];
private static object _sysLock = new object();
private static T CreateObject(string path, string CacheKey)
{
T objType = DataCache<T>.GetCache(CacheKey);
if (objType == null)
{
lock (_sysLock)
{
try
{
objType = (T) Assembly.Load(path).CreateInstance(CacheKey);
DataCache<T>.SetCache(CacheKey, objType);
}
catch (Exception e)
{
string w = e.ToString();
}
}
}
return objType;
}
public static T CreateProvider
{
get
{
string CacheKey = typeof (T).FullName;
return CreateObject(path, CacheKey);
}
}
}
}
抽象类和实现类如下:
using System;
using System.Collections.Generic;
using System.Text;
using Shoucao.Entity;
namespace Shoucao.Provider.Data
{
public abstract class Shoucao_UsersProvideBase : EntityProviderBase<Shoucao_UsersEntity,Shoucao_UsersEntityKey>
{
public Shoucao_UsersProvideBase()
{
}
}
}
using System.Collections.Generic;
using System.Text;
using Shoucao.Entity;
namespace Shoucao.Provider.Data
{
public abstract class Shoucao_UsersProvideBase : EntityProviderBase<Shoucao_UsersEntity,Shoucao_UsersEntityKey>
{
public Shoucao_UsersProvideBase()
{
}
}
}
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Shoucao.Provider.Data;
using Shoucao.Entity;
namespace Shoucao.Provider.SqlClient
{
public class Shoucao_UsersSqlProvider : Shoucao_UsersProvideBase
{
public Shoucao_UsersSqlProvider()
{
}
public override Shoucao_UsersEntity GetEntity(Shoucao_UsersEntityKey key)
{
SqlDatabase db = new SqlDatabase(SqlConn);
string sqlStr = "select * from Shoucao_Users where UserID=@UserID";
DbCommand cmd = db.GetSqlStringCommand(sqlStr);
db.AddInParameter(cmd, "@UserID", SqlDbType.Int, key.UserID);
Shoucao_UsersEntity _shoucao_Users = null;
using (SqlDataReader dr = db.ExecuteReader(cmd) as SqlDataReader)
{
EntityBuilder<Shoucao_UsersEntity> _eb = EntityBuilder<Shoucao_UsersEntity>.CreateBuilder(dr);
if (dr != null)
{
while (dr.Read())
{
_shoucao_Users = _eb.Build(dr);
}
}
}
return _shoucao_Users;
}
}
}
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Shoucao.Provider.Data;
using Shoucao.Entity;
namespace Shoucao.Provider.SqlClient
{
public class Shoucao_UsersSqlProvider : Shoucao_UsersProvideBase
{
public Shoucao_UsersSqlProvider()
{
}
public override Shoucao_UsersEntity GetEntity(Shoucao_UsersEntityKey key)
{
SqlDatabase db = new SqlDatabase(SqlConn);
string sqlStr = "select * from Shoucao_Users where UserID=@UserID";
DbCommand cmd = db.GetSqlStringCommand(sqlStr);
db.AddInParameter(cmd, "@UserID", SqlDbType.Int, key.UserID);
Shoucao_UsersEntity _shoucao_Users = null;
using (SqlDataReader dr = db.ExecuteReader(cmd) as SqlDataReader)
{
EntityBuilder<Shoucao_UsersEntity> _eb = EntityBuilder<Shoucao_UsersEntity>.CreateBuilder(dr);
if (dr != null)
{
while (dr.Read())
{
_shoucao_Users = _eb.Build(dr);
}
}
}
return _shoucao_Users;
}
}
}
配置文件节如下
<appSettings>
<add key="ShoucaoProviderDal" value="Shoucao.Provider.SqlClient" />
</appSettings>
调用方式如下:
Shoucao_UsersEntityKey key=new Shoucao_UsersEntityKey();
DataFactory<Shoucao_UsersSqlProvider>.CreateProvider.GetEntity( key)
以上方式减少了代码量,应用更加方便,不过只是个人观点,还请高手指点。