[asp.net]学习PetShop3.0(1)用户注册

谢谢Notus[http://api.533.net/]授予转载权限。

下面来分析一下PetShop3.0的用户注册部分(我今早上刚研究的,哈,趁热端出来)
PetShop3.0是业务实体和业务逻辑分开的,并且在表示层上也有逻辑处理。业务实体部分从前到后都有用到。实际上,在传递数据的时候就是传递的一个实体,而不是像我们一般用的一个变量一个变量的传,在用户注册中也是这样。
注册页面是CreateAccount.aspx,这里有一个usercontrol:AddressUI,用来收集用户的一般信息,其他的个人网站设定和用户名密码什么的都是分开来取的,通过提取AddressUI.Address来获得一个AddressInfo对象,然后用这些信息创建一个AccountInfo对象,最后调用ProcessFlow.AccountController的CreateAccount方法来完成注册。CreateAccount接收的参数自然是一个AddressInfo类型的对象,返回类型为bool。根据返回值来判断注册是否成功。实际上,它这里假定如果不成功,那就只有一种情况,就是用户名已经被注册了。
接下来的事情就是一层套一层的引用了。把业务实体AccountInfo一层的往下传,最后到达SQLServerDAL层,这里的Insert方法执行最后的操作。
PetSop.Web.ProcessFlow.AccountController :

ExpandedBlockStart.gif ContractedBlock.gif public   bool  CreateAccount(AccountInfo newAccountInfo) dot.gif {
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
try dot.gif{
InBlock.gif
// Creata a new business logic tier
InBlock.gif
Account account = new Account();
InBlock.gif
InBlock.gif
// Call the insert method
InBlock.gif
account.Insert(newAccountInfo);
InBlock.gif
InBlock.gif
// Store the data in session state and store the authenticated cookie
InBlock.gif
HttpContext.Current.Session[ACCOUNT_KEY] = newAccountInfo;
InBlock.gifFormsAuthentication.SetAuthCookie(newAccountInfo.UserId, 
false);
InBlock.gif
InBlock.gif
//Finally forward to the welcome page
InBlock.gif
HttpContext.Current.Response.Redirect(URL_ACCOUNTCREATE, true);
InBlock.gif
ExpandedSubBlockEnd.gif}

InBlock.gif
//注意在这里捕获异常,说明用户名已存在。详细描述见下面
ExpandedSubBlockStart.gifContractedSubBlock.gif
catch dot.gif{
InBlock.gif
return false;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
return true;
ExpandedBlockEnd.gif}

None.gif

PetShop.BLL.Account :

ExpandedBlockStart.gif ContractedBlock.gif public   void  Insert(AccountInfo account)  dot.gif {
InBlock.gif
InBlock.gif
// Validate input
InBlock.gif
if (account.UserId.Trim() == string.Empty)
InBlock.gif
return;
InBlock.gif
InBlock.gif
// Get an instance of the account DAL using the DALFactory
InBlock.gif
IAccount dal = PetShop.DALFactory.Account.Create();
InBlock.gif
InBlock.gif
// Call the DAL to insert the account
InBlock.gif
dal.Insert(account);
ExpandedBlockEnd.gif}

None.gif

最后进入实际的数据操作层
PetShop.SQLServerDAL.Account :

ExpandedBlockStart.gif ContractedBlock.gif public   void  Insert(AccountInfo acc)  dot.gif {
InBlock.gifSqlParameter[] signOnParms 
= GetSignOnParameters();
InBlock.gifSqlParameter[] accountParms 
= GetAccountParameters();
InBlock.gifSqlParameter[] profileParms 
= GetProfileParameters();
InBlock.gif
InBlock.gifsignOnParms[
0].Value = acc.UserId;
InBlock.gifsignOnParms[
1].Value = acc.Password;
InBlock.gif
InBlock.gifSetAccountParameters(accountParms, acc);
InBlock.gifSetProfileParameters(profileParms, acc);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
using (SqlConnection conn = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC)) dot.gif{
InBlock.gifconn.Open();
ExpandedSubBlockStart.gifContractedSubBlock.gif
using (SqlTransaction trans = conn.BeginTransaction()) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
try dot.gif{
InBlock.gifSQLHelper.ExecuteNonQuery(trans, CommandType.Text, SQL_INSERT_SIGNON, signOnParms);
InBlock.gifSQLHelper.ExecuteNonQuery(trans, CommandType.Text, SQL_INSERT_ACCOUNT, accountParms);
InBlock.gifSQLHelper.ExecuteNonQuery(trans, CommandType.Text, SQL_INSERT_PROFILE, profileParms);
InBlock.giftrans.Commit();
InBlock.gif
ExpandedSubBlockEnd.gif}

InBlock.gif
//违反约束,抛出异常
ExpandedSubBlockStart.gifContractedSubBlock.gif
catch dot.gif{
InBlock.giftrans.Rollback();
InBlock.gif
throw;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif

那么它是怎么判断用户名是否已经被注册了呢?原来,在保存用户名和密码的表里有一个主键约束,这样自然就插不进重复的用户名。一旦有相同的用户名进入,就会违反约束,抛出异常,当然在之前还要回滚事务,抛出的异常在表示层CreateAccount方法中被捕获,方法返回false,最后反映到页面上。
我在这里就有一个疑问,这样做不是把异常作为一种控制流程的手段了吗?
《effective java》第39条“只针对不正常的条件才使用异常”。根据这一条,万一不能注册是因为其他不可预料的原因而发生的呢?这也会返回给要注册的用户“Duplicate user ID! Please try again.”信息。而且我以前看过一篇文章,说.net的异常抛出会消耗大量的资源,建议不要把异常做为一种实现的方法。其实这里完全可以用T-SQL编程的手段来预先判断用户名是否存在,然后再采取下一步措施。



转载于:https://www.cnblogs.com/catcn/archive/2005/07/26/200223.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值