OfX-EFCore 1.1.4

There is a newer version of this package available.
See the version list below for details.
dotnet add package OfX-EFCore --version 1.1.4                
NuGet\Install-Package OfX-EFCore -Version 1.1.4                
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="OfX-EFCore" Version="1.1.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add OfX-EFCore --version 1.1.4                
#r "nuget: OfX-EFCore, 1.1.4"                
#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 OfX-EFCore as a Cake Addin
#addin nuget:?package=OfX-EFCore&version=1.1.4

// Install OfX-EFCore as a Cake Tool
#tool nuget:?package=OfX-EFCore&version=1.1.4                

OfX.EntityFrameworkCore

OfX.EntityFrameworkCore is an extension package for OfX that integrates with Entity Framework Core to simplify data fetching by leveraging attribute-based data mapping. This extension streamlines data retrieval using EF Core, reducing boilerplate code and improving maintainability.

Demo Project!


Introduction

OfX.EntityFrameworkCore extends the core OfX library by providing seamless integration with Entity Framework Core. This enables developers to automatically map and retrieve data directly from a database, leveraging the power of EF Core along with attribute-based data mapping.

For example, suppose you have a UserId property in your model, and you want to fetch the corresponding UserName and Email fields from the database. By using OfX.EntityFrameworkCore, you can annotate your model with attributes, and the library will handle data fetching for you.


Installation

To install the OfX.EntityFrameworkCore package, use the following NuGet command:

dotnet add package OfX-EFCore

Or via the NuGet Package Manager:

Install-Package OfX-EFCore

How to Use

1. Register OfX.EntityFrameworkCore

Add OfX.EntityFrameworkCore to your service configuration during application startup:

builder.Services.AddOfXEntityFrameworkCore(cfg =>
{
    cfg.RegisterContractsContainsAssemblies(typeof(SomeContractAssemblyMarker).Assembly);
    cfg.RegisterHandlersContainsAssembly<SomeHandlerAssemblyMarker>();
})
.AddOfXEFCore<ServiceDbContext>()
.AddOfXHandlers<IHandlerAssemblyMarker>();

After installing the package OfX-EFCore, you can use the extension method RegisterOfXEntityFramework(), which takes two arguments: the DbContext and the handlers assembly.

2. Write a Handler Using EF Core

Implement a request handler to fetch the required data using Entity Framework Core. For example:

public sealed class UserOfXHandler(IServiceProvider serviceProvider)
    : EfQueryOfXHandler<User, GetUserOfXQuery>(serviceProvider)
{
    protected override Func<GetUserOfXQuery, Expression<Func<User, bool>>> SetFilter() =>
        q => u => q.SelectorIds.Contains(u.Id);

    protected override Expression<Func<User, OfXDataResponse>> SetHowToGetDefaultData() =>
        u => new OfXDataResponse { Id = u.Id, Value = u.Name };
}

Function Details

SetFilter

This function is used to define the filter for querying data. It takes the query (GetUserOfXQuery) as an argument and returns an Expression<Func<TModel, bool>>, where TModel is your EF Core entity.

Example:

protected override Func<GetUserOfXQuery, Expression<Func<User, bool>>> SetFilter() =>
    q => u => q.SelectorIds.Contains(u.Id);

Here, SetFilter ensures that only entities matching the provided SelectorIds in the query are retrieved.

SetHowToGetDefaultData

This function specifies how to map the retrieved entity to the default data format. It returns an Expression<Func<TModel, OfXDataResponse>>, where TModel is your EF Core entity and OfXDataResponse is the mapped data structure.

Example:

protected override Expression<Func<User, OfXDataResponse>> SetHowToGetDefaultData() =>
    u => new OfXDataResponse { Id = u.Id, Value = u.Name };

Here, SetHowToGetDefaultData maps the Id and Name of the User entity to the OfXDataResponse format.

By overriding these functions, you can customize the filtering logic and data mapping behavior to suit your application's requirements.


Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
4.0.0 25 2/4/2025
3.3.3 64 2/4/2025
3.3.2 69 2/3/2025
3.3.1 62 2/1/2025
3.3.0 65 1/22/2025
3.2.1 67 1/22/2025
3.2.0 74 1/21/2025
3.1.13 68 1/18/2025
3.1.12 66 1/16/2025
3.1.11 70 1/13/2025
3.1.10 73 1/12/2025
3.1.9 77 1/10/2025
3.1.8 53 1/8/2025
3.1.7 73 1/7/2025
3.1.6 78 1/6/2025
3.1.5 81 1/5/2025
3.1.4 98 1/4/2025
3.1.3 101 1/4/2025
3.1.2 110 1/4/2025
3.1.1 110 1/3/2025
3.1.0 74 12/31/2024
3.0.5 79 12/30/2024
3.0.4 69 12/30/2024
3.0.3 77 12/30/2024
3.0.2 90 12/30/2024
3.0.1 81 12/30/2024
3.0.0 83 12/30/2024
2.0.4 79 12/30/2024
2.0.3 76 12/30/2024
2.0.2 80 12/29/2024
2.0.1 94 12/29/2024
2.0.0 89 12/28/2024
1.1.4 92 12/27/2024
1.1.3 78 12/26/2024
1.1.2 80 12/26/2024
1.1.1 78 12/26/2024
1.1.0 84 12/25/2024
1.0.8 88 12/25/2024
1.0.7 88 12/25/2024
1.0.6 79 12/25/2024