I've started working on a project with NH 3.3.4 and mapping-by-code. I've 
been meaning to update it to 4.x but I haven't gotten to it yet. Most of 
the queries are in LINQ extensions, though there's some HQL in there also.

The project runs on an older legacy database, so some of the mappings are 
less than idea, but for the most part it works OK. There's a few patterns 
that I've found that seem awkward to me though and I've been trying to 
determine if there's a better way. If I knew the correct verbage I'm sure I 
could find it via searches, but so far I haven't stumbled upon it.

The one I've been most interested in right now is what to do with 
projections that have nested lists inside of them.

Let's say I have 0-N Menus that have 0-N Meals and I wish to transform that 
output into some DTO. 

I'll map a Meal to a Menu with

>
>             Bag(x => x.Meals,
>                 map =>
>                 {
>                     map.Fetch(CollectionFetchMode.Subselect); // Or Join
>                     map.Inverse(true);
>                     map.Cascade(Cascade.None);
>                     map.Key(k => k.Column("MENU_ID"));
>                 },
>                 rm => rm.OneToMany());


This works fine if I want to do something like

var menu = Session.Load<Menu>(someID);
> foreach(var meal in menu.Meals)
>    dosomething-with-meal;


Now if I attempt (assuming Session is a valid session) to project my menus 
like:

Session.Query<Menu>.Where(x => x.SomeCritera).Select(menu => new MenuDTO {
>   Name = menu.Name,
>   Meals = menu.Meals.Select(meal => new MealDTO { Name = meal.Name 
> }).ToList()
> }).ToList();


This doesn't work in this context. I can get the aggregate of the 
collection on a single entity, but I can't seem to project a list within a 
list. Right now what I've been doing is doing the two queries separately 
and re integrating them.

>
> menuQuery = Session.Query<Menu>.Where(x => x.SomethingCritera);
> menus = menuQuery.Select(menu => new MenuDTO{Name = menu.Name}).ToFuture();
> meals = Session.Query<Meals>.Where(meal => menuQuery.Any(menu => menu.ID = 
> meal.MenuID)).Select(meal => new MealDTO{MenuID = meal.MenuID, Name = 
> meal.Name}).ToFuture();
> foreach(var menu in menus) 
>     menu.Meals = meals.Where(meal => meal.MenuID == menu.ID).ToList();


It works OK, but it has a tendency to break down as menuQuery becomes more 
complex, especially if menu query starts paging and filtering.

So, what is the best way to go about this? I noticed there's transformers 
in QueryOver, but this project hasn't used QueryOver so far and I'm 
hesitant to just start changing that style mid-project as I would have to 
retrain all the other developers.

Thank you for any feedback or suggestions on searches I may perform,

PM


-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.

Reply via email to