Raiqub.Expressions.Writing 1.0.0-pre20230410-0253

Prefix Reserved
This is a prerelease version of Raiqub.Expressions.Writing.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Raiqub.Expressions.Writing --version 1.0.0-pre20230410-0253                
NuGet\Install-Package Raiqub.Expressions.Writing -Version 1.0.0-pre20230410-0253                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Raiqub.Expressions.Writing" Version="1.0.0-pre20230410-0253" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Raiqub.Expressions.Writing --version 1.0.0-pre20230410-0253                
#r "nuget: Raiqub.Expressions.Writing, 1.0.0-pre20230410-0253"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Raiqub.Expressions.Writing as a Cake Addin
#addin nuget:?package=Raiqub.Expressions.Writing&version=1.0.0-pre20230410-0253&prerelease

// Install Raiqub.Expressions.Writing as a Cake Tool
#tool nuget:?package=Raiqub.Expressions.Writing&version=1.0.0-pre20230410-0253&prerelease                

Expressions

GitHub license Nuget Nuget

Raiqub.Expressions is a library that provides abstractions for creating specifications and query models using LINQ expressions. It also supports querying and writing to databases using various providers.

🏃 Quickstart   |   📗 Guide   |   📦 NuGet

<hr />

Features

  • Abstractions for creating specifications and query models
  • Abstractions for querying and writing to databases
  • Supports Entity Framework Core and Marten providers
  • Built with .NET Standard 2.0, 2.1, and .NET Core 6.0

NuGet Packages

  • Raiqub.Expressions: abstractions for creating specifications and query models (defines Specification and QueryModel base classes)
  • Raiqub.Expressions.Reading: abstractions for creating query sessions and querying from database (defines IQuerySessionFactory and IQuerySession interfaces)
  • Raiqub.Expressions.Writing: abstractions for creating write sessions and writing to database (defines ISessionFactory and ISession interfaces)
  • Raiqub.Expressions.EntityFrameworkCore: implementation of sessions and factories using Entity Framework Core
  • Raiqub.Expressions.Marten: implementation of sessions and factories using Marten library

Prerequisites

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
  • A database to query against (if using the reading package) or write to (if using the writing package)

Quickstart

To use the library, you can install the desired NuGet package(s) in your project and start creating your specifications and query models. Here's an example of how to create a simple specification:

public class CustomerIsActiveSpecification : 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 Entity Framework Core:

await using (var session = querySessionFactory.Create())
{
    var query = session.Query(new CustomerIsActiveSpecification());
    var customers = await query.ToListAsync();
}

Guide

To use Raiqub.Expressions in your project, follow these steps:

  1. Install the required NuGet package(s) for the database provider you'll be using, such as `Microsoft.EntityFrameworkCore.SqlServer` for Entity Framework Core or `Marten` for Marten.

  2. Install the `Raiqub.Expressions.EntityFrameworkCore` package if using Entity Framework or the `Raiqub.Expressions.Marten` package if using Marten.

  3. Register the session factories using the appropriate extension method(s) for your database provider:

    For Entity Framework Core:

    services.AddEntityFrameworkSessionFactory<YourDbContext>();
    

    For Marten:

    services.AddMartenSessionFactory();
    
  4. Inject the appropriate session factory interface (IQuerySessionFactory for read sessions, ISessionFactory for write sessions) into your services, and use it to create sessions as needed.

Creating Query Models and Specifications

The `Raiqub.Expressions` package provides abstractions for creating specifications and query models. You can create a new query model by creating a new class that derives from `QueryModel<TSource, TResult>`. Similarly, you can create a new specification by creating a new class that derives from `Specification<T>`.

Here's an example of a query model that filters a list of entities based on a set of conditions:

public class MyQueryModel : QueryModel<MyEntity, MyResult>
{
    protected override IEnumerable<Specification<MyEntity>> GetPreconditions()
    {
        yield return new MyEntityIsEnabledSpecification();
    }

    protected override IQueryable<MyResult> ExecuteCore(IQueryable<MyEntity> source)
    {
        return source.OrderBy(e => e.Name).Select(e => new MyResult { Id = e.Id, Name = e.Name });
    }
}

And here's an example of a specification that checks if an entity is enabled:

public class MyEntityIsEnabledSpecification : Specification<MyEntity>
{
    public override Expression<Func<MyEntity, bool>> ToExpression()
    {
        return entity => entity.IsEnabled;
    }
}

Creating Query Sessions and Querying Data

To create a query session and query data using a query model, follow these steps:

  1. Inject an instance of `IQuerySessionFactory` into your service or controller.
  2. Use the `Create()` method of the `IQuerySessionFactory` interface to create a new query session.
  3. Call the `Query()` method on the query session, passing in your query model or specification instance.
  4. Call one of the methods on the resulting `IQuery<T>` interface to execute the query and retrieve the results.
await using (var session = querySessionFactory.Create())
{
    var query = session.Query(new CustomerIsActiveSpecification());
    var customers = await query.ToListAsync();
}

Creating Write Sessions and Writing Data

To create a write session and write data to the database, follow these steps:

  1. Inject an instance of `ISessionFactory` into your service or controller.
  2. Use the `Create()` method of the `ISessionFactory` interface to create a new write session.
  3. Call the appropriate methods on the write session to perform insert, update, or delete operations on your entities.
  4. Call the `SaveChangesAsync()` method on the write session to persist your changes to the database.
await using (var session = sessionFactory.Create())
{
    var blog = new Blog { Url = "https://example.com" };
    session.Add(blog);
    await session.SaveChangesAsync();
}

Supported Databases

Currently, Raiqub.Expressions supports the following databases:

  • Entity Framework Core
  • Marten

If you need to use another database, you will need to implement your own `ISessionFactory` and `ISession` interfaces.

Contributing

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!

License

This library is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Raiqub.Expressions.Writing:

Package Downloads
Raiqub.Expressions.EntityFrameworkCore

Provides implementation of sessions and factories using Entity Framework Core.

Raiqub.Expressions.Marten

Provides implementation of sessions and factories using Marten library.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.3.208 113 12/8/2024
2.3.206 103 10/17/2024
2.3.176 199 8/31/2024
2.3.123 190 6/9/2024
2.3.117 202 6/4/2024
2.3.99 175 5/19/2024
2.3.69 208 4/24/2024
2.3.53 231 4/6/2024
2.2.39 273 1/7/2024
2.2.5 247 11/11/2023
2.1.50 195 11/2/2023
2.1.8 250 10/6/2023
2.1.7 207 10/1/2023
2.0.4 202 9/24/2023
1.1.3 192 9/24/2023
1.0.34 190 9/7/2023
1.0.19-gc5aee6dbbe 190 7/30/2023
1.0.16-gc38afc643f 157 7/29/2023
1.0.3-g5a9a3695a8 224 4/20/2023
1.0.0-pre20230410-0253 171 4/10/2023