Note that there are some explanatory texts on larger screens.

plurals
  1. POAm I using IRepository correctly?
    text
    copied!<p>I'm looking to use the IRepository pattern (backed by NHibernate, if it matters) in a small project. The domain is a simple one, intentionally so to allow me to focus on understanding the IRepository pattern. The lone domain class is <code>Movie</code>, with properties for <code>Year</code>, <code>Genre</code>, and <code>Title</code>. My intent would be to "get" movies whose properties match criteria of the aforementioned types.</p> <p>Convention seems to be to have a generic <code>IRepository</code> interface, similar to the following:</p> <pre><code>public interface IRepository&lt;T&gt; { T Get(int id); T[] GetAll(); void Add(T item); void Update(T item); void Delete(T item); } </code></pre> <p>With a base implementation:</p> <pre><code>public abstract class Repository&lt;T&gt; : IRepository&lt;T&gt; { public T Get(int id) { ... } public T[] GetAll() { ... } public void Add(T item) { ... } public void Update(T item) { ... } public void Delete(T item) { ... } } </code></pre> <p>Then to have a domain-specific interface:</p> <pre><code>public interface IMovieRepository { Movie[] GetByGenre(Genre genre); Movie[] GetByYear(int year); Movie[] GetByTitle(string title); } </code></pre> <p>With an implementation that also extends the base <code>Repository</code> class:</p> <pre><code>public class MovieRepository : Repository&lt;Movie&gt;, IMovieRepository { public Movie[] GetByGenre(Genre genre) { ... } public Movie[] GetByYear(int year) { ... } public Movie[] GetByTitle(string title) { ... } } </code></pre> <p>I would need to add necessary implementation to the base class as well as the concrete one, using NHibernate, but I would like to know if I am on the right track with this setup.</p> <p>There seems to be a fair bit of overhead for just one domain class, though it would be less noticeable if there were multiple domain classes involved. Right now I'm trying to keep it simple so I can pin down the concept.</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