Created
May 14, 2011 22:42
-
-
Save geoffreysmith/972720 to your computer and use it in GitHub Desktop.
QueryObjects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// #001 Using Query Objects | |
namespace SharpArchCookbook.Web.Mvc.Controllers.Queries.Products | |
{ | |
using Domain; | |
using MvcContrib.Pagination; | |
using NHibernate.Transform; | |
using SharpArch.NHibernate; | |
using ViewModels; | |
public class ProductsListQuery : NHibernateQuery, IProductsListQuery | |
{ | |
public IPagination<ProductViewModel> GetPagedList(int index, int size) | |
{ | |
var query = Session.QueryOver<Product>().OrderBy(x => x.Name).Asc; | |
var count = query.ToRowCountQuery(); | |
var totalCount = count.FutureValue<int>(); | |
var firstResult = (index - 1) * size; | |
ProductViewModel viewModel = null; | |
ProductCategory categoryAlias = null; | |
ProductModel modelAlias = null; | |
var viewModels = | |
query.JoinAlias(x => x.Category, () => categoryAlias) | |
.JoinAlias(x => x.Model, () => modelAlias) | |
.SelectList(list => list | |
.Select(x => x.Id).WithAlias(() => viewModel.Id) | |
.Select(x => x.Name).WithAlias(() => viewModel.Name) | |
.Select(x => x.ProductNumber).WithAlias(() => viewModel.ProductNumber) | |
.Select(x => x.Color).WithAlias(() => viewModel.Color) | |
.Select(x => x.StandardCost).WithAlias(() => viewModel.StandardCost) | |
.Select(x => x.ListPrice).WithAlias(() => viewModel.ListPrice) | |
.Select(x => x.SellStartDate).WithAlias(() => viewModel.SellEndDate) | |
.Select(x => x.SellEndDate).WithAlias(() => viewModel.SellEndDate) | |
// Flattening the object graph | |
.Select(x => categoryAlias.Name).WithAlias(() => viewModel.CategoryName) | |
.Select(x => modelAlias.Name).WithAlias(() => viewModel.ModelName)) | |
.TransformUsing(Transformers.AliasToBean(typeof(ProductViewModel))) | |
.Skip(firstResult) | |
.Take(size) | |
.Future<ProductViewModel>(); | |
return new CustomPagination<ProductViewModel>(viewModels, index, size, totalCount.Value); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment