1
votes

I've grown accustomed in SQLAlchemy (Python) to map a relationship/collection with lazy="dynamic" which maps the property as a Query object instead of a populated list/collection (or Proxy for lazy loaded properties). This mapped property then allows you to further refine the query used to fetch the collection before doing so (apply an order, limit, filter, etc).

For example, in SQLAlchemy I can map a relationship like so:

class Post(Base):
    ...

class User(Base):
    ...
    posts = relationship(Post, lazy="dynamic")

And then when I retrieve a user, I can apply an order on posts, or only retrieve the last 5, etc.

user = session.query(User).get(1)

# Fetch the last 5 posts by user 1
posts = user.posts.order_by(Post.create_date.desc()).limit(5).all()

http://docs.sqlalchemy.org/en/rel_0_7/orm/collections.html


I would love to find a way to do this using Fluent NHibernate, mapping the collection as a QueryOver or IQueryable (LINQ) such as:

public virtual QueryOver<Post> Posts {get; set;}
or
public virtual IQueryable<Post> Posts { get; set; }

and in the mappings do something like:

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        ...
        HasMany(u => u.Posts).Fetch.Dynamic
    }
}

Is this currently possible using Fluent NHibernate (or just NHibernate)?

1

1 Answers

0
votes

It is possible using NHibernate, however it's not quite out of the box.

You would need to write your own collectionwrapper implementing IQueryable, inject it with your own CollectionFactory and delegate the query generation to the session which loaded the containing object.