Example #10: Using projections to return only the necessary columns from the database

1. Here we can send our view to the client, no need to pass any data yet.
public ActionResult CustomAggregatesFunctionsClientSide()
{
    return View();
}
2.1. Generic linq version:
[ApplyTransaction, GridAction(EnableCustomBinding = true)]
public ActionResult UsingProjectionsNData(GridCommand command)
{
    var query = NHibernateHelper.CurrentSession.Query<Product>();
    // with projections there's no need to fetch anything
    //.Fetch(x => x.Category)
    var queryToCount = NHibernateHelper.CurrentSession.Query<Product>();

    var gridQueryProvider = new GridQueryProvider(query, queryToCount);

    var gridHelper = new GridCustomBindingHelper<Product, ProductModel>(command, gridQueryProvider)
        .UseProjections();

    var gridModel = gridHelper.BuildGridModel();

    return View(gridModel);
}
2.2. NHibernate QueryOver API version:
[ApplyTransaction, GridAction(EnableCustomBinding = true)]
public ActionResult UsingProjectionsNHibernateData(GridCommand command)
{
    var query = NHibernateHelper.CurrentSession.QueryOver<Product>();
    // with projections there's no need to fetch anything
    //.Fetch(x => x.Category).Eager;

    var gridHelper = new NHibernateGridCustomBindingHelper<Product, ProductModel>(command, query)
        .UseProjections();

    var gridModel = gridHelper.BuildGridModel();

    return View(gridModel);
}
3. Finaly, the Grid configuration
@(Html.Telerik().Grid<ProductModel>().Name("Grid")
        .Columns(columns =>
        {
            columns.Bound(d => d.ProductID).Width(80);
            columns.Bound(d => d.ProductName);
            columns.Bound(d => d.QuantityPerUnit);
            columns.Bound(d => d.Category.CategoryName).Title("Category");
        })
        .EnableCustomBinding(true).Sortable().Filterable().Groupable().Pageable(x => x.PageSize(15))
        .DataBinding(dataBinding => dataBinding.Ajax().Select<ExamplesController>(ctr => ctr.UsingProjectionsNData(null))))

Last edited Mar 5, 2012 at 11:10 PM by Luis_Fernando, version 3

Comments

No comments yet.