## What is a Repository Interface? A repository interface defines the contract between an application and its data-access layer. It abstracts the details of how data is stored or queried, exposing only the operations the application needs-such as create, read, update, and delete. Implementations of the interface can then be swapped without changing the business logic. --- # Examples - https://github.com/topics/repository-pattern ## TypeScript ```typescript export interface IRepository { findById(id: TKey): Promise; findAll(): Promise; findOne(filter: Partial): Promise; add(entity: T): Promise; update(id: TKey, entity: Partial): Promise; delete(id: TKey): Promise; } ``` ### Examples - https://github.com/ErickWendel/generic-repository-nodejs-typescript-article/blob/master/repositories/base/BaseRepository.ts ## Python ```python from abc import ABC, abstractmethod from typing import Generic, TypeVar, List, Optional T = TypeVar('T') K = TypeVar('K') class IRepository(ABC, Generic[T, K]): @abstractmethod def find_by_id(self, id: K) -> Optional[T]: ... @abstractmethod def find_all(self) -> List[T]: ... @abstractmethod def add(self, entity: T) -> T: ... @abstractmethod def update(self, id: K, entity: T) -> Optional[T]: ... @abstractmethod def delete(self, id: K) -> bool: ... ``` ### Examples - https://gist.github.com/Greyvend/b56baa53b96e5bbfa7b650c3e6b69d40 ## Java ```java public interface IRepository { T findById(K id); List findAll(); T save(T entity); T update(K id, T entity); boolean delete(K id); } ``` ### Examples - https://github.com/JAVA-MSDT/repository-design-pattern ## Rust ```rust pub trait Repository { fn find_by_id(&self, id: K) -> Option; fn find_all(&self) -> Vec; fn add(&mut self, entity: T) -> T; fn update(&mut self, id: K, entity: T) -> Option; fn delete(&mut self, id: K) -> bool; } ``` ## Go ```go type Repository[T any, K comparable] interface { FindById(id K) (T, bool) FindAll() []T Add(entity T) T Update(id K, entity T) (T, bool) Delete(id K) bool } ``` ### Examples - https://github.com/2yuri/repository-pattern-go - https://github.com/hendratommy/repository-pattern ### c# https://github.com/threenine/Threenine.Data/tree/master/src ```c# public interface IRepository : IDisposable where T : class { Task FindOne(Expression> predicate = null; Task> FindManyPagedOrdered(Expression> predicate = null, Func, IOrderedQueryable> orderBy = null, Func, IIncludableQueryable> include = null, int pageNumber = 1, int pageSize = 20, CancellationToken cancellationToken = default); IQueryable FindManyQuery(string sql, params object[] parameters); IQueryable Search(params object[] keyValues); void Add(T entity); void Add(params T[] entities); void Add(IEnumerable entities); void Delete(T entity); void Delete(object id); void Delete(params T[] entities); void Delete(IEnumerable entities); void Update(T entity); void Update(params T[] entities); void Update(IEnumerable entities); } ``` ### Examples - https://www.codeguru.com/csharp/repository-pattern-c-sharp/ - https://learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application ## Node.js ### Examples - https://github.com/blugavere/node-repositories - https://github.com/claclacla/Building-a-Node-Express.js-Rest-API-server-using-a-repository-pattern ## Elf ### Examples - https://ngneat.github.io/elf/docs/repository/