repository-pattern

Repository Pattern Generator

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "repository-pattern" with this command: npx skills add ronnythedev/dotnet-clean-architecture-skills/ronnythedev-dotnet-clean-architecture-skills-repository-pattern

Repository Pattern Generator

Overview

This skill generates Repositories that provide an abstraction over data access:

  • Interface in Domain layer - Defines data access contract

  • Implementation in Infrastructure - Uses EF Core

  • Per Aggregate Root - Not per entity

  • Unit of Work integration - SaveChanges via IUnitOfWork

Quick Reference

Repository Method Purpose Returns

GetByIdAsync

Retrieve by primary key Entity?

GetByXxxAsync

Retrieve by business key Entity?

GetAllAsync

Retrieve all (use sparingly) IReadOnlyList<Entity>

Add

Track new entity void

Update

Track modified entity void

Remove

Track deleted entity void

ExistsAsync

Check existence bool

Repository Structure

/Domain/{Aggregate}/ └── I{Entity}Repository.cs # Interface (Domain layer)

/Infrastructure/Repositories/ └── {Entity}Repository.cs # Implementation (Infrastructure layer)

Template: Repository Interface (Domain Layer)

// src/{name}.domain/{Aggregate}/I{Entity}Repository.cs namespace {name}.domain.{aggregate};

public interface I{Entity}Repository { // ═══════════════════════════════════════════════════════════════ // QUERY METHODS // ═══════════════════════════════════════════════════════════════

/// &#x3C;summary>
/// Gets an entity by its unique identifier
/// &#x3C;/summary>
Task&#x3C;{Entity}?> GetByIdAsync(
    Guid id,
    CancellationToken cancellationToken = default);

/// &#x3C;summary>
/// Gets an entity by its unique identifier with related entities
/// &#x3C;/summary>
Task&#x3C;{Entity}?> GetByIdWithDetailsAsync(
    Guid id,
    CancellationToken cancellationToken = default);

/// &#x3C;summary>
/// Gets an entity by a unique business key
/// &#x3C;/summary>
Task&#x3C;{Entity}?> GetByNameAsync(
    string name,
    CancellationToken cancellationToken = default);

/// &#x3C;summary>
/// Gets all entities for a parent organization
/// &#x3C;/summary>
Task&#x3C;IReadOnlyList&#x3C;{Entity}>> GetByOrganizationIdAsync(
    Guid organizationId,
    CancellationToken cancellationToken = default);

/// &#x3C;summary>
/// Gets all active entities
/// &#x3C;/summary>
Task&#x3C;IReadOnlyList&#x3C;{Entity}>> GetAllActiveAsync(
    CancellationToken cancellationToken = default);

/// &#x3C;summary>
/// Checks if an entity exists
/// &#x3C;/summary>
Task&#x3C;bool> ExistsAsync(
    Guid id,
    CancellationToken cancellationToken = default);

/// &#x3C;summary>
/// Checks if an entity with the given name exists
/// &#x3C;/summary>
Task&#x3C;bool> ExistsByNameAsync(
    string name,
    CancellationToken cancellationToken = default);

// ═══════════════════════════════════════════════════════════════
// COMMAND METHODS (tracking only, no SaveChanges)
// ═══════════════════════════════════════════════════════════════

/// &#x3C;summary>
/// Adds a new entity to the context
/// &#x3C;/summary>
void Add({Entity} {entity});

