|
本帖最后由 1602264241 于 2013-7-26 20:23 编辑
为了节省开发周期我们利用CodeSmith代码生成器,实现将数据库中的表自动生成C#的实体类和数据访问层。
其中数据表中的外键将自动生成外键属性,数据库表外键字段定义为xxxId即可。
例如我想把下面的Order表生成实体类(Order)和数据访问层(OrderService),表结构如下所示。
Order表定义
最终生成Model层如下:- using System;
- namespace Mybookshop.Models{
- /// <summary>
- /// (Order)实体类
- /// </summary>
- public class Order
- {
- /// <summary>
- /// (Order)实体类
- /// </summary>
- public Order()
- {
- _User=new User();
- }
-
- #region 字段
- private int _Id;
- private System.DateTime _OrderDate;
- private User _User;
- private decimal _TotalPrice;
- #endregion 字段
- #region 属性
- /// <summary>
- ///
- /// </summary>
- public int Id
- {
- set { _Id = value; }
- get { return _Id; }
- }
-
- /// <summary>
- ///
- /// </summary>
- public System.DateTime OrderDate
- {
- set { _OrderDate = value; }
- get { return _OrderDate; }
- }
-
- public User User
- {
- set { _User = value; }
- get { return _User; }
- }
-
- /// <summary>
- ///
- /// </summary>
- public decimal TotalPrice
- {
- set { _TotalPrice = value; }
- get { return _TotalPrice; }
- }
-
- #endregion 属性
- }
- }
复制代码 DAL层如下:- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.Sql;
- using System.Data.SqlClient;
- using Mybookshop.Models;
- namespace Mybookshop.DAL
- {
- /// <summary>
- /// SysFunService 类
- /// </summary>
- public class OrderService
- {
- #region 查询
- public static Order GetOrderById(int id)
- {
- string sql = "SELECT * FROM Order WHERE Id = @id";
- SqlParameter p=new SqlParameter("@id",id);
- DataTable dt=DBHelper.GetDataSet(sql,p);
- IList<Order> list=GetList(dt);
- if(list.Count>0) return list[0];
- return null;
- }
-
- #endregion
-
- #region 修改
-
- public static int ModifyOrder(Order model){
- string sql="UPDATE Orders SET OrderDate=@orderDate,UserId=@userId,TotalPrice=@totalPrice WHERE Id=@id";
- SqlParameter[] para = new SqlParameter[]
- {
- new SqlParameter("@id", model.Id),
- new SqlParameter("@orderDate", model.OrderDate),
- new SqlParameter("@totalPrice", model.TotalPrice),
- new SqlParameter("@userId ", model.User.Id),
- };
- return DBHelper.ExecuteCommand(sql, para);
- }
- #endregion
-
- #region 删除
- public static int DeleteOrder(Order model){
- string sql="DELETE Orders WHERE Id=@id";
- SqlParameter[] para = new SqlParameter[]
- {
- new SqlParameter("@id", model.Id)
- };
- return DBHelper.ExecuteCommand(sql, para);
- }
- #endregion
-
- #region 新增
- public static Order AddOrder(Order model){
- string sql="INSERT INTO Orders (OrderDate,UserId,TotalPrice) VALUES (@orderDate,@userId,@totalPrice)";
- SqlParameter[] para = new SqlParameter[]
- {
- new SqlParameter("@orderDate", model.OrderDate),
- new SqlParameter("@totalPrice", model.TotalPrice),
- new SqlParameter("@userId ", model.User.Id),
- };
- int newId = DBHelper.GetScalar(sql, para);
- return GetOrderById(newId);
- }
- #endregion
-
- #region 将DataTable转换成Order集合
- private static IList<Order> GetList(DataTable dt)
- {
- IList<Order> list = new List<Order>();
- foreach (DataRow row in dt.Rows)
- {
- Order model = new Order();
- if (dt.Columns.Contains("Id"))
- model.Id= (int)row["Id"];
- if (dt.Columns.Contains("OrderDate"))
- model.OrderDate= (System.DateTime)row["OrderDate"];
- if (dt.Columns.Contains("TotalPrice"))
- model.TotalPrice= (decimal)row["TotalPrice"];
- if (dt.Columns.Contains("UserId")) //外键处理
- model.User = UserService.GetUserById((int)row["UserId"]);
-
- list.Add(model);
- }
- return list;
- }
- #endregion
-
- }
- }
复制代码 以上rar中包含5个文件:
DAL.cst:生成DAL层的模板
Models.cst:生成Model层的模板
StringProcess.cst:在模板中定义字符串处理类,供模板中的方法调用。
DBHelper.cs:DAL层调用的数据库通用方法
download.txt:包含CodeSmith下载地址
下载地址:
MyTemplates.rar
(4.59 KB, 下载次数: 268)
|
|