C# 輕量級ORM 編寫思惟

目標:sql

下降研發人員門檻,提升效率,去除重複引用DLL的工做,基礎配置由抽象工廠處理。數據庫

基礎擴展框架

 

/// <summary>
/// DataTable 轉換爲List 集合
/// </summary>
/// <typeparam name="TResult">類型</typeparam>
/// <param name="dt">DataTable</param>
/// <returns></returns>
public static List<TResult> ToList<TResult>(this DataTable dt) where TResult : class,new()

 

/// <summary>
/// 轉換爲一個DataTable
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="value"></param>
/// <returns></returns>
public static DataTable ToDataTable<TResult>(this IEnumerable<TResult> value) where TResult : class

  

 

業務邏輯接口this

//name:wujc 2013-06-20
namespace Yc.BLL
{
public class DataDomain : IDataService
{
private readonly IDataService dal = DataAccess.CreateService();
public DataDomain()
{ }
#region 成員方法
/// <summary>
/// 獲取錶行數
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="strWhere"></param>
/// <returns></returns>
public int GetRowsCount<T>(string strWhere) where T : BaseModel, new() { return dal.GetRowsCount<T>(strWhere); }

/// <summary>
/// 獲取錶行數
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="sql"></param>
/// <returns></returns>
public int GetRowsCount(string sql) { return dal.GetRowsCount(sql); }

/// <summary>
/// 是否存在該記錄
/// </summary>
public bool Exists<T>(string id) where T : BaseModel, new() { return dal.Exists<T>(id); }

/// <summary>
/// 增長一條數據
/// </summary>
public bool Add<T>(T model) where T : BaseModel, new() { return dal.Add<T>(model); }

/// <summary>
/// 增長多條數據
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="modelList"></param>
/// <returns></returns>
public bool Add<T>(List<T> modelList) where T : BaseModel, new() { return dal.Add<T>(modelList); }

/// <summary>
/// 更新一條數據
/// </summary>
public bool Update<T>(T model) where T : BaseModel, new() { return dal.Update<T>(model); }

/// <summary>
/// 更新多條數據
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="model"></param>
/// <returns></returns>
public bool Update<T>(List<T> modelList) where T : BaseModel, new() { return dal.Update<T>(modelList); }

/// <summary>
/// 刪除一條數據
/// </summary>
public bool Delete<T>(string id) where T : BaseModel, new() { return dal.Delete<T>(id); }

/// <summary>
/// 刪除一條數據
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="id"></param>
/// <returns></returns>
public bool Delete<T>(T model) where T : BaseModel, new() { return dal.Delete<T>(model); }

/// <summary>
/// 刪除多條數據
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="model"></param>
/// <returns></returns>
public bool Delete<T>(List<T> modelList) where T : BaseModel, new() { return dal.Delete<T>(modelList); }

/// <summary>
/// 批量刪除數據
/// </summary>
/// <param name="idList">逗號分隔</param>
/// <returns></returns>
public bool DeleteList<T>(string idList) where T : BaseModel, new() { return dal.DeleteList<T>(idList); }

/// <summary>
/// 獲得一個對象實體
/// </summary>
public T GetModel<T>(string id) where T : BaseModel, new() { return dal.GetModel<T>(id); }

/// <summary>
/// 獲得所有對象實體
/// </summary>
public List<T> GetModelList<T>(string strWhere) where T : BaseModel, new() { return dal.GetModelList<T>(strWhere); }

/// <summary>
/// 得到數據列表
/// </summary>
public DataSet GetList<T>(string strWhere) where T : BaseModel, new() { return dal.GetList<T>(strWhere); }

/// <summary>
/// 得到數據表
/// </summary>
public DataTable GetTable<T>(string strWhere) where T : BaseModel, new() { return dal.GetTable<T>(strWhere); }

/// <summary>
/// 根據分頁得到數據列表
/// </summary>
public DataSet GetList<T>(string strWhere, string orderby, int startIndex, int endIndex) where T : BaseModel, new() { return dal.GetList<T>(strWhere, orderby, startIndex, endIndex); }

/// <summary>
/// 根據分頁得到數據列表
/// </summary>
public DataSet GetListBySql(string sql, string orderby, int startIndex, int endIndex) { return dal.GetListBySql(sql, orderby, startIndex, endIndex); }

/// <summary>
/// 執行SQL語句,返回影響的記錄數
/// </summary>
/// <param name="SQLString">SQL語句</param>
/// <returns>影響的記錄數</returns>
public int ExecuteSql(string SQLString) { return dal.ExecuteSql(SQLString); }

/// <summary>
/// 執行查詢語句,返回DataSet
/// </summary>
/// <param name="SQLString">查詢語句</param>
/// <returns>DataSet</returns>
public DataSet Query(string SQLString) { return dal.Query(SQLString); }

/// <summary>
/// 執行多條SQL語句,實現數據庫事務。
/// </summary>
/// <param name="SQLStringList">SQL語句的哈希表(key爲sql語句,value是該語句的SqlParameter[])</param>
public void ExecuteSqlTran(Hashtable SQLStringList) { dal.ExecuteSqlTran(SQLStringList); }
#endregion 成員方法

  

 

由於這仍是個公司框架,還不能開源,不過看完以上接口定義以後,已經知道個大概了吧。spa

 

數據邏輯抽象工廠,由抽象接口層,根據配置建立抽象實例,還有緩衝池等。3d

 

數據邏輯基礎方法和接口orm

這些很基礎的,網上也一大把,我只是爲了配合框架作了一些小修改。對象

 

 

接着,每創立一個新項目,都不須要從新再去重作一套orm 了, 引入以上所說的。blog

接着,新增一個Model層,值得主意的是,Model要繼承Yc.Base的BaseModel。繼承

示例:

/// <summary>
    /// 用戶表
    /// </summary>
    [System.Serializable()]
    public class UserInfo : Yc.Base.BaseModel
    {
        /// <summary>
        /// 初始化
        /// </summary>
        public UserInfo()
        {
            this.TableUser = "test";
        }
        private System.String _userInfoId;

        /// <summary>
        /// 用戶Id,NVARCHAR2,128
        /// </summary>
        public System.String UserInfoId
        {
            get
            {
                return _userInfoId ==null? "":_userInfoId;
            }
            set
            {
                _userInfoId = value;
            }
        }        
        private System.String _name;

        /// <summary>
        /// 用戶名,NVARCHAR2,500
        /// </summary>
        public System.String Name
        {
            get
            {
                return _name ==null? "":_name;
            }
            set
            {
                _name = value;
            }
        }        
        private System.String _pwd;

        /// <summary>
        /// 用戶密碼,NVARCHAR2,500
        /// </summary>
        public System.String Pwd
        {
            get
            {
                return _pwd ==null? "":_pwd;
            }
            set
            {
                _pwd = value;
            }
        }        
    }

  

再引用你該項目的Model層

一切就這樣簡單,但願對你們瞭解orm有所幫助!

 

by:五加乘

相關文章
相關標籤/搜索