/// &#x3C;summary>
/// Adds multiple entities to the context
/// &#x3C;/summary>
void AddRange(IEnumerable&#x3C;{Entity}> {entities});

/// &#x3C;summary>
/// Updates an existing entity in the context
/// &#x3C;/summary>
void Update({Entity} {entity});

/// &#x3C;summary>
/// Removes an entity from the context
/// &#x3C;/summary>
void Remove({Entity} {entity});

/// &#x3C;summary>
/// Removes multiple entities from the context
/// &#x3C;/summary>
void RemoveRange(IEnumerable&#x3C;{Entity}> {entities});

}

Template: Repository Implementation (Infrastructure Layer)

// src/{name}.infrastructure/Repositories/{Entity}Repository.cs using Microsoft.EntityFrameworkCore; using {name}.domain.{aggregate};

namespace {name}.infrastructure.repositories;

internal sealed class {Entity}Repository : I{Entity}Repository { private readonly ApplicationDbContext _dbContext;

public {Entity}Repository(ApplicationDbContext dbContext)
{
    _dbContext = dbContext;
}

// ═══════════════════════════════════════════════════════════════
// QUERY METHODS
// ═══════════════════════════════════════════════════════════════

public async Task&#x3C;{Entity}?> GetByIdAsync(
    Guid id,
    CancellationToken cancellationToken = default)
{
    return await _dbContext
        .Set&#x3C;{Entity}>()
        .FirstOrDefaultAsync(e => e.Id == id, cancellationToken);
}

public async Task&#x3C;{Entity}?> GetByIdWithDetailsAsync(
    Guid id,
    CancellationToken cancellationToken = default)
{
    return await _dbContext
        .Set&#x3C;{Entity}>()
        .Include(e => e.{ChildEntities})
        .Include(e => e.{OtherRelation})
        .FirstOrDefaultAsync(e => e.Id == id, cancellationToken);
}

public async Task&#x3C;{Entity}?> GetByNameAsync(
    string name,
    CancellationToken cancellationToken = default)
{
    return await _dbContext
        .Set&#x3C;{Entity}>()
        .FirstOrDefaultAsync(
            e => e.Name.ToLower() == name.ToLower(),
            cancellationToken);
}

public async Task&#x3C;IReadOnlyList&#x3C;{Entity}>> GetByOrganizationIdAsync(
    Guid organizationId,
    CancellationToken cancellationToken = default)
{
    return await _dbContext
        .Set&#x3C;{Entity}>()
        .Where(e => e.OrganizationId == organizationId)
        .OrderBy(e => e.Name)
        .ToListAsync(cancellationToken);
}

public async Task&#x3C;IReadOnlyList&#x3C;{Entity}>> GetAllActiveAsync(
    CancellationToken cancellationToken = default)
{
    return await _dbContext
        .Set&#x3C;{Entity}>()
        .Where(e => e.IsActive)
        .OrderBy(e => e.Name)
        .ToListAsync(cancellationToken);
}

public async Task&#x3C;bool> ExistsAsync(
    Guid id,
    CancellationToken cancellationToken = default)
{
    return await _dbContext
        .Set&#x3C;{Entity}>()
        .AnyAsync(e => e.Id == id, cancellationToken);
}

public async Task&#x3C;bool> ExistsByNameAsync(
    string name,
    CancellationToken cancellationToken = default)
{
    return await _dbContext
        .Set&#x3C;{Entity}>()
        .AnyAsync(
            e => e.Name.ToLower() == name.ToLower(),
            cancellationToken);
}

// ═══════════════════════════════════════════════════════════════
// COMMAND METHODS
// ═══════════════════════════════════════════════════════════════

public void Add({Entity} {entity})
{
    _dbContext.Set&#x3C;{Entity}>().Add({entity});
}

public void AddRange(IEnumerable&#x3C;{Entity}> {entities})
{
    _dbContext.Set&#x3C;{Entity}>().AddRange({entities});
}

public void Update({Entity} {entity})
{
    _dbContext.Set&#x3C;{Entity}>().Update({entity});
}

public void Remove({Entity} {entity})
{
    _dbContext.Set&#x3C;{Entity}>().Remove({entity});
}

public void RemoveRange(IEnumerable&#x3C;{Entity}> {entities})
{
    _dbContext.Set&#x3C;{Entity}>().RemoveRange({entities});
}

}

Template: Repository with Child Entity Access

// src/{name}.domain/{Aggregate}/I{Entity}Repository.cs namespace {name}.domain.{aggregate};

public interface I{Entity}Repository { // Standard methods...

// ═══════════════════════════════════════════════════════════════
// CHILD ENTITY QUERIES (accessed through aggregate root)
// ═══════════════════════════════════════════════════════════════

/// &#x3C;summary>
/// Gets a child entity through its aggregate root
/// &#x3C;/summary>
Task&#x3C;{ChildEntity}?> Get{ChildEntity}ByIdAsync(
    Guid {entity}Id,
    Guid {childEntity}Id,
    CancellationToken cancellationToken = default);

/// &#x3C;summary>
/// Gets all child entities for a parent
/// &#x3C;/summary>
Task&#x3C;IReadOnlyList&#x3C;{ChildEntity}>> Get{ChildEntities}By{Entity}IdAsync(
    Guid {entity}Id,
    CancellationToken cancellationToken = default);

}

