Link Search Menu Expand Document

Repositories


A repository is a software design pattern implemented as an additional layer between the application and the database, represented as a class object. It centralizes data access logic — including basic operations such as Insert, Delete, and Update, as well as application-specific advanced operations. All data access in the application flows through this object rather than directly to the database, promoting correct call chains and code reusability.

Type of Repositories

This library provides two repository base classes:

ObjectDescription
BaseRepositoryA base repository for entity-specific repositories.
DbRepositoryA generic base repository supporting any entity type.

Creating an Entity-Based Repository

First, create an interface:

public interface IPersonRepository
{
    int Delete(Person person);
    Person Get(int id);
    IEnumerable<Person> GetAll();
    int Save(Person person);
    int SaveAll(IEnumerable<Person> people);
    int Update(Person person);
}

Then create a class that inherits from BaseRepository and implements the interface:

public class PersonRepository : BaseRepository<Person, SqlConnection>, IPersonRepository
{
    public PersonRepository(IOptions<AppSettings> settings)
        : base(settings.Value.ConnectionString)
    { }
}

Implement the interface methods:

Delete

public int Delete(Person person)
{
    return base.Delete(person);
}

Get

public Person Get(int id)
{
    return Query(id).FirstOrDefault();
}

public IEnumerable<Person> GetAll()
{
    return QueryAll();
}

Save

public int Save(Person person)
{
    return Insert<int>(person);
}

public int SaveAll(IEnumerable<Person> people)
{
    return InsertAll(people);
}

Update

public int Update(Person person)
{
    return Update(person);
}

Register with the DI container:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();

    // Registration
    services.AddTransient<IPersonRepository, PersonRepository>();
}

Please visit our BaseRepository reference implementation page for the detailed implementation.

Creating a Database Level Repository

First, create an interface:

public interface INorthwindRepository
{
    // Customer
    int DeleteCustomer(Customer customer);
    Customer GetCustomer(int id);
    IEnumerable<Customer> GetAllCustomers();
    int SaveCustomer(Customer customer);
    int SaveAllPCustomers(IEnumerable<Customer> customers);
    int UpdateCustomer(Customer customer);

    // Order
    int DeleteOrder(Order order);
    Order GetOrder(int id);
    IEnumerable<Order> GetAllOrders();
    int SaveOrder(Order order);
    int SaveAllOrders(IEnumerable<Order> orders);
    int UpdateOrder(Order order);
}

Then create a class that inherits from DbRepository and implements the interface:

public class NorthwindRepository : DbRepository<SqlConnection>, INorthwindRepository
{
    public PersonRepository(IOptions<AppSettings> settings)
        : base(settings.Value.ConnectionString)
    { }
}

Implement the interface methods:

Delete

// Customer

public int DeleteCustomer(Customer customer)
{
    return Delete<Customer>(customer);
}

// Order

public int DeleteOrder(Order order)
{
    return Delete<Order>(order);
}

Get

// Customer

public Customer GetCustomer(int id)
{
    return Query<Customer>(id).FirstOrDefault();
}

public IEnumerable<Customer> GetAllCustomers()
{
    return QueryAll<Customer>();
}

// Order

public Order GetOrder(int id)
{
    return Query<Order>(id).FirstOrDefault();
}

public IEnumerable<Order> GetAllOrders()
{
    return QueryAll<Order>();
}

Save

// Customer

public int SaveCustomer(Customer customer)
{
    return Insert<Customer, int>(customer);
}

public int SaveAllCustomers(IEnumerable<Customer> people)
{
    return InsertAll<int><Customer>(people);
}

// Order

public int SaveOrder(Order order)
{
    return Insert<Order, int>(order);
}

public int SaveAllOrders(IEnumerable<Order> people)
{
    return InsertAll<Order>(people);
}

Update

// Customer

public int UpdateCustomer(Customer customer)
{
    return Update<Customer>(customer);
}

// Order

public int UpdateOrder(Order order)
{
    return Update<Order>(order);
}

Register with the DI container:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();

    // Registration
    services.AddTransient<INorthwindRepository, NorthwindRepository>();
}

Please visit our DbRepository reference implementation page for the detailed implementation.

Creating a Custom Generic Repository

First, create a generic interface:

public interface IGenericRepository<TEntity, TDbConnection>
{
    TDbConnection CreateConnection();
    int Delete(TEntity entity);
    TEntity Get(int id);
    IEnumerable<TEntity> GetAll();
    int Save(TEntity entity);
    int SaveAll(IEnumerable<TEntity> entities);
    int Update(TEntity entity);
}

Then create a base interface that inherits the generic interface:

public interface IPersonRepository : IGenericRepository<Person, SqlConnection>
{
    ...
}

Then create a class that inherits from GenericRepository and implements the interface:

public class PersonRepository : GenericRepository<Person, SqlConnection>, IPersonRepository
{
    private IOptions<AppSettings> _settings = null;

    public PersonRepository(IOptions<AppSettings> settings)
        : base(settings.Value.ConnectionString)
    {
        _settings = settings;
    }
}

Implement the interface methods:

CreateConnection

public SqlConnection CreateConnection()
{
    return new SqlConnection(_settings.Value.ConnectionString);
}

Delete

public int Delete(Person entity)
{
    using (var connection = CreateConnection())
    {
        return connection.Delete<Person>(entity);
    }
}

Get

public Person Get(int id)
{
    using (var connection = CreateConnection())
    {
        return connection.Query(id).FirstOrDefault();
    }
}

public IEnumerable<Person> GetAll()
{
    using (var connection = CreateConnection())
    {
        return connection.QueryAll(id);
    }
}

Save

public int Save(Person entity)
{
    using (var connection = CreateConnection())
    {
        return Insert<Person, int>(entity);
    }
}

public int SaveAll(IEnumerable<Person> entities)
{
    using (var connection = CreateConnection())
    {
        return InsertAll<Person>(entities);
    }
}

Update

public int Update(Person entity)
{
    using (var connection = CreateConnection())
    {
        return Update<Person>(entity);
    }
}

Register with the DI container:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();

    // Registration
    services.AddTransient<IPersonRepository, PersonRepository>();
}

Please visit our Generic Repository reference implementation page for the detailed implementation.