私の認証モデルでは、同じ電子メールを複数回登録することができないようにしたいと思います。私はnHibernateと部分的にLINQの両方に新しいので、これが起こることを確実にするための十分な「チェック」があるかどうか尋ねています。電子メールが存在するかどうかを徹底的にチェックする、NHibernate、LINQ
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;
}
もっと知的な方法がありますか?私はこれまで、以前のプログラム(私はLinqをSQLに使っていました)でこの問題を抱えていましたので、今度は専門家のアドバイスを受けたいと思っていました。
@Femaref:苦労していませんが、何を変更しましたか?私は変更を特定できません。 – Ciel
私はNHibernateでこれを行う方法を必ずしも知っているわけではありませんが、あなたが作成するトランザクションがシリアライズ可能であることを確認する必要があるので、チェックした時間と実行した時間の間に重複した電子メールアドレスが挿入されませんインサート。 –
Stacey:フォーマット。あなたのコードのいくつかは、正しく字下げされていないので、フォーマットされませんでした。 – Femaref