implementing-repository-pattern

.NET Repository Pattern

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 "implementing-repository-pattern" with this command: npx skills add christian289/dotnet-with-claudecode/christian289-dotnet-with-claudecode-implementing-repository-pattern

.NET Repository Pattern

A guide for implementing the Repository pattern that abstracts the data access layer.

  1. Project Structure

MyApp/ ├── Program.cs ├── App.cs ├── Models/ │ └── User.cs ├── Repositories/ │ ├── IUserRepository.cs │ └── UserRepository.cs ├── Services/ │ ├── IUserService.cs │ └── UserService.cs └── GlobalUsings.cs

  1. Model Definition

namespace MyApp.Models;

public sealed record User(int Id, string Name, string Email);

  1. Repository Layer

3.1 Interface

namespace MyApp.Repositories;

public interface IUserRepository { Task<List<User>> GetAllAsync(); Task<User?> GetByIdAsync(int id); Task AddAsync(User user); Task UpdateAsync(User user); Task DeleteAsync(int id); }

3.2 Implementation

namespace MyApp.Repositories;

public sealed class UserRepository : IUserRepository { private readonly List<User> _users = [];

public Task&#x3C;List&#x3C;User>> GetAllAsync()
{
    return Task.FromResult(_users.ToList());
}

public Task&#x3C;User?> GetByIdAsync(int id)
{
    return Task.FromResult(_users.FirstOrDefault(u => u.Id == id));
}

public Task AddAsync(User user)
{
    _users.Add(user);
    return Task.CompletedTask;
}

public Task UpdateAsync(User user)
{
    var index = _users.FindIndex(u => u.Id == user.Id);
    if (index >= 0) _users[index] = user;
    return Task.CompletedTask;
}

public Task DeleteAsync(int id)
{
    _users.RemoveAll(u => u.Id == id);
    return Task.CompletedTask;
}

}

  1. Service Layer

4.1 Interface

namespace MyApp.Services;

public interface IUserService { Task<IReadOnlyList<User>> GetAllUsersAsync(); Task<User?> GetUserByIdAsync(int id); }

4.2 Implementation

namespace MyApp.Services;

public sealed class UserService(IUserRepository repository) : IUserService { private readonly IUserRepository _repository = repository;

public async Task&#x3C;IReadOnlyList&#x3C;User>> GetAllUsersAsync()
{
    var users = await _repository.GetAllAsync();
    return users.AsReadOnly();
}

public Task&#x3C;User?> GetUserByIdAsync(int id)
{
    return _repository.GetByIdAsync(id);
}

}

  1. DI Registration

var host = Host.CreateDefaultBuilder(args) .ConfigureServices(services => { // Register Repository services.AddSingleton<IUserRepository, UserRepository>();

    // Register Service
    services.AddSingleton&#x3C;IUserService, UserService>();

    services.AddSingleton&#x3C;App>();
})
.Build();

6. Generic Repository (Optional)

public interface IRepository<T> where T : class { Task<List<T>> GetAllAsync(); Task<T?> GetByIdAsync(int id); Task AddAsync(T entity); Task UpdateAsync(T entity); Task DeleteAsync(int id); }

  1. Layer Structure

App (Presentation) ↓ Service Layer (Business Logic) ↓ Repository Layer (Data Access) ↓ Data Source (DB, API, File, etc.)

  1. Core Principles
  • Repository handles data access only

  • Business logic goes in Service

  • Abstract with interfaces for testability

  • Use Constructor Injection for dependencies

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

converting-html-css-to-wpf-xaml

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

publishing-wpf-apps

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

managing-styles-resourcedictionary

No summary provided by upstream source.

Repository SourceNeeds Review