PersistenceSpecification

Maxus's Avatar

Maxus

23 Oct, 2010 05:18 AM via web

Hi There,

I have a relationship of:

public class AccountMapping
{
    public AccountMapping()
    {
        // Fields:
        Map(x => x.Name).Length(50);

        // Relationships:
        HasMany(x => x.Users).Cascade.All();
    }
}

public class UserMapping 
{
    public UserMapping()
    {
        // Fields:
        Map(x => x.Login).Length(50).Unique();

        // Relationships:
        References(x => x.Account).Cascade.All().Not.Nullable();
    }
}

Im trying to test an account is created correctly using the PersistenceSpecification class.

   [Test]
    public void Can_Correctly_Map_Account()
    {
        var account = DataHelper.GenerateFakeAccount();

        new PersistenceSpecification<Account>(Session)
            .CheckProperty(c => c.Name, "Name")
            .CheckList(c => c.Users, new List<User>() { new User { Login = "LoginPerson"  } } )
            .VerifyTheMappings();
    }

I end up in a chicken and the egg situation, when the persistance spec saves the account I need an account for the user, but one hasn't been created, Is there a way to handle this situation?

Thanks!
Maxus

  1. Support Staff 2 Posted by Paul Batum on 24 Oct, 2010 03:35 AM

    Paul Batum's Avatar

    It sort of depends on how you have your domain objects maintaining the
    relationship. Most people will use Account.AddUser(User u) and
    Account.RemoveUser(User u) methods and allow those methods to maintain the
    bidirectional relationship. There's a good way to use the persistence spec
    to check these:
    http://groups.google.com/group/fluent-nhibernate/browse_thread/thread/cebc70ff873e4fd2/

    On Sat, Oct 23, 2010 at 4:20 PM, Maxus <
    ***@tenderapp.com<tender%***@tenderapp.com>
    > wrote:

  2. 3 Posted by Maxus on 24 Oct, 2010 07:16 AM

    Maxus's Avatar

    Hi Paul,

    Yes that is eaxctly what I'm looking for, but I'm still getting:

    NHibernate.PropertyValueException : not-null property references a null or transient value User.Account

    the add method on the account is:

        public virtual void AddUser(User user)
        {
            user.Account = this;
            Users.Add(user);
        }

    In the PersistenceSpecification i have added this:

    .CheckList(c => c.Users, users, (a, u) => a.AddUser(u))

    The account entity is simple:

    public class Account : DomainEntity
    {
        public virtual string Name { get; set; }
        public virtual IList<User> Users { get; private set;}
    
        public Account()
        {
            Users = new List<User>();
        }
    
        public virtual void AddUser(User user)
        {
            user.Account = this;
            Users.Add(user);
        }
    
        public virtual void RemoveUser(User user)
        {
            if (!Users.Contains(user)) { return; }
            user.Account = null;
            Users.Remove(user);
        }
    }

    The user entity:

    public class User : DomainEntity
    {
        public virtual string Login { get; set; }
        public virtual Account Account { get; set; }
    
        public User(){}
    }

    I think im just missing something simple, any ideas?

    Thanks,
    M

  3. 4 Posted by Maxus on 24 Oct, 2010 11:57 AM

    Maxus's Avatar

    Seem to have got it working, not exactly sure how, if i figure it out will post the details.

    Thank you for your help Paul.
    -M

Reply to this discussion

Preview Comments are parsed with Markdown. Help with syntax

Attached Files

    You can attach files up to 10MB

    Ten divided by two is what?