C# Programming
07-31-2012, 08:05 AM
Assume a table called tblCompanies and another called tblUsers. Each Company can have one or more Users.
So I have this AddUser method. See the comments in-line
private int AddUser(UserEntity User){ int retVal = 0; using (var dc = getDataContext()) { var user = (from u in dc.tblUsers where u.CompanyId == User.CompanyId && u.UserName.Trim().ToLower() == User.UserName.Trim().ToLower() select u).FirstOrDefault(); if (user == null) { tblUser newUser = new tblUser { CompanyId = User.CompanyId, RoleId = User.RoleId, FirstName = User.FirstName, LastName = User.LastName, UserName = User.UserName, Password = User.Password, CanLogIn = User.CanLogIn }; // Wrap this in a try/catch, or handle exceptions in the BLL? dc.tblUsers.InsertOnSubmit(newUser); retVal = newUser.UserId; } else { // If here, the user already exists. Throw an exception here? } } return retVal;}
Aside from try/catch & exceptions, what about the 2 potential FK violations on CompanyId and RoleId?
What's the right way to handle all this. I'v heard people say "Handle these issues in the BLL", while other folks seem to think exceptions related to data should be handled in the DAL?
What's your thoughts?
If it's not broken, fix it until it is
So I have this AddUser method. See the comments in-line
private int AddUser(UserEntity User){ int retVal = 0; using (var dc = getDataContext()) { var user = (from u in dc.tblUsers where u.CompanyId == User.CompanyId && u.UserName.Trim().ToLower() == User.UserName.Trim().ToLower() select u).FirstOrDefault(); if (user == null) { tblUser newUser = new tblUser { CompanyId = User.CompanyId, RoleId = User.RoleId, FirstName = User.FirstName, LastName = User.LastName, UserName = User.UserName, Password = User.Password, CanLogIn = User.CanLogIn }; // Wrap this in a try/catch, or handle exceptions in the BLL? dc.tblUsers.InsertOnSubmit(newUser); retVal = newUser.UserId; } else { // If here, the user already exists. Throw an exception here? } } return retVal;}
Aside from try/catch & exceptions, what about the 2 potential FK violations on CompanyId and RoleId?
What's the right way to handle all this. I'v heard people say "Handle these issues in the BLL", while other folks seem to think exceptions related to data should be handled in the DAL?
What's your thoughts?
If it's not broken, fix it until it is