I have a best-practice question. I'm using NHiberate 2.0.1 with Burrow and ASP.NET.
Say I have a User domain class that has a rule that no two users with the same username may exist. To enforce this in the database I add a unique constraint to the "username" column. I realize that I need to enforce this in my business logic as well.
What are some valid ways this can be handled? I've considered:
1. Injecting a DAO instance into User objects and having them call a DAO method that checks for an existing User with the same username. I assume a problem here is the off chance of a race condition where the DAO checks for a duplicate, doesn't find one, and then before the database update happens, someone else inserts a User with that name.
2. Catching the exception raised by NHiberate when the database constraint is violated. My problem with this approach is since Burrow is flushing the session at the end of the HTTP request, the only option I think I have is to catch it in the global exception handler for the web application and redirect to an error page. I won't be able to give any specific error message on the page they were working with. Also, some of my business logic is now expressed in my web application not in my domain.
In addition, is it normal/acceptable to enforce business logic that requires looking at the database in a domain class itself like in option 1?
Thanks for any help, ideas, or shoves in the right direction.
|