// src/{name}.infrastructure/Repositories/{Entity}Repository.cs internal sealed class {Entity}Repository : I{Entity}Repository { // ... other methods

public async Task&#x3C;{ChildEntity}?> Get{ChildEntity}ByIdAsync(
    Guid {entity}Id,
    Guid {childEntity}Id,
    CancellationToken cancellationToken = default)
{
    var {entity} = await _dbContext
        .Set&#x3C;{Entity}>()
        .Include(e => e.{ChildEntities})
        .FirstOrDefaultAsync(e => e.Id == {entity}Id, cancellationToken);

    return {entity}?.{ChildEntities}
        .FirstOrDefault(c => c.Id == {childEntity}Id);
}

public async Task&#x3C;IReadOnlyList&#x3C;{ChildEntity}>> Get{ChildEntities}By{Entity}IdAsync(
    Guid {entity}Id,
    CancellationToken cancellationToken = default)
{
    var {entity} = await _dbContext
        .Set&#x3C;{Entity}>()
        .Include(e => e.{ChildEntities})
        .FirstOrDefaultAsync(e => e.Id == {entity}Id, cancellationToken);

    return {entity}?.{ChildEntities}.ToList() 
        ?? new List&#x3C;{ChildEntity}>();
}

}

Template: Repository with Specification Pattern

// src/{name}.domain/Abstractions/ISpecification.cs using System.Linq.Expressions;

namespace {name}.domain.abstractions;

public interface ISpecification<T> { Expression<Func<T, bool>> Criteria { get; } List<Expression<Func<T, object>>> Includes { get; } List<string> IncludeStrings { get; } Expression<Func<T, object>>? OrderBy { get; } Expression<Func<T, object>>? OrderByDescending { get; } int? Take { get; } int? Skip { get; } bool IsPagingEnabled { get; } }

// src/{name}.domain/Abstractions/BaseSpecification.cs using System.Linq.Expressions;

namespace {name}.domain.abstractions;

