Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I believe you <strong>really should</strong> add a <code>TEntity GetById(int id)</code> to your <code>IRepository&lt;TEntity&gt;</code> generic interface.</p> <p>Why? Because if you don't, and if you want to fetch a single record on your business layer, you only have two options (on the repository, data-access layer):</p> <ol> <li>Return a complete, "unlazy" collection, meaning that you'll return, say, 100,000 records in order to use a single one.</li> <li>Return a lazy collection like <code>IQueryable&lt;TEntity&gt;</code>, which, yes, will allow you to get a single record from the database, but may cause a lot of <a href="http://www.weirdlover.com/2010/05/11/iqueryable-can-kill-your-dog-steal-your-wife-kill-your-will-to-live-etc/" rel="nofollow noreferrer">nasty side-effects</a>.</li> </ol> <p>The first option is clearly wrong. The second is controversial, but (unless your project has you as a single developer, and you really really really know what you're doing) it is potentially leaky and unsafe. So, if you do need a single record (and sometimes you'll surely do), expose a method that does exactly that.</p> <p>Having said that, you should also <strong>not</strong> expose <code>IQueryable&lt;TEntity&gt; All { get; }</code>, for exactly the same reasons above. Use <code>IEnumerable&lt;TEntity&gt; All { get; }</code> instead, and make your concrete generic repository class return a real collection, by calling <code>context.Set&lt;TEntity&gt;().ToList()</code> for instance.</p> <p><strong>Edit</strong></p> <p>Regarding IDisposable:</p> <p>There are only two (related) reasons for implementing the IDisposable interface that I can think of:</p> <ol> <li>Disposing unmanaged resources</li> <li>A cool way of <a href="http://www.peterprovost.org/blog/post/RAII-and-IDisposable-and-Thoughts-About-OO.aspx" rel="nofollow noreferrer">implementing the RAII pattern</a>.</li> </ol> <p>In your case, you probably <em>should</em> use it on your repository implementation. Please take a look at <a href="https://stackoverflow.com/questions/2975760/should-linq-to-sql-repository-implement-idisposable">this SO question</a> for further information.</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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