Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use ToDataSourceResult from KendoUI in Onion Architecture withouth exposing IQueryable

I am designing application with Onion architecture and I would like to use grid from Kendo UI with server side filtering sorting and paging. I would like to avoid manually parsing DataSourceRequest into dynamic LinQ, but use ToDataSourceResult instead.

I came to the working solution, where my controller contains following:

    private ISalesService salesService;

    public HomeController(ISalesService salesService)
    {
        this.salesService = salesService;
    }

    public JsonResult Post([DataSourceRequest] DataSourceRequest request)
    {
        var sales = salesService.GetQueryableSales();

        return Json(sales , JsonRequestBehavior.AllowGet);

    } 

ISalesService is maped to implementation where I use repository, with following code:

    public IQueryable<SalesOrderHeader> GetQueryableSales()
    {
        if (context == null) 
            context = new AdventureWorks2012Context();

        return context.SalesOrderHeaders;
    }

This is just a skeleton of application, so there is no logic in service nor repository. This way I can avoid manually parsing DataSourceRequest, but at a cost of exposing IQueryable to MVC layer. I would like to keep IQueryable inside repository and expose IEnumerable if possible, or maybe DataSourceResult from Kendo UI library.

I tried to change my repository call as following:

    public DataSourceResult GetSales(DataSourceRequest request)
    {
        using (var c = new AdventureWorks2012Context())
        {
            var headersQuery = c.SalesOrderHeaders;

            var result = headersQuery.ToDataSourceResult(request);

            return result;
        }
    }

This way I could keep IQueryabl inside repository and still use automatic DataSourceRequest transformation into dynamic LinQ provided by Kendo UI. But this would required to reference Kendo.Mvc and System.Web.Mvc from my data layer, which does not seem as a good choice either.

So my question is, whether is it possible to automatically convert DataSourceRequest to LinQ, without compromising application layering? Am I missing something in Kendo UI architecture? Or I have to choose either to parse DataSourceRequest manually or compromise my architecture to some point?

like image 418
afx Avatar asked Apr 06 '15 17:04

afx


1 Answers

The repository method with no reference to Kendo:

public T GetSales<T>(Func<IQueryable<SalesOrderHeader>, T> processQueryable)
{
    using (var c = new AdventureWorks2012Context())
    {
        return processQueryable(c.SalesOrderHeaders);
    }
}

To use it you need a function to process the iqueryable as parameter, in this case it returns a DataSourceResult:

public JsonResult Post([DataSourceRequest] DataSourceRequest request)
{
    DataSourceResult sales = salesService.GetSales<DataSourceResult>(headersQuery=>headersQuery.ToDataSourceResult(request));

    return Json(sales , JsonRequestBehavior.AllowGet);
}
like image 73
Hedi Avatar answered Nov 01 '22 20:11

Hedi