public abstract class BaseSpecification<T> : ISpecification<T> { public Expression<Func<T, bool>> Criteria { get; private set; } = _ => true; public List<Expression<Func<T, object>>> Includes { get; } = new(); public List<string> IncludeStrings { get; } = new(); public Expression<Func<T, object>>? OrderBy { get; private set; } public Expression<Func<T, object>>? OrderByDescending { get; private set; } public int? Take { get; private set; } public int? Skip { get; private set; } public bool IsPagingEnabled { get; private set; }

protected void AddCriteria(Expression&#x3C;Func&#x3C;T, bool>> criteria)
{
    Criteria = criteria;
}

protected void AddInclude(Expression&#x3C;Func&#x3C;T, object>> includeExpression)
{
    Includes.Add(includeExpression);
}

protected void AddInclude(string includeString)
{
    IncludeStrings.Add(includeString);
}

protected void ApplyOrderBy(Expression&#x3C;Func&#x3C;T, object>> orderByExpression)
{
    OrderBy = orderByExpression;
}

protected void ApplyOrderByDescending(Expression&#x3C;Func&#x3C;T, object>> orderByDescExpression)
{
    OrderByDescending = orderByDescExpression;
}

protected void ApplyPaging(int skip, int take)
{
    Skip = skip;
    Take = take;
    IsPagingEnabled = true;
}

}

// src/{name}.domain/{Aggregate}/Specifications/Active{Entities}Specification.cs using {name}.domain.abstractions;

namespace {name}.domain.{aggregate}.specifications;

public sealed class Active{Entities}Specification : BaseSpecification<{Entity}> { public Active{Entities}Specification() { AddCriteria(e => e.IsActive); ApplyOrderBy(e => e.Name); } }

public sealed class {Entities}ByOrganizationSpecification : BaseSpecification<{Entity}> { public {Entities}ByOrganizationSpecification(Guid organizationId) { AddCriteria(e => e.OrganizationId == organizationId && e.IsActive); AddInclude(e => e.{ChildEntities}); ApplyOrderBy(e => e.Name); } }

// Repository with specification support public interface I{Entity}Repository { Task<IReadOnlyList<{Entity}>> GetAsync( ISpecification<{Entity}> specification, CancellationToken cancellationToken = default);

Task&#x3C;{Entity}?> GetFirstOrDefaultAsync(
    ISpecification&#x3C;{Entity}> specification,
    CancellationToken cancellationToken = default);

Task&#x3C;int> CountAsync(
    ISpecification&#x3C;{Entity}> specification,
    CancellationToken cancellationToken = default);

}

Template: Generic Repository Base (Optional)

// src/{name}.infrastructure/Repositories/Repository.cs using Microsoft.EntityFrameworkCore; using {name}.domain.abstractions;

namespace {name}.infrastructure.repositories;

internal abstract class Repository<T> where T : Entity { protected readonly ApplicationDbContext DbContext;

protected Repository(ApplicationDbContext dbContext)
{
    DbContext = dbContext;
}

public async Task&#x3C;T?> GetByIdAsync(
    Guid id,
    CancellationToken cancellationToken = default)
{
    return await DbContext
        .Set&#x3C;T>()
        .FirstOrDefaultAsync(e => e.Id == id, cancellationToken);
}

public void Add(T entity)
{
    DbContext.Set&#x3C;T>().Add(entity);
}

public void Update(T entity)
{
    DbContext.Set&#x3C;T>().Update(entity);
}

public void Remove(T entity)
{
    DbContext.Set&#x3C;T>().Remove(entity);
}

}

// Using the base repository internal sealed class {Entity}Repository : Repository<{Entity}>, I{Entity}Repository { public {Entity}Repository(ApplicationDbContext dbContext) : base(dbContext) { }

// Add entity-specific methods
public async Task&#x3C;{Entity}?> GetByNameAsync(
    string name,
    CancellationToken cancellationToken = default)
{
    return await DbContext
        .Set&#x3C;{Entity}>()
        .FirstOrDefaultAsync(
            e => e.Name.ToLower() == name.ToLower(),
            cancellationToken);
}

}

Registering Repositories

// src/{name}.infrastructure/DependencyInjection.cs private static void AddPersistence(IServiceCollection services, IConfiguration configuration) { var connectionString = configuration.GetConnectionString("Database") ?? throw new ArgumentNullException(nameof(configuration));

services.AddDbContext&#x3C;ApplicationDbContext>(options =>
{
    options.UseNpgsql(connectionString)
           .UseSnakeCaseNamingConvention();
});

// Register Unit of Work
services.AddScoped&#x3C;IUnitOfWork>(sp => 
    sp.GetRequiredService&#x3C;ApplicationDbContext>());

// Register Repositories
services.AddScoped&#x3C;IUserRepository, UserRepository>();
services.AddScoped&#x3C;IOrganizationRepository, OrganizationRepository>();
services.AddScoped&#x3C;IDepartmentRepository, DepartmentRepository>();
services.AddScoped&#x3C;ISurveyRepository, SurveyRepository>();
// Add more repositories here...

// Register SQL Connection Factory for Dapper queries
services.AddSingleton&#x3C;ISqlConnectionFactory>(_ => 
    new SqlConnectionFactory(connectionString));

}

Query Optimization Patterns

AsNoTracking for Read-Only Queries

