Fluent NHibernate - unit-testing a one-to-many *inverse* mapping

169 views
Skip to first unread message

Cristian Diaconescu

unread,
Dec 2, 2009, 4:18:27 AM12/2/09
to Fluent NHibernate
I can't figure out how (or if it's at all possible) to use the Fluent
NHibernate `PersistenceSpecification<T>.CheckList(...)` method to
check an *inverse* one-to-many mapping.

Here's what I'm trying to do:

Using fluent nhibernate on a vanilla blog/post example, I have defined
a one-to-many blog<->posts mapping.
One restriction I want is that a post must ALWAYS point to the blog it
corresponds to, right from the moment it is added to the database.
(basically, the BlogID column in the Posts table is NOT NULL)

For this reason, I defined the mapping like this:

public class BlogMap: ClassMap<Blog>
{
public BlogMap(){
[...]
HasMany(x => x.Posts)
.Inverse()
.Cascade.All();
}

The Blog class has a `AddPosts(IList<Post> posts)` method which takes
care of both sides of the mapping, so when the Post instances are
INSERTed in the database, their BlogID column is already populated
with the corresponding Blog ID.

So far, so good.

The problem is, when using

new PersistenceSpecification<Blog>(GetSession(), eq)
.CheckList(c => c.Posts,
new [] {new Post{Name = "Post 1"}, new Post
{Name = "Post 2"}})
.VerifyTheMappings();

in a mapping unit-test, I get this exception:

"System.ApplicationException: Actual count does not equal expected
count"

...which is to be expected, since the BlogID value of the posts is not
set anywhere.
I'm wondering if I can somehow get access to the `Blog` instance
created behind the scenes by the `PersistenceSpecification`, so I can
manually call the AddBlogs(...) method before doing the checks.

If I remove the `.Inverse()` from the mapping (and also remove the
database NOT NULL constraint for the BlogID column), the test passes.

Any ideas?

Paul Batum

unread,
Dec 2, 2009, 5:44:41 AM12/2/09
to fluent-n...@googlegroups.com
Try using CheckEnumerable instead of CheckList.  You use it roughly like this:

_spec.CheckEnumerable(x => x.EnumerableOfKittens, (cat, kitten) => cat.AddKitten(kitten), kittens);

Its marked as obsolete but I now think that was an oversight. If that sorts you out, I'll be sure to see that we remove the obsolete attribute.



--

You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group.
To post to this group, send email to fluent-n...@googlegroups.com.
To unsubscribe from this group, send email to fluent-nhibern...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/fluent-nhibernate?hl=en.



Alexander Groß

unread,
Dec 2, 2009, 11:35:28 AM12/2/09
to Fluent NHibernate
> Its marked as obsolete but I now think that was an oversight. If that sorts
> you out, I'll be sure to see that we remove the obsolete attribute.

CheckEnumerable is marked obsolete because there is an overload of
CheckList that accepts a setter delegate. The signature is basically
the same as CheckEnumerable with the last two parameters exchanged:

_spec.CheckList(x => x.EnumerableOfKittens, kittens, (cat, kitten) =>
cat.AddKitten(kitten));

Please note similar overloads for other built-in checks (property,
reference, etc).

You can implement custom checks by deriving from
FluentNHibernate.Testing.Values.Property<T> and adding extension
methods to the PersistenceSpecification, as shown in
FluentNHibernate.Testing.PersistenceSpecificationExtensions.

HTH,
Alex

Mikael Henriksson

unread,
Dec 2, 2009, 12:10:27 PM12/2/09
to fluent-nhibernate
Thanks Alexander :)

2009/12/2 Alexander Groß <agr...@therightstuff.de>

cliff vaughn

unread,
Dec 2, 2009, 1:36:18 PM12/2/09
to fluent-nhibernate
i haven't checked the code, but I hope that referral to CheckList is in or added to the comment of the Obsolete attribute on CheckEnumerable. :)
--
thanks

cliff

Alexander Groß

unread,
Dec 2, 2009, 3:01:51 PM12/2/09
to fluent-n...@googlegroups.com

It is.

 

--

Alexander Groß

http://therightstuff.de/

Paul Batum

unread,
Dec 2, 2009, 4:48:44 PM12/2/09
to fluent-n...@googlegroups.com
Ahh, so the parameters are just swapped around. I hoped I'd just missed something simple like that. Thanks for sorting us out Alexander.

2009/12/3 Alexander Groß <agr...@therightstuff.de>
Reply all
Reply to author
Forward
0 new messages