Giter Site home page Giter Site logo

mr.patterns.repository's Introduction

MR.Patterns.Repository

Build status NuGet version License

Provides a base when implementing the repository pattern with EF6.

(Version 3.1 and above supports EF 6.4)

Objectives

  • Quickly implement the pattern with little to no plumbing or boilerplate on your end.
  • Ease out unit testing by providing some kind of an in memory repository store.

To that end:

  • Have a base repository class that handles most of the plumbing (like implementing Add, Remove, Update, SaveChanges, Transactions, ...).
  • Have another base repository class that in memory implementations should inherit from. It should basically be an in memory store for entities that also provides things like ensuring (int, long) PKs get a proper incrementing value when entities are added.

The basics

IRepositoryCore, RepositoryCore, and InMemoryRepositoryCore are provided for your (real) Repository classes to inherit from. They do most of the work.

A simple example

// This is your service.
public interface IRepository : IRepositoryCore
{
    IQueryable<Blog> Blogs { get; }
}
// This is the real repository that stores entities in a database.
public class Repository : RepositoryCore<ApplicationDbContext>, IRepository
{
    public Repository(ApplicationDbContext context) : base(context) { }

    IQueryable<Blog> Blogs => Context.Blogs;

    // Add, Remove, Update, SaveChangesAsync are all implemented in RepositoryCore.
}
// This is the in memory repository that will depend on InMemoryRepositoryCore to store entities in memory.
public class InMemoryRepository : InMemoryRepositoryCore, IRepository
{
    // For() is a method on InMemoryRepositoryCore
    IQueryable<Blog> Blogs => For<Blog>();

    // Add, Remove, Update, SaveChangesAsync are all implemented in InMemoryRepositoryCore.
}

Note: The InMemoryRepository requires the PK property to be called "Id" for auto incrementing to work.

Nice improvements to have

  • Relationship fixups for the in memory store.

mr.patterns.repository's People

Contributors

mrahhal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

ladislavsopko

mr.patterns.repository's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.