NHibernate 3.0 QueryOver Syntax is the Bee's Knees

Back to Listing

NHibernate 3.0 QueryOver Syntax is the Bee's Knees


16 Aug, 2010


I’ll let a few queries from my current project say all that needs to be said.

public IEnumerable GetRolledUpEventsFor(DateTime tradeDate, IEnumerable tickers)  
{
    return worker.On(connection, session =>
                                     {
                                         OrderEventRollup rollup = null;
                                         return session.QueryOver()
                                             .Where(x => x.TradeDate == tradeDate)
                                             .WhereRestrictionOn(x => x.Ticker).IsIn(tickers.ToArray())
                                             .Select(
                                                 list =>
                                                 list.SelectGroup(x => x.Ticker).WithAlias(() => rollup.Ticker)
                                                     .SelectSum(x => x.Shares).WithAlias(() => rollup.SharesTotal))
                                             .TransformUsing(Transformers.AliasToBean())
                                             .List();
                                     });
}
public IEnumerable GetBrokersByCode(IEnumerable brokerCodes)  
{
    if (brokerCodes.Count() == 0) return Enumerable.Empty();
    return worker.On(connection, session =>
                                 session.Query()
                                     .Where(x => brokerCodes.Contains(x.Code))
                                     .ToList()
        );
}
public IEnumerable GetByTradeDateAndTicker(DateTime tradeDate, string ticker)  
{
    return worker.On(connection, session =>
                                     {
                                         CSMSecurity security = null;
                                         return session.QueryOver()
                                             .Fetch(x => x.CSMSecurity).Eager
                                             .Fetch(x => x.TSOrderSecuritySpec).Eager
                                             .Fetch(x => x.CSMSecurity.CSMSecurityType).Eager
                                             .JoinAlias(x => x.CSMSecurity,() => security)
                                             .Where(x => x.TradeDate == tradeDate)
                                             .Where(() => security.Ticker == ticker)
                                             .List();
                                     });
}

So hawt indeed.

Share this story

Bobby Johnson

About Author

I am a passionate engineer with an interest in shipping quality software, building strong collaborative teams and continuous improvement of my skills, team and the product.

comments powered by Disqus
Back to top