For my authentication model, I want to make sure that it is impossible for the same email to be registered more than once. I am new to both nHibernate and partially LINQ, so I am asking if this is adequate enough of a 'check' to ensure that happens.
public MembershipCreateStatus CreateUser(string email, string password)
{
if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", "userName");
if (String.IsNullOrEmpty(password)) throw new ArgumentException("Value cannot be null or empty.", "password");
using (var session = sessionFactory.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
var members = session.CreateCriteria<Member>().List<Member>();
// determine is the email address already exists in the database
if (members.Any(i => i.Email == email))
return MembershipCreateStatus.DuplicateEmail;
// create the new member, if they are valid
var member = new Member { Email = email };
session.SaveOrUpdate(member);
transaction.Commit();
}
}
return MembershipCreateStatus.Success;
}
Is there a more intelligent way of accomplishing this? I've had trouble with this in the past with a previous program (I used Linq to SQL on that one), and so I wanted to get some expert advice, this time around.