public async Task<IReadOnlyList<{Entity}>> GetAllForDisplayAsync( CancellationToken cancellationToken = default) { return await _dbContext .Set<{Entity}>() .AsNoTracking() // Performance: no change tracking .Where(e => e.IsActive) .OrderBy(e => e.Name) .ToListAsync(cancellationToken); }

Selective Includes (Avoid Over-fetching)

// ❌ WRONG: Loading everything public async Task<{Entity}?> GetByIdAsync(Guid id, CancellationToken ct) { return await _dbContext .Set<{Entity}>() .Include(e => e.Children) .Include(e => e.Parent) .Include(e => e.Logs) // Potentially thousands of records! .FirstOrDefaultAsync(e => e.Id == id, ct); }

// ✅ CORRECT: Separate methods for different needs public async Task<{Entity}?> GetByIdAsync(Guid id, CancellationToken ct) { return await _dbContext .Set<{Entity}>() .FirstOrDefaultAsync(e => e.Id == id, ct); }

public async Task<{Entity}?> GetByIdWithChildrenAsync(Guid id, CancellationToken ct) { return await _dbContext .Set<{Entity}>() .Include(e => e.Children) .FirstOrDefaultAsync(e => e.Id == id, ct); }

Split Queries for Large Collections

public async Task<{Entity}?> GetByIdWithAllRelationsAsync( Guid id, CancellationToken cancellationToken = default) { return await _dbContext .Set<{Entity}>() .Include(e => e.Children) .Include(e => e.OtherRelation) .AsSplitQuery() // Splits into multiple SQL queries .FirstOrDefaultAsync(e => e.Id == id, cancellationToken); }

Critical Rules

  • Repository per aggregate root - Not per entity

  • No SaveChanges in repository - That's IUnitOfWork's job

  • Interface in Domain - Implementation in Infrastructure

  • Use CancellationToken - All async methods

  • Return null for not found - Let handler decide what to do

  • AsNoTracking for reads - When not modifying

  • Selective Includes - Don't over-fetch

  • Avoid GetAll without filters - Can be dangerous at scale

  • Child entities through aggregate - Don't expose child repositories

  • Internal class for implementation - Hide implementation details

Anti-Patterns to Avoid

// ❌ WRONG: SaveChanges in repository public void Add({Entity} {entity}) { _dbContext.Set<{Entity}>().Add({entity}); _dbContext.SaveChanges(); // Don't do this! }

// ✅ CORRECT: Only track, save via UnitOfWork public void Add({Entity} {entity}) { _dbContext.Set<{Entity}>().Add({entity}); } // In handler: await _unitOfWork.SaveChangesAsync(ct);

// ❌ WRONG: Repository for child entities public interface IOrderItemRepository { ... }

// ✅ CORRECT: Access through aggregate root public interface IOrderRepository { Task<OrderItem?> GetOrderItemAsync(Guid orderId, Guid itemId, ...); }

// ❌ WRONG: Exposing IQueryable public IQueryable<{Entity}> GetAll() => _dbContext.Set<{Entity}>();

// ✅ CORRECT: Return materialized lists public async Task<IReadOnlyList<{Entity}>> GetAllAsync(CancellationToken ct) { return await _dbContext.Set<{Entity}>().ToListAsync(ct); }

// ❌ WRONG: Business logic in repository public async Task<{Entity}?> GetActiveByIdAsync(Guid id, CancellationToken ct) { var entity = await GetByIdAsync(id, ct); if (entity?.IsActive == false) throw new BusinessException("Entity is inactive"); // Wrong! return entity; }

// ✅ CORRECT: Let handler handle business logic public async Task<{Entity}?> GetByIdAsync(Guid id, CancellationToken ct) { return await _dbContext.Set<{Entity}>() .FirstOrDefaultAsync(e => e.Id == id, ct); }

Related Skills

  • domain-entity-generator

  • Generate entities for repositories

  • ef-core-configuration

  • Configure entity mappings

  • cqrs-command-generator

  • Use repositories in handlers

  • dotnet-clean-architecture

  • Overall project structure

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

Coding

dotnet-clean-architecture

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

unit-testing

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

permission-authorization

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

quartz-background-jobs

No summary provided by upstream source.

Repository SourceNeeds Review