在3tier体系结构中分离层任务 [英] separating layers tasks in 3tier architecture

查看:93
本文介绍了在3tier体系结构中分离层任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,有人可以告诉我,在3tier win应用程序中,应在BLL或数据库(作为存储过程)中实施避免插入重复的用户名"之类的策略?

Hi can anyone tell me that in a 3tier win application, policies like ''avoid inserting duplicate username'' should be implemented in the BLL, or in database (as stored procedures) ?

推荐答案

您可以按照以下方式进行操作

检查是否重复
创建一种方法来检查数据库中用户名是否已存在
You can do that in following ways

Check if duplicate
Create a method to check the username is already exists in database
public bool IsUsernameExists(string strUsername)
{
  //Check the Username is exists in database Ex Query like SELECT * FROM Table WHERE [UserName]=@strUsername

}




引发异常
在表中的UserName列上放置一个约束.如果您尝试保存重复的名称,则会引发错误,根据错误,您可以向用户抛出适当的消息.

现在您决定.


Or

Throw the exception
Put a constraint for the UserName column in table. If you try to save duplicate name then it will raise the error, based on the error you can throw the appropriate message to the user.

Now you decide.


您可以通过两种方式进行操作.但是,如果您问我更喜欢哪一个,我将在BLL中实施.在数据库级别实现这种验证的方法不是那么好的方法.一个缺点是将异常从DAL传播到BLL,然后到UI,这比从BLL传播到UI要低一级.因此,无需在DB中弄乱,在BLL中实现会更好.
You can do it in both ways. However if you ask me which one I prefer, I would go implementing in BLL. It is not as such good way of implement such kind of validation in DB level. One draw back is propagating the exception from DAL to BLL and then to UI is one step down than propagating from BLL to UI. So without messing around in DB, it is much better implementing in BLL. See thatraja example carefully.


通常,我将这样的事情放在业务逻辑层–中.毕竟,这是业务逻辑.但是,在特殊情况下,假设对表键有唯一性约束,只需在表上设置主键(或者,如果不是键列,则为唯一性约束)会为您带来额外的好处效果,因此您实际上不需要在任何地方编写任何代码. (当然,假设您使用关系数据库作为数据存储层.)
Generally I would put things like this in the business logic layer – after all, it is business logic. However, in the special case of unique constraints on what is presumably a table key, simply setting the primary key (or, if it''s not the key column, a unique constraint) on the table will give you this as a bonus side effect so you don''t actually need to write any code anywhere. (That assuming you''re using a relational database as the data storage layer, of course.)


这篇关于在3tier体系结构中分离层任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