Note that there are some explanatory texts on larger screens.

plurals
  1. POAdvantage of creating a generic repository vs. specific repository for each object?
    primarykey
    data
    text
    <p>We are developing an ASP.NET MVC application, and are now building the repository/service classes. I'm wondering if there are any major advantages to creating a generic IRepository interface that all repositories implement, vs. each Repository having its own unique interface and set of methods.</p> <p>For example: a generic IRepository interface might look like (taken from <a href="https://stackoverflow.com/questions/291344/repository-pattern-vs-dal">this answer</a>):</p> <pre><code>public interface IRepository : IDisposable { T[] GetAll&lt;T&gt;(); T[] GetAll&lt;T&gt;(Expression&lt;Func&lt;T, bool&gt;&gt; filter); T GetSingle&lt;T&gt;(Expression&lt;Func&lt;T, bool&gt;&gt; filter); T GetSingle&lt;T&gt;(Expression&lt;Func&lt;T, bool&gt;&gt; filter, List&lt;Expression&lt;Func&lt;T, object&gt;&gt;&gt; subSelectors); void Delete&lt;T&gt;(T entity); void Add&lt;T&gt;(T entity); int SaveChanges(); DbTransaction BeginTransaction(); } </code></pre> <p>Each Repository would implement this interface, for example:</p> <ul> <li>CustomerRepository:IRepository</li> <li>ProductRepository:IRepository</li> <li>etc.</li> </ul> <p>The alternate that we've followed in prior projects would be:</p> <pre><code>public interface IInvoiceRepository : IDisposable { EntityCollection&lt;InvoiceEntity&gt; GetAllInvoices(int accountId); EntityCollection&lt;InvoiceEntity&gt; GetAllInvoices(DateTime theDate); InvoiceEntity GetSingleInvoice(int id, bool doFetchRelated); InvoiceEntity GetSingleInvoice(DateTime invoiceDate, int accountId); //unique InvoiceEntity CreateInvoice(); InvoiceLineEntity CreateInvoiceLine(); void SaveChanges(InvoiceEntity); //handles inserts or updates void DeleteInvoice(InvoiceEntity); void DeleteInvoiceLine(InvoiceLineEntity); } </code></pre> <p>In the second case, the expressions (LINQ or otherwise) would be entirely contained in the Repository implementation, whoever is implementing the service just needs to know which repository function to call. </p> <p>I guess I don't see the advantage of writing all the expression syntax in the service class and passing to the repository. Wouldn't this mean easy-to-messup LINQ code is being duplicated in many cases? </p> <p>For example, in our old invoicing system, we call</p> <pre><code>InvoiceRepository.GetSingleInvoice(DateTime invoiceDate, int accountId) </code></pre> <p>from a few different services (Customer, Invoice, Account, etc). That seems much cleaner than writing the following in multiple places:</p> <pre><code>rep.GetSingle(x =&gt; x.AccountId = someId &amp;&amp; x.InvoiceDate = someDate.Date); </code></pre> <p>The only disadvantage I see to using the specific approach is that we could end up with many permutations of Get* functions, but this still seems preferable to pushing the expression logic up into the Service classes.</p> <p>What am I missing?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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