EnGen.OhData.Client
1.4.0
See the version list below for details.
dotnet add package EnGen.OhData.Client --version 1.4.0
NuGet\Install-Package EnGen.OhData.Client -Version 1.4.0
<PackageReference Include="EnGen.OhData.Client" Version="1.4.0" />
<PackageVersion Include="EnGen.OhData.Client" Version="1.4.0" />
<PackageReference Include="EnGen.OhData.Client" />
paket add EnGen.OhData.Client --version 1.4.0
#r "nuget: EnGen.OhData.Client, 1.4.0"
#:package EnGen.OhData.Client@1.4.0
#addin nuget:?package=EnGen.OhData.Client&version=1.4.0
#tool nuget:?package=EnGen.OhData.Client&version=1.4.0
OhData
Convention-based OData 4.0 server and typed client for ASP.NET Core. Define a profile class, assign handler delegates, and get a spec-faithful OData API - no controllers required (see docs/spec-compliance.md for exactly what's covered). Consume it from .NET with a fluent, LINQ-native client.
Try it live — fire real $filter/$orderby/$expand queries (writes too) at a deployed OhData demo service from an interactive API reference, or hit the raw v2 service document directly:
(Free-tier hosting: the first load after a quiet spell takes a moment to wake up, and demo data is ephemeral — anything you write disappears whenever the instance recycles.)
Getting Started
Install the server package:
dotnet add package EnGen.OhData.AspNetCore
Install the client package:
dotnet add package EnGen.OhData.Client
Packages
| Package | What it does |
|---|---|
| The server framework. | |
| The typed LINQ client. | |
Optional API-documentation companions — each documents the OData query parameters ($filter, $orderby, $top, ...) in its respective OpenAPI stack with one line of registration. |
Server quick start
// 1. Define your entity
public class Product
{
public int Id { get; set; }
public string Name { get; set; } = "";
public decimal Price { get; set; }
}
// 2. Create a profile - assign only the handlers you need
public class ProductProfile : EntitySetProfile<int, Product>
{
public ProductProfile(AppDbContext db) : base(x => x.Id)
{
FilterEnabled = true;
OrderByEnabled = true;
CountEnabled = true;
SelectEnabled = true;
// IQueryable path: $filter/$orderby/$skip/$top push down to SQL via EF Core
GetQueryable = (_) => Task.FromResult(db.Products.AsQueryable());
GetById = (id, ct) => db.Products.FindAsync(id, ct).AsTask();
Post = (p, ct) => { db.Products.Add(p); return db.SaveChangesAsync(ct).ContinueWith(_ => (Product?)p, ct); };
Put = (id, p, ct) => { db.Products.Update(p); return db.SaveChangesAsync(ct).ContinueWith(_ => p, ct); };
Patch = (id, delta, ct) => { var e = db.Products.Find(id); return Task.FromResult(e is null ? null : delta.Patch(e)); };
Delete = (id, ct) => { /* remove by id */ return Task.FromResult(true); };
}
}
// 3. Register in Program.cs
builder.Services.AddOhData(o => o
.WithPrefix("/odata")
.AddProfile<ProductProfile>() // list profiles explicitly
// ...or scan assemblies for every EntitySetProfile they contain:
.AddProfilesFromAssembly(Assembly.GetExecutingAssembly()) // by assembly instance
.AddProfilesFromAssemblyOf<ProductProfile>()); // by marker type
// 4. Map endpoints after app.Build()
app.MapOhData();
This produces:
| Method | Route | Handler |
|---|---|---|
GET |
/odata |
Service document |
GET |
/odata/$metadata |
CSDL (EDM) |
GET |
/odata/Products |
GetQueryable - supports $filter, $orderby, $skip, $top, $select, $count |
GET |
/odata/Products/$count |
filtered row count |
GET |
/odata/Products({key}) |
GetById |
GET |
/odata/Products({key})/Name |
individual property (OData envelope) - rides GetById |
GET |
/odata/Products({key})/Name/$value |
raw property value (text/plain) |
PUT/PATCH |
/odata/Products({key})/Name |
set an individual property ({"value":...}) - rides Patch |
DELETE |
/odata/Products({key})/Name |
set an individual property to null - rides Patch |
POST |
/odata/Products |
Post |
PUT |
/odata/Products({key}) |
Put |
PATCH |
/odata/Products({key}) |
Patch |
DELETE |
/odata/Products({key}) |
Delete |
Only routes with a handler assigned are registered. Unassigned handlers produce no route.
OpenAPI / Swagger documentation
Each OpenAPI stack has an optional companion package that documents the OData query parameters
($filter, $orderby, $top, $skip, $select, $expand, $count, $search) on OhData
endpoints, driven by each entity set's capability flags. Install the one matching your stack and
register one line — the core package has no dependency on any OpenAPI stack:
| Package | Registration |
|---|---|
EnGen.OhData.AspNetCore.OpenApi |
builder.Services.AddOpenApi(o => o.AddOperationTransformer<OhDataOpenApiOperationTransformer>()); |
EnGen.OhData.AspNetCore.Swashbuckle |
builder.Services.AddSwaggerGen(c => c.OperationFilter<OhDataSwaggerOperationFilter>()); |
EnGen.OhData.AspNetCore.NSwag |
builder.Services.AddOpenApiDocument(s => s.OperationProcessors.Add(new OhDataNSwagOperationProcessor())); |
See docs/openapi.md, docs/nswag.md, and docs/versioning.md (Swashbuckle multi-doc setup) for details.
Beyond the basics
The rest of the surface rides other profile declarations - navigation properties (HasMany/HasOptional/HasRequired), UseETag, and BindFunction/BindAction - rather than the plain CRUD handlers above:
| Method | Route | Registered by |
|---|---|---|
GET |
/odata/Orders({key})/Lines |
HasMany/HasOptional/HasRequired with a getAll/get/batchGetAll/batchGet delegate |
GET |
/odata/Orders({key})/Lines/$count |
same, for collection navigations |
POST |
/odata/Orders({key})/Lines |
HasMany with a post delegate - creates a related entity |
GET/POST/PUT/DELETE |
/odata/Orders({key})/Lines/$ref |
HasMany/HasOptional with addRef/setRef/removeRef |
GET |
/odata/Products/{FunctionName} |
BindFunction (collection-bound) |
POST |
/odata/Products/{ActionName} |
BindAction (collection-bound) |
GET |
/odata/Products({key})/{FunctionName} |
BindEntityFunction |
POST |
/odata/Products({key})/{ActionName} |
BindEntityAction |
See docs/navigation-routing.md, docs/property-access.md, docs/deep-insert.md, and docs/bound-operations.md for the full details behind each row.
And to shrink the surface instead of growing it: Ignore(x => x.CostBasis) hides a property
from $metadata, query options, routes, and every request/response body — without touching the
CLR model. See docs/ignoring-properties.md.
Authorization
OhData rides ASP.NET Core's own authentication and authorization - there's no OhData-specific auth system. Protect a whole entity set with one call:
RequireAuthorization("AdminOnly"); // or RequireAuthorization() / RequireRoles("Admin")
…or authorize per operation with ConfigureAuthorization, whose per-category lambdas mirror AuthorizationPolicyBuilder (requirements accumulate and AND):
ConfigureAuthorization(auth => auth
.Read(r => r.AllowAnonymous()) // catalog reads are public
.Create(c => c.RequirePolicy("Editors"))
.Update(u => u.RequireRole("Editors").RequireResource()) // Editor AND owns the row
.Delete(d => d.RequireRole("Admin"))
.Invoke("Approve", i => i.RequirePolicy("Approvers")));
.RequireResource() adds instance-level (owner/tenant) checks: OhData loads the entity and evaluates ASP.NET Core resource-based authorization against it, so you write a standard AuthorizationHandler<OperationAuthorizationRequirement, TModel>. Profiles stay free of ASP.NET Core types - requirements are stored as plain policy/role/claim names. See docs/authorization.md.
Client quick start
// Setup - inject via IHttpClientFactory or create directly
var client = new OhDataClient("https://api.example.com/odata");
// Query with LINQ-style filter, ordering, and pagination
var page = await client.For<Product>()
.Filter(x => x.Price > 10 && x.Name.StartsWith("W"))
.OrderBy(x => x.Name)
.Top(20)
.Skip(0)
.ToPageAsync(); // returns ODataPage<Product> with Items, TotalCount, NextLink
// Traverse all pages automatically via IAsyncEnumerable - follows @odata.nextLink
await foreach (Product p in client.For<Product>().Filter(x => x.Price > 0).ToAsyncEnumerable())
{
Console.WriteLine(p.Name);
}
// Get a single entity - returns null on 404
Product? p = await client.For<Product>().Key(42).GetAsync();
// Mutate
Product created = (await client.For<Product>().InsertAsync(new Product { Name = "Cog", Price = 4.99m }))!;
var updated = await client.For<Product>().Key(created.Id)
.PutAsync(new Product { Id = created.Id, Name = created.Name, Price = 5.49m });
await client.For<Product>().Key(42).PatchAsync(new { Price = 3.99m });
await client.For<Product>().Key(42).DeleteAsync();
With IHttpClientFactory:
// Registration - the typed-client overload configures the HttpClient and registers
// OhDataClient to be constructed with it (OhDataClient has an HttpClient constructor).
builder.Services.AddHttpClient<OhDataClient>(c =>
c.BaseAddress = new Uri("https://api.example.com/odata/"));
// Injection
public class MyService(OhDataClient client) { ... }
Performance
OhData's minimal-API pipeline was benchmarked head-to-head against Microsoft.AspNetCore.OData's
ODataController + [EnableQuery] pipeline over the full HTTP round-trip (routing → OData
query-option processing → handler → serialization), same dataset, same requests, correctness
verified before every run. OhData won all 11 scenarios:
| Scenario | OhData | Microsoft.AspNetCore.OData | Speedup | Alloc ratio |
|---|---|---|---|---|
| GetAll page (100) | 763 µs | 2,821 µs | 3.7× | 6.3× |
$filter |
1,778 µs | 3,393 µs | 1.9× | 6.0× |
$orderby |
968 µs | 2,949 µs | 3.0× | 5.4× |
$select |
878 µs | 1,858 µs | 2.1× | 1.3× |
$top + $skip |
1,262 µs | 2,061 µs | 1.6× | 4.6× |
$count=true (+$filter) |
2,831 µs | 4,740 µs | 1.7× | 5.4× |
| GetById | 37 µs | 112 µs | 3.0× | 3.0× |
| POST | 51 µs | 286 µs | 5.6× | 7.7× |
| PUT | 57 µs | 281 µs | 4.9× | 7.7× |
| PATCH | 53 µs | 325 µs | 6.2× | 7.1× |
| DELETE | 16 µs | 24 µs | 1.5× | 1.3× |
The biggest gaps are on writes (POST/PUT/PATCH, ~5-6× — MS OData's OData-JSON formatters and EDM-bound serialization dominate there) and full-page reads (~3-3.7×). "Alloc ratio" is how many times more memory the MS OData pipeline allocates per request. BenchmarkDotNet over in-process TestServer hosts, identical 1,000-entity dataset and byte-identical requests on both sides, with a correctness gate run before measurement; see src/OhData.Server.Benchmarks/docs/server-comparison-report.md for the full methodology, raw output, and known asymmetries between the two pipelines.
Battle-testing
1,100 automated tests protect the framework end to end: 778 in OhData.AspNetCore.Tests (server
routing, query options, navigation, ETags, authorization, malformed-payload hardening), 267 in
OhData.Client.Tests, 30 in OhData.MicrosoftODataClient.Tests (compatibility against the
official Microsoft.OData.Client), and 25 across the OpenAPI-integration suites
(OhData.AspNetCore.OpenApi.Tests, OhData.AspNetCore.NSwag.Tests). Run them yourself with the
commands in CLAUDE.md.
Documentation
| Topic | Guide |
|---|---|
Query options ($filter, $orderby, $select, $expand, $count, $search) |
docs/query-options.md |
Navigation property routing, $ref, and POST-to-navigation |
docs/navigation-routing.md |
Individual property access, reads/writes, and /$value |
docs/property-access.md |
| Deep insert (nested related entities in POST) | docs/deep-insert.md |
| Bound functions and actions | docs/bound-operations.md |
| ETags and optimistic concurrency | docs/etags.md |
| Authorization | docs/authorization.md |
| API versioning | docs/versioning.md |
OpenAPI (built-in AddOpenApi) integration |
docs/openapi.md |
| NSwag integration | docs/nswag.md |
| Client guide | docs/client.md |
| OData 4.0 spec compliance | docs/spec-compliance.md |
| Framework architecture | docs/architecture.md |
| Migrating from Microsoft.AspNetCore.OData | docs/migrating-from-microsoft-odata.md |
| Deployment (Dockerfile, Render) | docs/deployment.md |
| Releasing to NuGet | docs/releasing.md |
| Product | Versions 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 was computed. 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. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- No dependencies.
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.