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
新闻热点
疑难解答