Raiqub.Expressions is a library that provides abstractions for creating specifications and query strategies using LINQ expressions. It also supports querying and writing to databases using various providers.
🏃 Quickstart | 📖 Documentation | 🔄 Migration
- Easily define and compose specifications to encapsulate business rules
- Create custom query strategies for flexible and efficient data retrieval
- Simplify database operations by using consistent abstractions
- Seamlessly integrate with Entity Framework Core for database interactions
- Utilize Marten providers for a NoSQL document database experience
- Built with .NET Standard 2.0, 2.1, and .NET 6.0
- Raiqub.Expressions: provides abstractions for creating specifications
- Raiqub.Expressions.Reading: provides abstractions for creating query strategies and query sessions. Defines the
IDbQuerySession
andIDbQuerySessionFactory
interfaces for querying from the database - Raiqub.Expressions.Writing: provides abstractions for creating write sessions and performing write operations. Defines the
IDbSession
andIDbSessionFactory
interfaces for writing to the database - Raiqub.Expressions.EntityFrameworkCore: implements sessions and factories using Entity Framework Core. Ideal for integrating with Entity Framework Core for database access
- Raiqub.Expressions.Marten: implements sessions and factories using Marten library. Perfect for leveraging Marten's NoSQL document database capabilities
This README aims to give a quick overview of some Raiqub Expressions features. For deeper detail of available features, be sure also to check out Documentation Page.
Before you begin, you'll need the following:
- .NET Standard 2.0 or 2.1, or .NET Core 6.0 installed on your machine
- An IDE such as Visual Studio, Visual Studio Code, or JetBrains Rider
- If you plan to use the reading package, have a database available for querying. If you intend to use the writing package, ensure you have a writable database to perform write operations
To use Raiqub.Expressions in your project, follow these steps:
-
Install the required NuGet package(s) for the database provider you'll be using, such as `Microsoft.EntityFrameworkCore.SqlServer`
-
Install the `Raiqub.Expressions.EntityFrameworkCore` NuGet package
-
Register your DbContext by using `AddDbContextFactory` extension method
services.AddDbContextFactory<YourDbContext>();
-
Register the session and session factories using the following extension method:
services.AddEntityFrameworkExpressions() .AddSingleContext<YourDbContext>();
-
Install the `Marten` NuGet package
-
Install the `Raiqub.Expressions.Marten` NuGet package
-
Register the session and session factories using the following extension method:
services.AddMartenExpressions() .AddSingleContext();
Inject the appropriate session interface (IDbQuerySession
for read sessions, IDbSession
for read and write sessions) into your services, and use it read and write from/to database.
public class YourService
{
private readonly IDbSession _dbSession;
public YourService(IDbSession dbSession)
{
_dbSession = dbSession;
}
// ...
}
You can also create specifications and query strategies. Here's an example of how to create a simple specification:
public class CustomerIsActive : Specification<Customer>
{
public override Expression<Func<Customer, bool>> ToExpression()
{
return customer => customer.IsActive;
}
}
And here's an example of how to use the specification:
// Assuming 'session' is of type IDbSession or IDbQuerySession and has been injected
var query = session.Query(new CustomerIsActive());
var customers = await query.ToListAsync();
If something is not working for you or if you think that the source file should change, feel free to create an issue or Pull Request. I will be happy to discuss and potentially integrate your ideas!
This library is licensed under the MIT License.