接上面2篇,下面是具体实现,先定义一个抽象类DALAbstract
里面的DbQueryable 是自定义的linq查询,,没有实现完全 以后再分享出来
using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Configuration; using System.Transactions; using System.Linq; using System.Linq.Expressions; using FYJ.Data.Linq; using System.Data.Common; using System.Reflection; namespace FYJ.Data.Entity { public abstract class DALAbstract: IDALBasewhere T : new() { private readonly EntityHelpereq; public DALAbstract() { eq = new EntityHelper(this.TableName, this.PrimaryKey, NewID); } #region 属性 public abstract IDbHelper DbInstance { get; } ////// 主键 ///public abstract String PrimaryKey { get; } public abstract string TableName { get; } public virtual object NewID() { PropertyInfo pi = typeof(T).GetProperty(PrimaryKey); if (pi.PropertyType == typeof(string)) { return Guid.NewGuid().ToString("N"); } string result = new Random().Next(10000000, 99999999) + ""; return result; } public DbQueryableQuery { get { return new DbQueryable(this.DbInstance); } } public DbQueryableQueryDataTable { get { QueryProviderDataTable dtProvider = new QueryProviderDataTable(typeof(T), DbInstance); return new DbQueryable(dtProvider); } } #endregion #region 增删改 public virtual int Add(T model) { return eq.Add(model, DbInstance); } public int Delete(params long[] ids) { ListstrList = new List(); foreach (long id in ids) { strList.Add(id + ""); } return Delete(strList.ToArray()); } public virtual int Delete(string[] guids) { return eq.Delete(guids, DbInstance); } public int Delete(string idOrGuids) { ListstrList = new List(); foreach (string s in idOrGuids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { strList.Add(s); } int result = 0; if (strList.Count > 0) { result += Delete(strList.ToArray()); } return result; } public virtual int Update(T model) { PropertyInfo pi = typeof(T).GetProperty(this.PrimaryKey); object primaryValue = pi.GetValue(model, null); if (primaryValue == null || primaryValue.ToString().Equals("0") || primaryValue.ToString().Equals("")) { return eq.Add(model, DbInstance); } else { return eq.Update(model, DbInstance); } } public int Update(string fieldName, string value, params long[] ids) { ListstrList = new List(); foreach (long id in ids) { strList.Add(id + ""); } return Update(strList.ToArray(), fieldName, value); } public virtual int Update(string[] guids, string fieldName, string value) { return eq.Update(guids, DbInstance, fieldName, value); } public int Update(string idOrGuids, string fieldName, string value) { ListstrList = new List(); foreach (string s in idOrGuids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { strList.Add(s); } int result = 0; if (strList.Count > 0) { result += Update(strList.ToArray(), fieldName, value); } return result; } #endregion public virtual T GetModel(long id) { return eq.GetModel(id, this.DbInstance); } public virtual T GetModel(string guid) { return eq.GetModel(guid, this.DbInstance); } public virtual T GetModel(T model) { DataTable dt = this.GetDataTable(model); if (dt != null && dt.Rows.Count > 0) { return DataTableToModel(dt)[0]; } return default(T); } public virtual bool Exists(T model) { return eq.Exists(model, this.DbInstance); } public virtual DataTable GetDataTable(T model, string[] accessProperty = null) { return eq.GetDataTable(model, this.DbInstance, accessProperty); } public virtual DataTable GetDataTable(ref long? count, string order, string[] accessProperty = null, string where = null, int currentPage = 1, int pageSize = 20) { return this.DbInstance.GetDataTable(ref count, this.TableName, order, accessProperty, where, currentPage, pageSize); } public virtual DataTable GetDataTable(string order, string[] accessProperty = null, string where = null, int currentPage = 1, int pageSize = 20) { long? count = null; return this.DbInstance.GetDataTable(ref count, this.TableName, order, accessProperty, where, currentPage, pageSize); } public virtual ListDataTableToModel(DataTable dt) { DataConvert convert = new DataConvert(); return convert.DataTableToModel(dt); } public virtual DataTable IQueryableToDataTable(IQueryable query) { DataConvert convert = new DataConvert(); return convert.IQueryableToDataTable(query); } } }
然后一个具体实现类
public class DComment : DALAbstract, IComment { public override FYJ.Data.IDbHelper DbInstance { get { return FYJ.Data.DbFactory.GetIDbHelper("blog"); } } }
IComment 可以定义一些其它的接口啥的. 然后就可以 DComment dal=new DComment(); 来调用相应的方法了。
我这里还写了一个单例和ioc的实现
using System; using System.Collections.Generic; using System.Configuration; using System.Reflection; using System.Text; namespace FYJ.Data.Entity { public class DALContainerContextwhere T:class { private static Dictionary后续我会把所有源码给出下载.list = new Dictionary (); ////// 静态方法 获取或创建一个实例 /////////配置///public static T Load(string dalappSettingKey = null) { if (list.ContainsKey(typeof(T).FullName)) { return (T)list[typeof(T).FullName]; } return Create(dalappSettingKey); } ////// 动态方法 获取或创建一个实例 /////////配置///public T Load2() { if (list.ContainsKey(typeof(T).FullName)) { return (T)list[typeof(T).FullName]; } return Create(null); } ////// 创建一个新实例 ////////////public static T Create(string dalappSettingKey = null) { T instance = default(T); string settingKey = typeof(T).FullName; settingKey = settingKey.Substring(0,settingKey.LastIndexOf(".")); foreach (Type t in System.Reflection.Assembly.Load(ConfigurationManager.AppSettings[dalappSettingKey ?? settingKey]).GetTypes()) { foreach (Type tt in t.GetInterfaces()) { if (tt == typeof(T)) { object obj = Activator.CreateInstance(t);//CreateInstance(_fullClassName, false, BindingFlags.Default, null, objs, null, null); string fullName = typeof(T).FullName; if (!list.ContainsKey(fullName)) { list.Add(fullName, obj); } instance = (T)obj; } } } return instance; } } }