GithubHelp home page GithubHelp logo

codepb / fluentqueries Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 1.0 51 KB

A Fluent API for defining queries. Allows fluent language based linq queries, and easy definition and consumption of Query Objects.

License: MIT License

C# 100.00%
csharp dotnet dotnetcore querying fluent fluent-api

fluentqueries's Introduction

Build Status

FluentQueries

A Fluent API for defining queries. Allows fluent language based linq queries, and easy definition and consumption of Query Objects.

How to Use FluentQueries

It all starts with the Query class. to begin a new query, just do the following:

Query<Book>

Then you select what you want to query against. If it's the entity itself, then you can use Is, if it's a property you can use Has(b => b.SomeProperty) and use a lambda expression to select the property. Finally select the predicate to resolve the query. For example:

Query<Book>.Is.EqualTo(otherBook);
Query<Book>.Has(b => b.Author).NotEqualTo("Oscar Wilde");
Query<Book>.Has(b => b.Title).Containing("The");
Query<Book>.Has(b => b.Available).False();
Query<Book>.Has(b => b.Pages).GreaterThan(1000);

Obviously, a large amount of the time, we want to query more than one thing. You can chain queries together using And or Or:

Query<Book>.Is.EqualTo(otherBook).Or.Has(b => b.Author).NotEqualTo("Oscar Wilde");
Query<Book>.Has(b => b.Pages).GreaterThan(500).And.Has(b => b.Available).True();

Once you've defined your query you can compile to an Expression:

Query<Book>.Has(b.Title).EqualTo("The Picture of Dorian Gray").AsExpression();
Context.Books.Where(Query<Book>.Has(b.Title).EqualTo("The Picture of Dorian Gray").AsExpression()).ToList();

You can also check if any object satisfies a query, either by using the IsSatisfiedBy or the extension method Satisfies on an object.

var query = Query<Book>.Has(b.Title).StartingWith("The");
query.IsSatisfiedBy(aBook);
anotherBook.Satisfies(query);

The idea of FluentQueries is to help the readability of LINQ queries. One way to do this is to utilise the FluentQueries framework to create classes that represent queries:

public class AuthorHasName : Query<Book>
{
    public AuthorHasName(string firstName, string lastName)
    {
    	Define(
    		Has(b => b.Author)
    		.EqualTo($"{firstName} {lastName}")
    	);
    }
}

You now have a nice, explicit and reusable class for querying the authors name:

aBook.Satisfies(new AuthorHasName("Charles", "Dickens"));

Finally you can combine existing queries with new ones:

Query<Book>.Has(b => b.Pages).GreaterThan(1000).And.Is.Satisfying(new AuthorHasName("William", "Shakespeare"));

Or combine lambda expressions with queries:

Query<Book>.Has(b => b.Title).EndingWith("Wild").And.Has(b => b.Pages).Satisfying(p => p > 1000);

This means if there is anything not supported by FluentQueries you can still use a lambda expression combined with Queries.

fluentqueries's People

Contributors

codepb avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

bubdm

fluentqueries's Issues

Add logo

Add a logo for the nuget package

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.