Skip to content

Getting Started

The Raiqub Expressions was split into 5 packages to better integrate with layered projects:

Entity Framework Core

For projects going to use EF Core you need to follow these steps:

  1. Add the required NuGet package(s) for the database provider you'll be using, such as Microsoft.EntityFrameworkCore.SqlServer:
shell
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
powershell
PM> Install-Package Microsoft.EntityFrameworkCore.SqlServer
shell
paket add nuget Microsoft.EntityFrameworkCore.SqlServer
  1. Add the Raiqub.Expressions.EntityFrameworkCore library from NuGet:
shell
dotnet add package Raiqub.Expressions.EntityFrameworkCore
powershell
PM> Install-Package Raiqub.Expressions.EntityFrameworkCore
shell
paket add nuget Raiqub.Expressions.EntityFrameworkCore
  1. Register your DbContext by using AddDbContextFactory extension method (more details on EF Core documentation):
csharp
services.AddDbContextFactory<YourDbContext>();
  1. Register the session and session factories using the following extension method:
csharp
services.AddEntityFrameworkExpressions()
    .AddSingleContext<YourDbContext>();

Marten

For projects going to use Marten you need to follow these steps:

  1. Add the Raiqub.Expressions.Marten library from NuGet:
shell
dotnet add package Raiqub.Expressions.Marten
powershell
PM> Install-Package Raiqub.Expressions.Marten
shell
paket add nuget Raiqub.Expressions.Marten
  1. Register the session and session factories using the following extension method:
csharp
services.AddMartenExpressions()
    .AddSingleContext();

Using

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.

csharp
public class YourService
{
    private readonly IDbSession _dbSession;

    public YourService(IDbSession dbSession)
    {
        _dbSession = dbSession;
    }

    // ...
}
csharp
public class YourService(IDbSession dbSession)
{
    // ...
}

You can also create specifications and query strategies. Here's an example of how to create a simple specification:

csharp
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 with database session:

csharp
// Assuming 'session' is of type IDbSession or IDbQuerySession and has been injected
var query = session.Query(new CustomerIsActive());
var customers = await query.ToListAsync();

Released under the MIT License.