本节介绍建立Model实体的过程。
从本节开始的连续几节,我们要学习创建DAL层。依照以下顺序创建项目:
1、DBUtility
这个是数据库访问的基础,包含对应SqlServer的访问类SqlHelper和对应Oracle的访问类OracleHelper。创建好后,直接把PetShop中的两个类Ctrl+C过来就好。嘿嘿,我是很懒的。要注意的是这里使用了ConfigurationManager类来读取配置文件中的连接字符串
//数据库连接字符串
public static readonly string ConnectionStringLocalTransaction = ConfigurationManager.ConnectionStrings["SQLConnString1"].ConnectionString;
public static readonly string ConnectionStringInventoryDistributedTransaction = ConfigurationManager.ConnectionStrings["SQLConnString2"].ConnectionString;
public static readonly string ConnectionStringOrderDistributedTransaction = ConfigurationManager.ConnectionStrings["SQLConnString3"].ConnectionString;
public static readonly string ConnectionStringProfile = ConfigurationManager.ConnectionStrings["SQLProfileConnString"].ConnectionString;
要在配置文件中提供相应的配置信息才能够运行。顺便说下,PetShop中的数据库分为4个:
MSPetShop4——PetShop信息库,存储商品信息,供应商信息等基础信息
MSPetShop4Orders——订单库,存储订单信息
MSPetShop4Profiles——用户信息库,存储用户信息、购物车信息等
MSPetShop4Services——服务信息库,存储各种应用程序信息,dll路径等。
上面的四个连接,就分别对应这四个数据库
2、Model
这个是系统的实体类,按照自己的业务实体创建实体类吧。一般来说,我们的实体类有可能会用到Soap这样的协议中去,所以支持序列化还是必要的。不复杂,直接在类的头上加上[Serializable]这个特性描述就好。如:
/// <summary>
/// 订单业务实体
/// </summary>
[Serializable]
public class OrderInfo {
// 成员变量
private int orderId;
private DateTime date;
private string userId;
private CreditCardInfo creditCard;
private AddressInfo billingAddress;
private AddressInfo shippingAddress;
private decimal orderTotal;
private LineItemInfo[] lineItems;
private Nullable<int> authorizationNumber;
/// <summary>
/// 默认构造方法
/// WebService 的序列化机制需要
/// </summary>
public OrderInfo() { }
/// <summary>
/// 带初始值的构造方法
/// </summary>
/// <param name="orderId">主键</param>
/// <param name="date">订货日期</param>
/// <param name="userId">订货人(编号)</param>
/// <param name="creditCard">订货信用卡</param>
/// <param name="billing">订货地址</param>
/// <param name="shipping">送货地址</param>
/// <param name="total">订单价值</param>
/// <param name="line">订单商品(数组)</param>
/// <param name="authorization">信用卡注册号</param>
public OrderInfo(int orderId, DateTime date, string userId, CreditCardInfo creditCard, AddressInfo billing, AddressInfo shipping, decimal total, LineItemInfo[] line, Nullable<int> authorization) {
this.orderId = orderId;
this.date = date;
this.userId = userId;
this.creditCard = creditCard;
this.billingAddress = billing;
this.shippingAddress = shipping;
this.orderTotal = total;
this.lineItems = line;
this.authorizationNumber = authorization;
}
// 属性
public int OrderId {
get { return orderId; }
set { orderId = value; }
}
public DateTime Date {
get { return date; }
set { date = value; }
}
public string UserId {
get { return userId; }
set { userId = value; }
}
public CreditCardInfo CreditCard {
get { return creditCard; }
set { creditCard = value; }
}
public AddressInfo BillingAddress {
get { return billingAddress; }
set { billingAddress = value; }
}
public AddressInfo ShippingAddress {
get { return shippingAddress; }
set { shippingAddress = value; }
}
public decimal OrderTotal {
get { return orderTotal; }
set { orderTotal = value; }
}
public LineItemInfo[] LineItems {
get { return lineItems; }
set { lineItems = value; }
}
public Nullable<int> AuthorizationNumber {
get {return authorizationNumber;}
set {authorizationNumber = value;}
}
}
要注意的是需要一个默认构造方法,即使你没有使用它。因为在WebService中如果序列化这个实体,需要这个默认构造
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/onight/archive/2007/11/18/1891255.aspx