Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up an in-memory repository

I have the following class:

public class InMemoryRepository : IRepository
{
    public void Add(object entity)
    {
        throw new NotImplementedException();
    }

    public void Attach(object Entity)
    {
        throw new NotImplementedException();
    }

    public T Get<T>(object id)
    {
        throw new NotImplementedException();
    }

    public IList<T> GetAll<T>(string queryName)
    {
        throw new NotImplementedException();
    }

    public IList<T> GetAll<T>()
    {
        throw new NotImplementedException();
    }

    public IQueryable<T> Query<T>()
    {
        throw new NotImplementedException();
    }

    public void Remove(object entity)
    {
        throw new NotImplementedException();
    }

    public void Save(object entity)
    {
        throw new NotImplementedException();
    }
}

Our default repository implementation uses NHibernate for the backing store, but I'd like to implement an in-memory version of it so I can prototype the domain objects without having to create a backing SQL database. Assuming the convention that all objects have an Id property as the primary key, how would you implement a generic memory store for this?

Some key points I'm having a hard time addressing:

  • The repository methods themselves are generic, so I need some mechanism for automatically storing and referencing different types. Get<TestEntity>(object id) should be able to query all stored instances of TestEntity and find the one with the matching Id property, but I can't define a collection of TestEntity objects directly, as the repository won't know what types I'm feeding it until runtime.
  • I need to support LINQ to Objects for the Query() method. Assuming I can come up with a decent way to store the objects, this should be as simple as returning an array of stored objects AsQueryable().

How would you store the objects to meet the above requirements?

like image 552
Chris Avatar asked Mar 31 '11 15:03

Chris


People also ask

How do I create an in memory database?

Use create inmemory database to create an in-memory database, using model or another user database as its template. You can also create temporary databases as in-memory databases that reside entirely in in-memory storage. However, you cannot specify a template database for an in-memory temporary database.

What is in memory repository?

In-memory databases are purpose-built databases that rely primarily on memory for data storage, in contrast to databases that store data on disk or SSDs. In-memory data stores are designed to enable minimal response times by eliminating the need to access disks.

What is in memory database C#?

An in memory database works just like an ordinary database, but the content is stored in memory instead of on disk. This has the effect that all data is lost when the application is shut down. They have to be rebuild and populated with data on each startup.

What is Repository pattern in C#?

The Repository Design Pattern in C# Mediates between the domain and the data mapping layers using a collection-like interface for accessing the domain objects. In other words, we can say that a Repository Design Pattern acts as a middle layer between the rest of the application and the data access logic.


2 Answers

Basics are simple:

public class InMemoryRepository : IRepository
{
    private readonly IList<object> entities = new List<object>();

    public T Get<T>(object id)
    {
        return entities.OfType<T>.SingleOrDefault(e => e.ID == id);
    }

    public IList<T> GetAll<T>()
    {
        return entities.OfType<T>.ToList();
    }

    public IQueryable<T> Query<T>()
    {
        return GetAll<T>.AsQueryable();
    }
}

However, as soon as it comes to public IList<T> GetAll<T>(string queryName), things get complicated.

Potentially you can resort to an SQLite-based repository implementation for your tests.

like image 84
Anton Gogolev Avatar answered Sep 27 '22 16:09

Anton Gogolev


I would go with NHibernate configured for in-memory SqlLite database. You can test then your real code and be sure that everything works correct. Writing mock for Repository can be hard and if you change IRepository interface you will have to reimplement you InMemoryRepository.

For me one of big benefits of having NHibernate is the possibility for using in memory database for testing.

like image 25
Rafal Spacjer Avatar answered Sep 27 '22 15:09

Rafal Spacjer