如左图所示,先建好文件夹,然后再来填充内容。
首先,从最底层写起:
第一步,写一个操作数据库的类
我这边采用的是底层使用ADO.NET,通过泛型约束和反射来实现一个简单的ORM
namespace Zero.Infrastructure.IBase { public abstract class IDataBase { public virtual string TableName { get; set; } public virtual int ID { get; set; } } } abstract class IDataBase
namespace Zero.Infrastructure.Attributes { public class DataFieldAttribute : Attribute { private string _FieldName; public DataFieldAttribute(string fieldname) { this._FieldName = fieldname; } public string FieldName { get { return this._FieldName; } set { this._FieldName = value; } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Zero.Infrastructure.Attributes; using Zero.Infrastructure.IBase; namespace Zero.Domain.Entities { public class User: IDataBase { public User() { TableName = "User"; } private string _TableName; public override string TableName { get { if (_TableName == null) { return "User"; } else { return _TableName; } } set { _TableName = value; } } public override int ID { get; set; } [DataFieldAttribute("UserName")] public string UserName { get; set; } } } View Code
public class ZeroORMwhere<T> where T : IBase.IDataBase { public string SqlConnctionString { get; set; } public SqlConnection conn { get; set; } public SqlTransaction tran { get; set; } }
/// <summary> /// 获得实体T所有数据 /// </summary> /// <returns></returns> public List<T> Select(T t) { List<T> list = new List<T>(); string sql = "select * from " + t.TableName + " WITH (NOLOCK) order by id desc"; DataSet ds = SqlHelper.ExecuteDataset(SqlConnctionString, CommandType.Text, sql); for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { list.Add(DataSetToEntity.DsToEntity<T>(ds, i)); } return list; }
/// <summary> /// 插入新数据 /// </summary> /// <param name="t">实体类</param> /// <returns></returns> public int Insert(T t) { try { Type mytype = t.GetType(); // 获取类的所有公共属性 System.Reflection.PropertyInfo[] pInfo = mytype.GetProperties(); string FieldName = "";//字段名称 string Values = "";//值 StringBuilder sql = new StringBuilder(); List<SqlParameter> paras = new List<SqlParameter>();//不定参集合,防注入 sql.Append("Insert into "); sql.Append(mytype.Name);//数据库表名,可以放t.TableName sql.Append("("); object[] objDataFieldAttribute = null; foreach (System.Reflection.PropertyInfo pio in pInfo) { objDataFieldAttribute = pio.GetCustomAttributes(typeof(DataFieldAttribute), false); if (objDataFieldAttribute != null && objDataFieldAttribute.Length > 0) { FieldName += ((DataFieldAttribute)objDataFieldAttribute[0]).FieldName + ",";//给字段赋值 Values += "@" + ((DataFieldAttribute)objDataFieldAttribute[0]).FieldName + ",";//给对应字段的值赋值 paras.Add(new SqlParameter("@" + ((DataFieldAttribute)objDataFieldAttribute[0]).FieldName, pio.GetValue(t, null)));//添加不定参 } } FieldName = FieldName.TrimEnd(','); Values = Values.TrimEnd(','); sql.Append(FieldName); sql.Append(") VAlUES ("); sql.Append(Values); sql.Append(")"); int i = SqlHelper.ExecuteNonQuery(SqlConnctionString, CommandType.Text, sql.ToString(), paras.ToArray()); return i; } catch (Exception) { return -1; throw; } } View Code
using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Reflection; using System.Text; using Zero.Infrastructure.Attributes; using Zero.Infrastructure.Utilities; namespace Zero.Infrastructure.Conntion { public class ZeroORM<T> where T : IBase.IDataBase { public string SqlConnctionString { get; set; } public SqlConnection conn { get; set; } public SqlTransaction tran { get; set; } /// <summary> /// 获得实体T所有数据 /// </summary> /// <returns></returns> public List<T> Select(T t) { List<T> list = new List<T>(); string sql = "select * from [" + t.TableName + "] WITH (NOLOCK) order by id desc"; //出于性能考虑,不用反射来获取表名 DataSet ds = SqlHelper.ExecuteDataset(SqlConnctionString, CommandType.Text, sql); for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { list.Add(DataSetToEntity.DsToEntity<T>(ds, i)); } return list; } /// <summary> /// 根据主键ID获取数据(一条) /// </summary> /// <param name="id"></param> /// <param name="TableName"></param> /// <returns></returns> public T SelectByID(int id, T t) { string sql = "select * from [" + t.TableName + "] WITH (NOLOCK) where ID=@ID"; //出于性能考虑,不用反射来获取表名 DataSet ds = SqlHelper.ExecuteDataset(SqlConnctionString, CommandType.Text, sql, new SqlParameter("@ID", id)); t = DataSetToEntity.DsToEntity<T>(ds, 0); PropertyInfo[] prop = t.GetType().GetProperties(); return t; } /// <summary> /// 插入新数据 /// </summary> /// <param name="t">实体类</param> /// <returns></returns> public int Insert(T t) { try { Type mytype = t.GetType(); // 获取类的所有公共属性 System.Reflection.PropertyInfo[] pInfo = mytype.GetProperties(); string FieldName = "";//字段名称 string Values = "";//值 StringBuilder sql = new StringBuilder(); List<SqlParameter> paras = new List<SqlParameter>(); sql.Append("Insert into ["); sql.Append(mytype.Name);//数据库表名 sql.Append("]("); object[] objDataFieldAttribute = null; foreach (System.Reflection.PropertyInfo pio in pInfo) { objDataFieldAttribute = pio.GetCustomAttributes(typeof(DataFieldAttribute), false); if (objDataFieldAttribute != null && objDataFieldAttribute.Length > 0) { FieldName += ((DataFieldAttribute)objDataFieldAttribute[0]).FieldName + ","; Values += "@" + ((DataFieldAttribute)objDataFieldAttribute[0]).FieldName + ","; paras.Add(new SqlParameter("@" + ((DataFieldAttribute)objDataFieldAttribute[0]).FieldName, pio.GetValue(t, null))); } } FieldName = FieldName.TrimEnd(','); Values = Values.TrimEnd(','); sql.Append(FieldName); sql.Append(") VAlUES ("); sql.Append(Values); sql.Append(")"); int i = SqlHelper.ExecuteNonQuery(SqlConnctionString, CommandType.Text, sql.ToString(), paras.ToArray()); return i; } catch (Exception) { return -1; throw; } } /// <summary> /// 更新数据 /// </summary> /// <param name="t">需更新的实体类</param> /// <returns></returns> public int Update(T t) { try { int i = 0; int primarykey = t.ID; T oldT = t; Type mytype = t.GetType(); System.Reflection.PropertyInfo[] pInfo = mytype.GetProperties(); oldT = SelectByID(primarykey, oldT);//获得原始值,为日志做准备 if (t != oldT) { string SetValue = "";//字段名称 string Where = " where ID=@ID";//值 StringBuilder sql = new StringBuilder(); List<SqlParameter> paras = new List<SqlParameter>(); sql.Append("Update ["); sql.Append(mytype.Name); sql.Append("] Set "); object[] objDataFieldAttribute = null; foreach (System.Reflection.PropertyInfo pio in pInfo) { objDataFieldAttribute = pio.GetCustomAttributes(typeof(DataFieldAttribute), false); if (objDataFieldAttribute != null && objDataFieldAttribute.Length > 0 && pio.GetValue(t, null).ToString() != pio.GetValue(oldT, null).ToString()) { SetValue += ((DataFieldAttribute)objDataFieldAttribute[0]).FieldName + "=" + "@" + ((DataFieldAttribute)objDataFieldAttribute[0]).FieldName + ","; paras.Add(new SqlParameter("@" + ((DataFieldAttribute)objDataFieldAttribute[0]).FieldName, pio.GetValue(t, null))); } } SetValue = SetValue.TrimEnd(','); sql.Append(SetValue); sql.Append(Where); paras.Add(new SqlParameter("@ID", primarykey)); i = SqlHelper.ExecuteNonQuery(SqlConnctionString, CommandType.Text, sql.ToString(), paras.ToArray()); return i; } else { return -2; } } catch (Exception) { return -1; throw; } } /// <summary> /// 删除数据 /// </summary> /// <param name="t"></param> /// <returns></returns> public int Delete(T t) { int i = 0; int primarykey = t.ID; Type mytype = t.GetType(); string TableName = mytype.Name; string Where = " where ID =@ID"; string sql = "DELETE FROM " + TableName + Where; try { i = SqlHelper.ExecuteNonQuery(SqlConnctionString, CommandType.Text, sql, new SqlParameter("@ID", primarykey)); return i; } catch (Exception) { return -1; throw; } } /// <summary> /// 开始事务 /// </summary> /// <returns></returns> public void BeginTran() { try { conn = new SqlConnection(SqlConnctionString); conn.Open(); tran = conn.BeginTransaction(); } catch (Exception) { tran.Rollback(); conn.Close(); throw; } } /// <summary> /// 带事务的插入方法 /// </summary> /// <param name="t"></param> /// <returns></returns> public int InsertByTran(T t) { try { Type mytype = t.GetType(); // 获取类的所有公共属性 System.Reflection.PropertyInfo[] pInfo = mytype.GetProperties(); string FieldName = "";//字段名称 string Values = "";//值 StringBuilder sql = new StringBuilder(); List<SqlParameter> paras = new List<SqlParameter>(); sql.Append("Insert into ["); sql.Append(mytype.Name);//数据库表名 sql.Append("]("); object[] objDataFieldAttribute = null; foreach (System.Reflection.PropertyInfo pio in pInfo) { objDataFieldAttribute = pio.GetCustomAttributes(typeof(DataFieldAttribute), false); if (objDataFieldAttribute != null && objDataFieldAttribute.Length > 0) { FieldName += ((DataFieldAttribute)objDataFieldAttribute[0]).FieldName + ","; Values += "@" + ((DataFieldAttribute)objDataFieldAttribute[0]).FieldName + ","; paras.Add(new SqlParameter("@" + ((DataFieldAttribute)objDataFieldAttribute[0]).FieldName, pio.GetValue(t, null))); } } FieldName = FieldName.TrimEnd(','); Values = Values.TrimEnd(','); sql.Append(FieldName); sql.Append(") VAlUES ("); sql.Append(Values); sql.Append(")"); int i = SqlHelper.ExecuteNonQuery(tran, CommandType.Text, sql.ToString(), paras.ToArray()); return i; } catch (Exception) { tran.Rollback(); return -1; throw; } } /// <summary> /// 带事务的更新方法 /// </summary> /// <param name="t"></param> /// <returns></returns> public int UpdateByTran(T t) { try { int i = 0; int primarykey = t.ID; T oldT = t; Type mytype = t.GetType(); System.Reflection.PropertyInfo[] pInfo = mytype.GetProperties(); oldT = SelectByID(primarykey, oldT);//获得原始值,为日志做准备 if (t != oldT) { string SetValue = "";//字段名称 string Where = " where ID=@ID";//值 StringBuilder sql = new StringBuilder(); List<SqlParameter> paras = new List<SqlParameter>(); sql.Append("Update ["); sql.Append(mytype.Name); sql.Append("] Set "); object[] objDataFieldAttribute = null; foreach (System.Reflection.PropertyInfo pio in pInfo) { objDataFieldAttribute = pio.GetCustomAttributes(typeof(DataFieldAttribute), false); if (objDataFieldAttribute != null && objDataFieldAttribute.Length > 0 && pio.GetValue(t, null).ToString() != pio.GetValue(oldT, null).ToString()) { SetValue += ((DataFieldAttribute)objDataFieldAttribute[0]).FieldName + "=" + "@" + ((DataFieldAttribute)objDataFieldAttribute[0]).FieldName + ","; paras.Add(new SqlParameter("@" + ((DataFieldAttribute)objDataFieldAttribute[0]).FieldName, pio.GetValue(t, null))); } } SetValue = SetValue.TrimEnd(','); sql.Append(SetValue); sql.Append(Where); paras.Add(new SqlParameter("@ID", primarykey)); i = SqlHelper.ExecuteNonQuery(tran, CommandType.Text, sql.ToString(), paras.ToArray()); return i; } else { tran.Rollback(); return -2; } } catch (Exception) { tran.Rollback(); return -1; throw; } } /// <summary> /// 带事务的删除方法 /// </summary> /// <param name="t"></param> /// <returns></returns> public int DeleteByTran(T t) { int i = 0; int primarykey = t.ID; Type mytype = t.GetType(); string TableName = mytype.Name; string Where = "] where ID =@ID"; string sql = "DELETE FROM [" + TableName + Where; try { i = SqlHelper.ExecuteNonQuery(tran, CommandType.Text, sql, new SqlParameter("@ID", primarykey)); return i; } catch (Exception) { tran.Rollback(); return -1; throw; } } /// <summary> /// 带事务和条件的删除方法 /// </summary> /// <param name="t"></param> /// <returns></returns> public int DeleteByTran(T t, string where) { if (where.IndexOf("1=1") > 0) { return -1; } int i = 0; string Where = "] where " + where; string sql = "DELETE FROM [" + t.TableName + Where; try { i = SqlHelper.ExecuteNonQuery(tran, CommandType.Text, sql, null); return i; } catch (Exception) { tran.Rollback(); return -1; throw; } } /// <summary> /// 提交事务 /// </summary> /// <returns></returns> public void CommitTran() { try { tran.Commit(); conn.Close(); } catch (Exception) { tran.Rollback(); conn.Close(); throw; } finally { tran.Dispose(); conn.Dispose(); } } /// <summary> /// 回滚事务 /// </summary> public void RollBackTran() { try { tran.Rollback(); } catch (Exception) { throw; } } } } View Code
三:Domain层
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Zero.Domain.Abstract; using Zero.Infrastructure.IBase; namespace Zero.Domain.Concrete { public class ConcreteBase<T> where T : IDataBase { public Zero.Infrastructure.Conntion.ZeroORM<T> DbHelper; public ConcreteBase() { DbHelper = new Infrastructure.Conntion.ZeroORM<T>(); } public string SqlConnctionString { get { return DbHelper.SqlConnctionString; } set { DbHelper.SqlConnctionString = value; } } public List<T> GetAllList(T t) { List<T> ubiList = new List<T>(); ubiList = DbHelper.Select(t); return ubiList; } public bool Insert(T t) { int i = 0; i = DbHelper.Insert(t); return i > 0; } public bool Update(T t) { int i = 0; i = DbHelper.Update(t); return i > 0; } public bool Delete(T t) { int i = 0; i = DbHelper.Delete(t); return i > 0; } /// <summary> /// 带事务的插入方法 /// </summary> /// <param name="t"></param> /// <returns></returns> public bool InsertByTran(T t) { if (DbHelper.tran == null) { DbHelper.BeginTran(); } int i = DbHelper.InsertByTran(t); return i > 0; } /// <summary> /// 带事务的更新方法 /// </summary> /// <param name="t"></param> /// <returns></returns> public bool UpdateByTran(T t) { if (DbHelper.tran == null) { DbHelper.BeginTran(); } int i = DbHelper.UpdateByTran(t); return i > 0; } /// <summary> /// 带事务的删除方法 /// </summary> /// <param name="t"></param> /// <returns></returns> public bool DeleteByTran(T t) { if (DbHelper.tran == null) { DbHelper.BeginTran(); } int i = DbHelper.DeleteByTran(t); return i > 0; } /// <summary> /// 带事务和条件的删除方法 /// </summary> /// <param name="t"></param> /// <returns></returns> public bool DeleteByTran(T t, string where) { if (DbHelper.tran == null) { DbHelper.BeginTran(); } int i = DbHelper.DeleteByTran(t, where); return i > 0; } /// <summary> /// /// </summary> /// <param name="t"></param> /// <returns></returns> public void RollBackTran() { DbHelper.RollBackTran(); } /// <summary> /// /// </summary> /// <param name="t"></param> /// <returns></returns> public void BeginTran() { DbHelper.BeginTran(); } /// <summary> /// 提交事务 提交成功返回"";失败返回错误信息 /// </summary> /// <returns>提交成功返回"";失败返回错误信息</returns> public string CommitTran() { if (DbHelper.tran != null) { try { DbHelper.CommitTran(); return ""; } catch (Exception e) { return e.ToString(); throw; } } else { return "不存在可提交的事务"; } } /// <summary> /// 根据主键ID查询结果,返回T /// </summary> /// <param name="id"></param> /// <returns></returns> public T SelectByID(string id, T t) { if (id != "") { return DbHelper.SelectByID(int.Parse(id), t); } else { return null; } } } } View Code
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Zero.Domain.Entities; namespace Zero.Domain.Abstract { public interface IUserRepository { User GetEntity(); IQueryable<User> Users { get; } } }
using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Text; using Zero.Domain.Abstract; using Zero.Domain.Entities; namespace Zero.Domain.Concrete { public class UserRepository : ConcreteBase<User>, IUserRepository { public User GetEntity() { return new User(); } public IQueryable<User> BaseTitleTypes { get { return GetAllList(GetEntity()).AsQueryable(); } } public UserRepository() : base() { SqlConnctionString = ConfigurationManager.ConnectionStrings["ZeroTest"].ConnectionString;//获取连接字符串 } } }
链接字符串的位置写的有点蠢,不过先这样做吧,一切已快速为目的
// // GET: /Index/ public ActionResult Index() { UserRepository us = new UserRepository(); bool b =us.Insert(new Domain.Entities.User { UserName = "Ambre" }); if (b) { var ListEntity = from o in us.Users select o; return Json(ListEntity, JsonRequestBehavior.AllowGet); } else { return Content(b.ToString()); } }