Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should not have other repository interfaces besides your generic <code>IRepository&lt;T&gt;</code>. If you need those, you are missing an abstraction.</p> <p>For instance, a common reason for people to have custom repository interfaces is because they have a custom query that some repository has, while other don't. For instance:</p> <pre><code>public interface IEmployeeRepository : IRepository&lt;Employee&gt; { Employee GetEmployeeOfTheMonth(int month); } </code></pre> <p>The problem here is that the <code>IEmployeeRepository</code> is abused for a 'custom query'. Custom queries deserve their own (generic) abstraction:</p> <pre><code>// Defines a query public interface IQuery&lt;TResult&gt; { } // Defines the handler that will execute queries public interface IQueryHandler&lt;TQuery, TResult&gt; where TQuery : IQuery&lt;TResult&gt; { TResult Handle(TQuery query); } </code></pre> <p>With this abstraction we can add custom queries to the system, without the need of creating <code>IRepository&lt;T&gt;</code> derivatives:</p> <pre><code>public class GetEmployeeOfTheMonthQuery : IQuery&lt;Employee&gt; { [Range(1, 12)] public int Month { get; set; } } class GetEmployeeOfTheMonthHandler : IQueryHandler&lt;GetEmployeeOfTheMonthQuery, Employee&gt; { public Employee Handle(GetEmployeeOfTheMonthQuery query) { // todo: query the database, web service, disk, what ever. } } </code></pre> <p>A consumer that needs to know the employee of the month, can now simply take a dependency on <code>IQueryHandler&lt;GetEmployeeOfTheMonthQuery, Employee&gt;</code> and execute the query as follows:</p> <pre><code>var query = new GetEmployeeOfTheMonthQuery { Month = 11 }; var employee = this.employeeOfMonthHandler.Handle(query); </code></pre> <p>This might seem like overhead, but this model is very flexible, scalable, and has many interesting benefits. For instance, it is very easy to add cross-cutting concerns by wrapping handlers with decorators.</p> <p>This also allows our reposities to be hidden behind one generic interface, which allows us to easily batch register them at once and add decorators to them as well.</p> <p>For more in depth information, read this article: <a href="https://cuttingedge.it/blogs/steven/pivot/entry.php?id=92" rel="nofollow noreferrer">Meanwhile… on the query side of my architecture</a>.</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