学习 PetShop3.0
1、用户注册
PetShop3.0 是业务实体和业务逻辑分开的,并且在表示层上也有逻辑处理。业务实体部分
从前到后都有用到。实际上,在传递数据的时候就是传递的一个实体,而不是像我们一般
用的一个变量一个变量的传,在用户注册中也是这样。
注 册页面是 CreateAccount.aspx,这里有一个 usercontrol:AddressUI,用来收集用户的一
般 信 息 , 其 他 的 个 人 网 站 设 定 和 用 户 名 密 码 什 么 的 都 是 分 开 来 取 的 , 通 过 提 取
AddressUI.Address 来获得一个 AddressInfo 对象,然后用这些信息创建一个 AccountInfo 对
象 , 最 后 调 用 ProcessFlow.AccountController 的 CreateAccount 方 法 来 完 成 注 册 。
CreateAccount 接收的参数自然是一个 AddressInfo 类型的对象,返回类型为 bool。根据返回
值来判断注册是否成功。实际上,它这里 假定如果不成功,那就只有一种情况,就是用户
名已经被注册了。
接下来的事情就是一层套一层的引用了。把业务实体 AccountInfo 一层的往下传,最后到达
SQLServerDAL 层,这里的 Insert 方法执行最后的操作。
PetSop.Web.ProcessFlow.AccountController :
public bool CreateAccount(AccountInfo newAccountInfo){
try {
// Creata a new business logic tier
Account account = new Account();
// Call the insert method
account.Insert(newAccountInfo);
// Store the data in session state and store the authenticated cookie
HttpContext.Current.Session[ACCOUNT_KEY] = newAccountInfo;
FormsAuthentication.SetAuthCookie(newAccountInfo.UserId, false);
//Finally forward to the welcome page
HttpContext.Current.Response.Redirect(URL_ACCOUNTCREATE, true);
}
//注意在这里捕获异常,说明用户名已存在。详细描述见下面
catch {
return false;
}
return true;
}
PetShop.BLL.Account :
public void Insert(AccountInfo account) {
// Validate input
if (account.UserId.Trim() == string.Empty)