如何设置内存仓库 [英] How to set up an in-memory repository

查看:104
本文介绍了如何设置内存仓库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类:

  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>查询< T>()
{
throw new NotImplementedException();
}

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

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

我们的默认存储库实现使用NHibernate作为后备存储,但是我想实现一个内存版本,所以我可以对域对象进行原型化,而无需创建一个支持的SQL数据库。假设所有对象都有一个Id属性作为主键的约定,你将如何实现一个通用内存存储?



我有一个难点时间寻址:




  • 存储库方法本身是通用的,因此我需要一些自动存储和引用不同类型的机制。 Get< TestEntity>(object id)应该能够查询TestEntity的所有存储的实例,并找到具有匹配的Id属性的实例,但是我无法定义一个TestEntity对象直接,因为存储库将不知道我正在运行什么类型,直到运行时。

  • 我需要支持用于Query()方法的LINQ to Objects。假设我可以想出一个正确的方法来存储对象,这应该是返回一组存储对象AsQueryable()的简单。



解决方案

基础很简单:

  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>查询< T>()
{
返回GetAll< T> .AsQueryable();
}
}

但是,一旦达到 public IList< T> GetAll< T>(string queryName),事情变得复杂。



潜在的可能你可以使用基于SQLite的存储库实现来进行测试。


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?

解决方案

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.

这篇关于如何设置内存仓库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