Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Both approaches are possible and it is only matter of choice. </p> <p>Once you use <code>IQueryable</code> you have simple repository which will work in the most cases but it is worse testable because queries defined on <code>IQueryable</code> are linq-to-entities. If you mock repository they are linq-to-objects in unit tests = you don't test your real implementation. You need integration tests to test your query logic.</p> <p>Once you use <code>IEnumerable</code> you will have very complex public interfaces of your repositories - you will need special repository type for every entity which needs special query exposed on the repository. This kind of repositories was more common with stored procedures - each method on the repository was mapped to single stored procedure. This type of repository provides better separation of concerns and less leaky abstraction but in the same time it removes a lot of ORM and Linq flexibility. </p> <p>For the last you can have combined approach where you have methods returning IEnumerable for most common scenarios (queries used more often) and one method exposing <code>IQueryable</code> for rare or complex dynamically build queries.</p> <p>Edit:</p> <p>As noted in comments using <code>IQueryable</code> has some side effects. When you expose <code>IQueryable</code> you must keep your context alive until you execute the query - <code>IQueryable</code> uses deferred execution in the same way as <code>IEnumerable</code> so unless you call <code>ToList</code>, <code>First</code> or other functions executing your query you still need your context alive.</p> <p>The simplest way to achieve that is using disposable pattern in the repository - create context in its constructor and dispose it when repository disposes. Then you can use <code>using</code> blocks and execute queries inside them. This approach is for very simple scenarios where you are happy with single context per repository. More complex (and common) scenarios require context to be shared among multiple repositories. In such case you can use something like context provider / factory (disposable) and inject the factory to repository constructor (or allow provider to create repositories). This leads to DAL layer factory and custom unit of work.</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