Skip to content

Latest commit

 

History

154 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

img|300x200

Repository

.NET Coverage Status nuget CodeQL

Version Package Description
NuGet Package ManagedCode.Repository.Core Core
NuGet Package ManagedCode.Repository.AzureTable AzureTable
NuGet Package ManagedCode.Repository.CosmosDB CosmosDB
NuGet Package ManagedCode.Repository.LiteDB LiteDB
NuGet Package ManagedCode.Repository.MongoDB MongoDB
NuGet Package ManagedCode.Repository.SQLite SQLite
NuGet Package ManagedCode.Repository.EntityFramework EntityFramework
NuGet Package ManagedCode.Repository.EntityFramework.MSSQL MSSQL
NuGet Package ManagedCode.Repository.EntityFramework.PostgreSQL PostgreSQL

Repository


Repository pattern implementation for C#.

A universal repository for working with multiple databases:

  • InMemory
  • Azure Tables
  • CosmosDB
  • LiteDB
  • SQLite
  • MSSQL
  • PostgreSQL

General concept

We don't think you can hide the real database completely behind abstractions, so I recommend using your interfaces that will lend IReposity of the right type. And do the same with the direct implementation.

// declare the model as a descendant of the base type.
public class SomeModel : IItem<T>
{

}
// then create an interface
public interface ISomeRepository : IRepository<TId, TItem> where TItem : IItem<TId>
{
}
// create a class inherited from a repository of the desired type
public class SomeRepository : BaseRepository<TId, TItem> where TItem : class, IItem<TId>, new()
{
}

And then add your interface and dependency configuration class.

services
        .AddTransient<ISomeRepository, SomeRepository>()

This is to define the id type, and the object itself.


Azure Table

// declare the model as a descendant of the base type.
public class SomeModel : AzureTableItem
{

}


// then create an interface
public interface ISomeRepository : IAzureTableRepository<SomeModel>
{
}


// create a class inherited from a repository of the desired type
public class SomeRepository : AzureTableRepository<SomeModel>, ISomeRepository
{
    public SomeRepository(ILogger<SomeRepository> logger, IConfiguration config) : base(logger, 
        new AzureTableRepositoryOptions
        {
            ConnectionString = "connectionString"
        })
    {
    }
}

CosmosDB

// declare the model as a descendant of the base type.
public class SomeModel : CosmosDbItem
{

}


// then create an interface
public interface ISomeRepository : ICosmosDbRepository<SomeModel>
{
}


// create a class inherited from a repository of the desired type
public class SomeRepository : CosmosDbRepository<SomeModel>, ISomeRepository
{
    public SomeRepository(ILogger<SomeRepository> logger, IConfiguration config) : base(logger, 
        new CosmosDbRepositoryOptions
        {
            ConnectionString = "connectionString"
        })
    {
    }
}

LiteDB

// declare the model as a descendant of the base type.
public class SomeModel : LiteDbItem<string>
{

}


// then create an interface
public interface ISomeRepository : ILiteDbRepository<string, SomeModel>
{
}


// create a class inherited from a repository of the desired type
public class SomeRepository : LiteDbRepository<SomeModel>, ISomeRepository
{
    public SomeRepository(ILogger<SomeRepository> logger, IConfiguration config) : base(logger, 
        new LiteDbRepositoryOptions
        {
            ConnectionString = "connectionString"
        })
    {
    }
}

MongoDB

// declare the model as a descendant of the base type.
public class SomeModel : MongoDbItem<string>
{

}


// then create an interface
public interface ISomeRepository : IMongoDbRepository<SomeModel>
{
}


// create a class inherited from a repository of the desired type
public class SomeRepository : MongoDbRepository<SomeModel>, ISomeRepository
{
    public SomeRepository(ILogger<SomeRepository> logger, IConfiguration config) : base(logger, 
        new LiteDbRepositoryOptions
        {
            ConnectionString = "connectionString"
        })
    {
    }
}

SQLite

// declare the model as a descendant of the base type.
public class SomeModel : SQLiteItem
{

}


// then create an interface
public interface ISomeRepository : ISQLiteRepository<string, SomeModel>
{
}


// create a class inherited from a repository of the desired type
public class SomeRepository : SQLiteRepository<SomeModel>, ISomeRepository
{
    public SomeRepository(ILogger<SomeRepository> logger, IConfiguration config) : base(logger, 
        new SQLiteRepositoryOptions
        {
            ConnectionString = "connectionString"
        })
    {
    }
}

About

Unified interface for working with a variety of different document-oriented NoSQL databases.

Topics

Resources

Stars

28 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages