Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to set up an in-memory repository
    text
    copied!<p>I have the following class:</p> <pre><code>public class InMemoryRepository : IRepository { public void Add(object entity) { throw new NotImplementedException(); } public void Attach(object Entity) { throw new NotImplementedException(); } public T Get&lt;T&gt;(object id) { throw new NotImplementedException(); } public IList&lt;T&gt; GetAll&lt;T&gt;(string queryName) { throw new NotImplementedException(); } public IList&lt;T&gt; GetAll&lt;T&gt;() { throw new NotImplementedException(); } public IQueryable&lt;T&gt; Query&lt;T&gt;() { throw new NotImplementedException(); } public void Remove(object entity) { throw new NotImplementedException(); } public void Save(object entity) { throw new NotImplementedException(); } } </code></pre> <p>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?</p> <p>Some key points I'm having a hard time addressing:</p> <ul> <li>The repository methods themselves are generic, so I need some mechanism for automatically storing and referencing different types. <code>Get&lt;TestEntity&gt;(object id)</code> 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.</li> <li>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().</li> </ul> <p>How would you store the objects to meet the above requirements?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload