Round #854
11 May 2026, 09:47· csharp· Meta-Llama-3.3-70B-Instruct· r15-firing-v3
- Complexity
- band 1
- ZP / UO / OBS
- 4 / 6 / 3
- Journey weight
- 778
- Lightshift ΔL
- +0.00
Prompt
Implement a generic repository for read-only access to entities by id using a DbContext. Include AsNoTracking and a CancellationToken parameter.
Zero-point index (what the engine surfaced)
ZPI-ZPI-0011 | 2026-05-11T09:47:37Z
Implement a generic repository for read-only access to entities by id using a DbContext. Include AsNoTracking and a CancellationToken parameter.
ZERO POINT — 4 entries (structural invariants)
● hsl(136.10, 100%, 50%) DbContext [contract]
● hsl(282.77, 100%, 35%) DbSet<T> [contract]
● hsl(29.31, 100%, 35%) IEntityTypeConfiguration<T> [contract]
● hsl(144.66, 100%, 15%) IDbContextFactory<TContext> [contract]
UNOBSERVED — 6 entries (implied truths)
○ hsl(348.72, 100%, 36%) IDisposable [feature]
○ hsl(27.69, 100%, 50%) async [feature]
○ hsl(9.23, 100%, 50%) interface [feature]
○ hsl(180.92, 100%, 28%) Database health check [function]
○ hsl(144.00, 100%, 42%) Connection resilience [function]
○ hsl(83.08, 100%, 42%) ILogger of T injection [function]
OBSERVED — 3 entries (explicit in request)
◉ hsl(85.13, 100%, 50%) using [feature]
◉ hsl(234.87, 100%, 50%) using [feature]
◉ hsl(3.75, 100%, 38%) CancellationToken [feature]
COMPLEXITY │ 13 entries │ spread: 345° │ band: MEDIUM
Baseline response (no ZPI)
### Generic Repository Implementation
Below is a simple implementation of a generic repository for read-only access to entities by id using a DbContext. This implementation includes `AsNoTracking` to improve performance by not tracking the entities and a `CancellationToken` parameter to support cancellation of the operation.
```csharp
using Microsoft.EntityFrameworkCore;
using System;
using System.Threading;
using System.Threading.Tasks;
public class ReadOnlyRepository<TEntity, TKey> where TEntity : class
{
protected readonly DbContext Context;
protected readonly DbSet<TEntity> DbSet;
public ReadOnlyRepository(DbContext context)
{
Context = context;
DbSet = context.Set<TEntity>();
}
/// <summary>
/// Gets an entity by id.
/// </summary>
/// <param name="id">The id of the entity to retrieve.</param>
/// <param name="cancellationToken">A token to cancel the operation.</param>
/// <returns>The entity with the specified id, or null if no entity is found.</returns>
public virtual async Task<TEntity> GetByIdAsync(TKey id, CancellationToken cancellationToken = default)
{
return await DbSet.AsNoTracking()
.FirstOrDefaultAsync(e => GetId(e).Equals(id), cancellationToken);
}
/// <summary>
/// Gets the id of an entity.
/// </summary>
/// <param name="entity">The entity to get the id from.</param>
/// <returns>The id of the entity.</returns>
protected virtual TKey GetId(TEntity entity)
{
// Assuming the id property is named "Id" and is of type TKey
// You may need to adjust this based on your entity's actual id property
return (TKey)entity.GetType().GetProperty("Id").GetValue(entity);
}
}
```
### Example Usage
To use this repository, you would create a class that inherits from `ReadOnlyRepository<TEntity, TKey>`. Here's an example:
```csharp
public class UserReadOnlyRepository : ReadOnlyRepository<User, int>
{
public UserReadOnlyRepository(MyDbContext context) : base(context)
{
}
}
```
You can then use the `UserReadOnlyRepository` to retrieve a `User` entity by id:
```csharp
public class MyService
{
private readonly UserReadOnlyRepository _repository;
public MyService(UserReadOnlyRepository repository)
{
_repository = repository;
}
public async Task<User> GetUserAsync(int id)
{
return await _repository.GetByIdAsync(id);
}
}
```
### Notes
* This implementation assumes that the id property of the entity is named "Id" and is of type `TKey`. You may need to adjust the `GetId` method based on your entity's actual id property.
* The `AsNoTracking` method is used to improve performance by not tracking the entities. This is suitable for read-only access, but you should not use it if you plan to modify the entities.
* The `CancellationToken` parameter is used to support cancellation of the operation. You can pass a token to the `GetByIdAsync` method to cancel the operation if needed.
Enhanced response (with ZPI)
```csharp
using Microsoft.EntityFrameworkCore;
using System;
using System.Threading;
using System.Threading.Tasks;
public class ReadOnlyRepository<T> where T : class
{
private readonly DbContext _context;
private readonly ILogger<ReadOnlyRepository<T>> _logger;
public ReadOnlyRepository(DbContext context, ILogger<ReadOnlyRepository<T>> logger)
{
_context = context;
_logger = logger;
}
public async Task<T> GetByIdAsync(int id, CancellationToken cancellationToken = default)
{
try
{
return await _context.Set<T>().AsNoTracking().FirstOrDefaultAsync(x => x.GetType().GetProperty("Id").GetValue(x).Equals(id), cancellationToken);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error retrieving entity by id");
throw;
}
}
}
```
This implementation provides a generic repository for read-only access to entities by id using a DbContext. The `GetByIdAsync` method uses `AsNoTracking` to improve performance by not tracking the retrieved entity, and a `CancellationToken` parameter to support cancellation of the operation. The method also includes error logging using an injected `ILogger`.
Note that this implementation assumes that the entity has a property named "Id" of type int. If the property name or type is different, the `GetProperty` and `GetValue` calls will need to be adjusted accordingly.
To use this repository, you would need to register it in the DI container and inject the DbContext and ILogger instances.
This implementation requires additional text to explain the usage and potential adjustments.
Grader chain
Each grader reads the same baseline + enhanced pair and casts an independent verdict. Disagreement is the framework's own signal — not noise to be averaged away.
V1samba/Meta-Llama-3.3-70B-Instruct
BetterV4qwen2.5-coder-3b-instruct
BetterV5openrouter/qwen/qwen3-235b-a22b-2507
BetterV7qwen2.5-coder-7b-instruct
WorseV8openai/gpt-4.1
SameV9anthropic/claude-opus-4-7
WorseV12openai/gpt-4o
BetterComments
Our grader said what it said. What do you say? Comment as a guest below.
No comments yet. Be the first to say what you make of this round.