如何设置内存存储库
我有以下类:
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();
}
}
我们的默认存储库实现使用 NHibernate 作为后备存储,但我想实现它的内存版本,这样我就可以对域对象进行原型设计,而无需创建后备 SQL 数据库。假设所有对象都有一个 Id 属性作为主键,您将如何为此实现通用内存存储?
我很难解决一些关键点:
- 存储库方法本身是通用的,因此我需要某种机制来自动存储和引用不同类型。
Get
应该能够查询 TestEntity 的所有存储实例并找到具有匹配 Id 属性的实例,但我无法直接定义 TestEntity 对象的集合,因为直到运行时,存储库才会知道我正在向它提供什么类型。(object id) - 我需要支持 LINQ to Objects 的 Query() 方法。假设我可以想出一种存储对象的好方法,这应该像返回存储对象的数组 AsQueryable() 一样简单。
您将如何存储对象来满足上述要求?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
基础知识很简单:
但是,一旦涉及
public IList, GetAll(string queryName)
,事情变得复杂。您可能可以使用基于 SQLite 的存储库实现来进行测试。
Basics are simple:
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.
我会选择为 内存 SqlLite 数据库 配置 NHibernate。然后您可以测试您的真实代码并确保一切正常。为 Repository 编写模拟可能很困难,如果更改 IRepository 接口,则必须重新实现 InMemoryRepository。
对我来说,使用 NHibernate 的一大好处是可以使用内存数据库进行测试。
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 youInMemoryRepository
.For me one of big benefits of having NHibernate is the possibility for using in memory database for testing.
通过安东的回答,我能够修复我自己的 InMemoryRepository。我已经修改它以匹配问题中的类:
With Anton's answer I was able to fix my own InMemoryRepository. I have modified it to match the class in the question:
这是基于 DbSet 的假存储库的实现,包括通过主键查找:
http://refactorthis.wordpress.com/2011/11/30/generic-repository-fake-idbset-implementation-update-find-method-identity-key /
Here is an implementation of fake repository based on DbSet, including find by primary keys:
http://refactorthis.wordpress.com/2011/11/30/generic-repository-fake-idbset-implementation-update-find-method-identity-key/