Generic Queryable Repository


The enclosed solution represents a queryable reponsitory that can be integrated with any database back-end. 

The initial use for this project was in an OData, WebAPI and Entity Framework Project and Kendo UI - review here: OData Using C#, MVC, WebAPI, Entity Framework, Dependency Injection (DI)/Inversion of Control (IoC) and Kendo UI.

A queryable repository was required in order to pass along OData query options along through with a unit of work as follows:

 

        /// <summary>
        /// This is the primary GET method for OData Requests to ~/api/Users
        /// 
        /// Server-driven Paging:  http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options#server-paging
        /// To limit the # of records returned, use the PageSize attribute.  The response will contain a link to the next set of data.
        /// 
        /// </summary>
        /// <returns></returns>
        //[Queryable(AllowedQueryOptions = AllowedQueryOptions.All)]
        public IEnumerable<USER> Get(ODataQueryOptions<USER> options)
        {
            var unitOfWork = new ATMS.Repository.UnitOfWork(_dbContext);

            var users = options.ApplyTo(unitOfWork.Repository<USER>().Queryable
                                                  .Include(u => u.USERGROUPS)
                                                  .OrderBy(order => order.USERNAME))
                                                  .Cast<USER>().ToList();

            unitOfWork.Save();      // includes Dispose()

            return users;
        }

 

In using the options.ApplyTo(), the query string parameters used for filtering are then passed along through to Entity Framework to deliver an optimized data set, rather than an inflated payload then applying filtering later.

If using this with Entity Framework, some code refactoring must be done to your EF project.  Reference the link above for details.

Last edited Aug 13, 2013 at 3:38 PM by elhaix, version 7