/// <summary>
        /// Validates and saves a member.
        /// </summary>
        /// <param name="member">The member to save.</param>
        /// <returns>
        /// A <see cref="ValidationResult"/> indicating whether the member was saved.
        /// </returns>
        public ValidationResult SaveMember(Member member)
        {
            using (var transaction = Session.BeginTransaction(IsolationLevel.Serializable))
            {
                var validationResult = member.Validate();

                // Ensure OpenID is unique
                if (Session.CreateCriteria(typeof(Member))
                    .Add(Restrictions.Like("OpenId", member.OpenId))
                    .Add(Restrictions.Not(Restrictions.Eq("Id", member.Id)))
                    .SetLockMode(LockMode.Upgrade)
                    .UniqueResult() != null)
                {
                    validationResult.FlagRule("OpenId", string.Format("The OpenID '{0}' is already in use.", member.OpenId));
                }

                // Commit the record
                if (validationResult.Valid)
                {
                    Session.SaveOrUpdate(member);
                    transaction.Commit();
                }
                return validationResult;
            }
        }
 public void CanCreateMember()
 {
     using (var session = Database.SessionFactory.OpenSession())
     {
         var member = new Member() { OpenId = "www.paulstovell.com/1", FullName = "Paul Stovell", EmailAddress = "*****@*****.**", LastLogin = DateTime.Now };
         var repository = new MembershipRepository(session);
         repository.SaveMember(member);
     }
 }
 public void CreatingMembersWithSameOpenIDWillReturnValidationErrorOnSave()
 {
     using (var session = Database.SessionFactory.OpenSession())
     {
         var member1 = new Member() { OpenId = "www.paulstovell.com/2", FullName = "Paul Stovell", EmailAddress = "*****@*****.**", LastLogin = DateTime.Now };
         var member2 = new Member() { OpenId = "www.paulstovell.com/2", FullName = "Paul Stovell", EmailAddress = "*****@*****.**", LastLogin = DateTime.Now };
         var repository = new MembershipRepository(session);
         var result1 = repository.SaveMember(member1);
         var result2 = repository.SaveMember(member2);
         Assert.AreEqual(true, result1.Valid);
         Assert.AreEqual(false, result2.Valid);
         Assert.AreEqual("The OpenID 'www.paulstovell.com/2' is already in use.", result2.ForRule("OpenId"));
     }
 }
 public void CannotChangeMemberOpenIDIfNewIDAlreadyInUse()
 {
     using (var session = Database.SessionFactory.OpenSession())
     {
         var member1 = new Member() { OpenId = "www.paulstovell.com/5", FullName = "Paul Stovell", EmailAddress = "*****@*****.**", LastLogin = DateTime.Now };
         var member2 = new Member() { OpenId = "www.paulstovell.com/6", FullName = "Paul Stovell", EmailAddress = "*****@*****.**", LastLogin = DateTime.Now };
         var repository = new MembershipRepository(session);
         repository.SaveMember(member1);
         repository.SaveMember(member2);
         var member1_1 = repository.FindMemberByOpenId("www.paulstovell.com/6");
         member1_1.OpenId = "www.paulstovell.com/5";
         var result = repository.SaveMember(member1_1);
         Assert.AreEqual(false, result.Valid);
     }
 }
 public void CanChangeMembersOpenIDSoLongAsNewIDIsNotInUse()
 {
     using (var session = Database.SessionFactory.OpenSession())
     {
         var member = new Member() { OpenId = "www.paulstovell.com/4", FullName = "Paul Stovell", EmailAddress = "*****@*****.**", LastLogin = DateTime.Now };
         var repository = new MembershipRepository(session);
         repository.SaveMember(member);
         var member2 = repository.FindMemberByOpenId("www.paulstovell.com/4");
         member2.FullName = "Fred Stovell";
         member2.OpenId = "www.paulstovell.com/4_1";
         repository.SaveMember(member2);
         var member3 = repository.FindMemberByOpenId("www.paulstovell.com/4_1");
         Assert.AreEqual("Fred Stovell", member3.FullName);
         Assert.AreEqual(null, repository.FindMemberByOpenId("www.paulstovell.com/4"));
     }
 }
 private static Member CreateAndSaveMember(ISession session)
 {
     var member = new Member() { OpenId = Guid.NewGuid().ToString(), EmailAddress = "*****@*****.**", FullName = "Paul Stovell", LastLogin = DateTime.Now };
     new MembershipRepository(session).SaveMember(member);
     return member;
 }