it sounds like you need:
1. a collection of Mixes
2. a page at a time
3. 20 mixes per collection
4. whether the mix is part of the current users favorites
5. whether the user has already given the mix a thumbs up or thumbs
down.

so you would want
1 query to get the user
1 query to get a page of Mixes along with if the user has rated the
mix and if the user has added the mix

step 1. lazy loading
var account = session.Get<Account>(current user id);
var mixes = session.CreateQuery("from Mix m").SetMaxResults
(20).SetFirstResult(pageIndex).List<Mix>();

step 2. depends on how your entities are mapped. one option is to use
Filters. I think criteria based on the current user is good candidate
for filtering. the filters are configured in the mappings. Ayende has
some great content on filtering
http://ayende.com/Blog/archive/2009/05/04/nhibernate-filters.aspx
http://ayende.com/Blog/archive/2006/12/26/LocalizingNHibernateContextualParameters.aspx
we will still be using lazy loading, but those results will be limited
based on the filters.

the code may look something like this.
session.EnableFilter("CurrentUserFilter").SetParameter("user", current
user id);
var account = session.Get<Account>(current user id);
var mixes = session.CreateCriteria<Mix>().SetMaxResults
(20).SetFirstResult(pageIndex).List<Mix>();

step 3. use the filters in conjunction with sub selects
session.EnableFilter("CurrentUserFilter").SetParameter("user",
account);
var account = session.Get<Account>(current user id);
var mixes = session.CreateCriteria<Mix>()
            .SetFetchMode("Favorites", FechMode.Eager)
            .SetFetchMode("Rated", FechMode.Eager)
            .SetMaxResults(20)
            .SetFirstResult(pageIndex)
            .List<Mix>();

step 4. use future queries to batch the statements
session.EnableFilter("CurrentUserFilter").SetParameter("user",
account);
var account = session.CreateCriteria<Account>().Add(Restrictions.IdEq
(current user id)).FutureValue<Account>();
var mixes = session.CreateCriteria<Mix>()
            .SetFetchMode("Favorites", FechMode.Eager)
            .SetFetchMode("Rated", FechMode.Eager)
            .SetMaxResults(20)
            .SetFirstResult(pageIndex)
            .Future<Mix>();
var  realAccount = account.Value;

now once you have this you can render a list of Mixes along with "Add
to Favorites", if the user has not added it and the ability to Rate
the Mix if the user has not already done so.  the actions of Adding to
Favorites and Rating are separate commands executed in separate
sessions/requests. but they might look like this

Add to Favorites:
var account = session.Get<Account>(current user id);
var mix = session.Load<Mix>(mix id);
account.AddToFavorites(mix);

Rate a Mix
var account = session.Get<Account>(current user id);
var mix = session.Load<Mix>(mix id);
account.Likes(mix); // or account.Dislikes(mix);


On Nov 25, 4:09 pm, Paul <[email protected]> wrote:
> Hi,
>
> I'm still learning here and having some difficulty creating the most
> efficient way of returning all needed data for my view.
>
> I have a list of "DJ Mixes" that i want to return in pages of 20, but
> when displaying these "DJ Mixes" on my site each mix will also have
> some additional buttons displayed called "Add To Favourites", "Give
> Thumbs Up" and "Follow User" (or the remove from favs etc if already
> exists).
>
> I need to return all this information for each mix returned for the
> currently logged in user...how on earth would i go about doing this?
>
> Here is a simplified version of my classes:
>
> Accounts
>  - AccountId
>
> Mixes
>  - MixId
>  - AccountId
>
> Account_Mix_Favourites
>  - MixId  (the mix added to accounts favs)
>  - AccountId  (the account who added the mix to favs)
>
> Account_Mix_Thumbs
>  - MixId (the mix given thumbs up)
>  - AccountId (the account who gave the thumbs up)
>
> Account_Followers
>  - AccountId
>  - FollowingAccountId (which account is this user following)
>
> I could just lazy load each additional bit of information with lazy-
> loading when displaying each mix but this doesn't seem very efficient!
>
> This is something i have been trying to work out for weeks with no
> luck, i have tried playing around with projections, linq to nhibernate
> etc but i just don't quite understand what i am doing!
>
> Thanks very much guys!
>
> Paul

--

You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.


Reply via email to