NetworkComms網絡通信框架序言
也不能稱之為框架吧,其實就是幾個codesmith模板,只能用于sql server 數據庫,codesmith我所使用的而是6.5版本的。
作用: 生成存儲過程,Data類庫中的類,Business中的Entity類 ,和操作類 (自動生成的只有創建,更新,刪除,分頁獲取 等基本操作,高級的功能還需要手動添加)
原來也用過別的ORM,有時候覺得比較繁瑣,想找個簡單點的方式,這種通過codesmith模板生成 存儲過程 數據層 business層的方式,優點是簡單,直接操作sql,有時候感覺也很優美,
后來就一直在用。這個也不是原創的,是從國外一個開源的網站中學到的。后來擴展了一下,生成的存儲過程是直接支持分頁獲取的的。
2015年2月8日更新 v3版本下載解壓密碼 www.networkcomms.cn
我們在編寫程序的時候,通常有許多類庫,有的是專門用來同數據庫打交道的。如下圖的項目中:
data層是直接同數據庫打交道的,每一個類對應一個表,拿一個來舉例:
類中的方法是直接操作存儲過程的,這樣感覺比較靈活(存儲過程也是由模板生成的,當然有些是要手寫的,模板生成的是幾個基本的,比如添加數據,刪除數據,更新數據)
public static class DBDep { /// <summary> /// Gets the connection string for read. /// </summary> /// <returns></returns> PRivate static string GetReadConnectionString() { return ConfigurationManager.AppSettings["MSSQLConnectionString"]; } /// <summary> /// Gets the connection string for write. /// </summary> /// <returns></returns> private static string GetWriteConnectionString() { if (ConfigurationManager.AppSettings["MSSQLWriteConnectionString"] != null) { return ConfigurationManager.AppSettings["MSSQLWriteConnectionString"]; } return ConfigurationManager.AppSettings["MSSQLConnectionString"]; } /// <summary> /// Inserts a row in the Dep table. Returns new integer id. /// </summary> /// <param name="title"> title </param> /// <returns>int</returns> public static int Create( string title) { SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "Dep_Insert", 1); sph.DefineSqlParameter("@Title", SqlDbType.NVarChar, 100, ParameterDirection.Input, title); int newID = Convert.ToInt32(sph.ExecuteScalar()); return newID; } /// <summary> /// Updates a row in the Dep table. Returns true if row updated. /// </summary> /// <param name="id"> id </param> /// <param name="title"> title </param> /// <returns>bool</returns> public static bool Update( int id, string title) { SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "Dep_Update", 2); sph.DefineSqlParameter("@ID", SqlDbType.Int, ParameterDirection.Input, id); sph.DefineSqlParameter("@Title", SqlDbType.NVarChar, 100, ParameterDirection.Input, title); int rowsAffected = sph.ExecuteNonQuery(); return (rowsAffected > 0); } /// <summary> /// Deletes a row from the Dep table. Returns true if row deleted. /// </summary> /// <param name="id"> id </param> /// <returns>bool</returns> public static bool Delete( int id) { SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "Dep_Delete", 1); sph.DefineSqlParameter("@ID", SqlDbType.Int, ParameterDirection.Input, id); int rowsAffected = sph.ExecuteNonQuery(); return (rowsAffected > 0); } /// <summary> /// Gets an IDataReader with one row from the Dep table. /// </summary> /// <param name="id"> id </param> public static IDataReader GetOne( int id) { SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "Dep_SelectOne", 1); sph.DefineSqlParameter("@ID", SqlDbType.Int, ParameterDirection.Input, id); return sph.ExecuteReader(); } /// <summary> /// Gets an IDataReader with some list row from the Dep table. /// </summary> /// <param name="id"> id </param> public static IDataReader GetTopList( int id) { SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "Dep_SelectTopList", 1); sph.DefineSqlParameter("@pid", SqlDbType.Int, ParameterDirection.Input, id); return sph.ExecuteReader(); } /// <summary> /// Gets a count of rows in the Dep table. /// </summary> public static int GetCount() { return Convert.ToInt32(SqlHelper.ExecuteScalar( GetReadConnectionString(), CommandType.StoredProcedure, "Dep_GetCount", null)); } /// <summary> /// Gets a Listcount of rows in the Dep table. /// </summary> public static int GetListCount(int pid) { SqlParameter theSqlParameter = new SqlParameter("@Pid", pid); return Convert.ToInt32(SqlHelper.ExecuteScalar( GetReadConnectionString(), CommandType.StoredProcedure, "Dep_GetListCount", theSqlParameter)); } /// <summary> /// Gets an IDataReader with all rows in the Dep table. /// </summary> public static IDataReader GetAll() { return SqlHelper.ExecuteReader( GetReadConnectionString(), CommandType.StoredProcedure, "Dep_SelectAll", null); } /// <summary> /// Gets a page of data from the Dep table. /// </summary> /// <param name="pageNumber">The page number.</param> /// <param name="pageSize">Size of the page.</param> /// <param name="totalPages">total pages</param> public static IDataReader GetPage( int pageNumber, int pageSize, out int itemCount) { itemCount = GetCount(); SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "Dep_SelectPage", 2); sph.DefineSqlParameter("@PageNumber", SqlDbType.Int, ParameterDirection.Input, pageNumber + 1); sph.DefineSqlParameter("@PageSize", SqlDbType.Int, ParameterDirection.Input, pageSize); return sph.ExecuteReader(); } public static IDataReader GetListPage( int pageNumber, int pageSize, int pid, out int itemCount) { itemCount = GetListCount(pid); SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "Dep_SelectListPage", 3); sph.DefineSqlParameter("@PageNumber", SqlDbType.Int, ParameterDirection.Input, pageNumber + 1); sph.DefineSqlParameter("@PageSize", SqlDbType.Int, ParameterDirection.Input, pageSize); sph.DefineSqlParameter("@pid", SqlDbType.Int, ParameterDirection.Input, pid); return sph.ExecuteReader(); } }
Business層有想對應的類
我們分別來看一下相關的代碼:
Dep類
[ProtoContract] public class Dep { #region Constructors public Dep() { } #endregion #region Private Properties private int iD = -1; private string title = string.Empty; #endregion #region Public Properties [ProtoMember(1)] public int ID { get { return iD; } set { iD = value; } } [ProtoMember(2)] public string Title { get { return title; } set { title = value; } } #endregion }
DoDep類
public class DoDep { #region Private Methods /// <summary> /// Gets an instance of Dep. /// </summary> /// <param name="id"> id </param> private static Dep GetDep( int id) { using (IDataReader reader = DBDe
新聞熱點
疑難解答