Database Session
The database session interfaces abstracts away database operations and integrates with specifications and query strategies.
The `Raiqub.Expressions.Reading` package provides abstractions for querying data and `Raiqub.Expressions.Writing` package provides abstractions for saving data.
You should add the Raiqub.Expressions.Reading library on projects that only query data (read) and the Raiqub.Expressions.Writing library on projects that query and save data (read and write).
dotnet add package Raiqub.Expressions.Writing
PM> Install-Package Raiqub.Expressions.Writing
paket add nuget Raiqub.Expressions.Writing
You need to register a implementation for database sessions, and the Raiqub Expressions provides implementation for Entity Framework Core and Marten. If you need to use another ORM library, you will need to implement your own database session factory and database session implementing `IDbSessionFactory` and `IDbSession` interfaces.
services.AddEntityFrameworkExpressions()
.AddSingleContext<YourDbContext>();
services.AddMartenExpressions()
.AddSingleContext();
Injecting Database Session
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;
}
// ...
}
public class YourService(IDbSession dbSession)
{
// ...
}
Creating Query Sessions and Querying Data
To create a query session and query data using a query strategy, follow these steps:
- Inject an instance of
IDbQuerySessionFactory
into your service or controller. - Use the
Create()
method of theIDbQuerySessionFactory
interface to create a new query session. - Call the
Query()
method on the query session, passing in your query strategy or specification instance. - Call one of the methods on the resulting
IDbQuery<T>
interface to execute the query and retrieve the results.
await using (var session = querySessionFactory.Create())
{
IDbQuery<Customer> query = session.Query(new CustomerIsActive());
IReadOnlyList<Customer> customers = await query.ToListAsync();
}
Creating Write Sessions and Writing Data
To create a write session and write data to the database, follow these steps:
- Inject an instance of
IDbSessionFactory
into your service or controller. - Use the
Create()
method of theIDbSessionFactory
interface to create a new write session. - Call the appropriate methods on the write session to perform insert, update, or delete operations on your entities.
- 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();
}