Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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) ?
Posted

You can do that in following ways

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

}

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.
 
Share this answer
 
Comments
Wonde Tadesse 9-Jul-11 23:42pm    
5+
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.
 
Share this answer
 
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.)
 
Share this answer
 
Firstly, do you want to use any built-in tool? Nextly, if you want a simple validation, then YES, surely that can be done in BLL. Put a simple validation by checking the Data Set you have on that moment and run validation block or function. Stored Procedure can also be used and in that case definitely your application would have a better performance.

Finally, if you can give your BLL and DAL then I can write the code for you also. Obviously, if do not have any other problem giving code.
 
Share this answer
 
thanks, so if I do that in BLL, it reduces one level of propagating exceptions, but also it needs to refer to database twice, first to see if it exists,next insert the record. so which one is preferred?!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900