- Notifications
You must be signed in to change notification settings - Fork4
A simple query language for Entity Framework Core.
License
spectresystems/spectre.query
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Spectre.Query is a library for doing simplified (safe) querying in Entity Framework Core. Perfect when you want to let end users or APIs search with a SQL-esque language without actually letting them execute any SQL directly (which you never should).
ID > 0 AND Year < 2007 AND Comment != null AND (!Seen OR Comment LIKE '%Awesome%')
This project is currently under active development and might not be ready for production.
PM> Install-Package Spectre.Query
varprovider=QueryProviderBuilder.Build(context, options=>{options.Configure<Movie>(movie=>{movie.Map("Id", e=>e.MovieId);movie.Map("Genre", e=>e.Genre.Name);movie.Map("Title", e=>e.Name);movie.Map("Year", e=>e.ReleasedAt);movie.Map("Score", e=>e.Rating);movie.Map("Seen", e=>e.Seen);});});
The createdIQueryProvider<TContext>
is thread safe andcan be cached for the duration of the application.
varmovies=provider.Query<Movie>(context,"NOT Seen AND Score > 60").ToList();
PM> Install-Package Spectre.Query.AspNetCore
Start by adding the registrations in yourStartup.cs
.
publicvoidConfigureServices(IServiceCollectionservices){services.AddQueryProvider<MovieDbContext>(options=>{options.Configure<Movie>(movie=>{movie.Map("Id", e=>e.MovieId);movie.Map("Genre", e=>e.Genre.Name);movie.Map("Title", e=>e.Name);movie.Map("Year", e=>e.ReleasedAt);movie.Map("Score", e=>e.Rating);movie.Map("Seen", e=>e.Seen);});});// ...}
publicvoidConfigure(IApplicationBuilderapp,IHostingEnvironmentenv){// This is not required, but will make sure that all// initialization is performed at start up and not at// the first time the query provider is used.app.UseQueryProvider<MovieDbContext>();// ...}
[ApiController][Route("api/movies")]publicclassMovieController:ControllerBase{privatereadonlyMovieContext_context;privatereadonlyIQueryProvider<MovieContext>_provider;publicMovieController(MovieContextcontext,IQueryProvider<MovieContext>provider){_context=context;_provider=provider;}[HttpGet]publicIActionResult<List<Movie>>Query([FromHeader]stringquery="Rating > 80 AND !Seen"){return_provider.Query<Movie>(_context,query).OrderByDescending(movie=>movie.Rating).ToList()}}
Copyright © Spectre Systems
Spectre.Query is provided as-is under the MIT license. For more information seeLICENSE.
About
A simple query language for Entity Framework Core.
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.