No Problem. Also, I'm guessing from the snippet of code you showed that you're going to map your projections to properties of the ViewModel. You could map these in a type-safe fashion using something like this:

public class CarViewModel
{
   public string CarMake { get; set; }
   public string CarModel { get; set; }
}


public class FeaturedCarFinder : QueryOver<Car, Car>
...
   private void BuildProjections()
   {
       CarViewModel viewModel = null;
       SelectList(l => l
           .Select(c => c.Make).WithAlias(() => viewModel.CarMake)
           .Select(c => c.Model).WithAlias(() => viewModel.CarModel)
           );
   }


Don't know if that's of any use to you.


-----Original Message----- From: Colin Ramsay
Sent: Wednesday, January 12, 2011 5:57 PM
To: nhusers
Subject: [nhusers] Re: Reusable QueryOver

I tried this approach but I used QueryOver<T> rather than
QueryOver<T1, T2> as my base class and it didn't work. Using
QueryOver<T1, T2> works as expected - thank you!

On Jan 12, 4:55 pm, "Richard Brown \(gmail\)"
<[email protected]> wrote:
Hi Colin,

I think it would look something like this:

public class CarViewModelMessage
{
    public bool IsFeatured { get; set; }

}

public class FeaturedCarFinder : QueryOver<Car, Car>
{
    public FeaturedCarFinder(int maxResults)
    {
        Where(c => c.IsFeatured);
        Take(maxResults);
        BuildProjections();

TransformUsing(Transformers.AliasToBean(typeof(CarViewModelMessage)));
    }

    private void BuildProjections()
    {
        SelectList(l =>
            l.Select(c => c.IsFeatured)
            //...
            );

    }

}

And you could call it using something like:

var carsViewModel =
    new FeaturedCarFinder(25)
        .GetExecutableQueryOver(session)
        .List<CarViewModelMessage>();

Let me know how you get on.

Cheers,
    Richard







-----Original Message-----
From: Colin Ramsay
Sent: Wednesday, January 12, 2011 3:48 PM
To: nhusers
Subject: [nhusers] Reusable QueryOver

To keep my queries self-contained and potentially reusable, I tended
to do this in NH2:

public class FeaturedCarFinder : DetachedCriteria
{
    public FeaturedCarFinder(int maxResults) : base(typeof(Car))
    {
        Add(Restrictions.Eq("IsFeatured", true));
        SetMaxResults(maxResults);
        SetProjection(BuildProjections());
        SetResultTransformer(typeof(CarViewModelMessage));
    }
}

I'd like to use QueryOver now that I've moved to NH3, but I'm not sure
how to do the above using QueryOver?

--
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 athttp://groups.google.com/group/nhusers?hl=en.

--
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.

--
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