What is the best approach to load/filter/order a Kendo grid with the following classes:
Domain:
public class Car
{
public virtual in
One good way to solve it if you use Telerik Data Access or any other IQueryable enabled interface/ORM over your data, is to create views directly in your database RDBMS that map one-to-one (with automapper) to your viewmodel.
Create the viewmodel you wish to use
public class MyViewModelVM
{
public int Id { get; set; }
public string MyFlattenedProperty { get; set; }
}
Create a view in your SQL Server (or whatever RDBMS you're working with) with columns exactly matching the viewmodel property names, and of course build your view to query the correct tables. Make sure you include this view in your ORM classes
CREATE VIEW MyDatabaseView
AS
SELECT
t1.T1ID as Id,
t2.T2SomeColumn as MyFlattenedProperty
FROM MyTable1 t1
INNER JOIN MyTable2 t2 on t2.ForeignKeyToT1 = t1.PrimaryKey
Configure AutoMapper to map your ORM view class to your viewmodel
Mapper.CreateMap();
In your Kendo grid Read action, use the view to build your query, and project the ToDataSourceQueryResult using Automapper
public ActionResult Read([DataSourceRequest]DataSourceRequest request)
{
if (ModelState.IsValid)
{
var dbViewQuery = context.MyDatabaseView;
var result = dbViewQuery.ToDataSourceResult(request, r => Mapper.Map(r));
return Json(result);
}
return Json(new List().ToDataSourceResult(request));
}
It's a bit of overhead but it will help you in achieve performance on two levels when working with large datasets: