Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>@Alex - i know this is an old question, but what I would be doing would be letting the Repository do <em>really simple stuff</em> only. This means, get all records for a table or view.</p> <p>Then, in the SERVICES layer (you are using an n-tiered solution, right? :) ) i would be handling all the 'special' query stuff there.</p> <p>Ok, example time.</p> <h3>Repository Layer</h3> <pre><code>ContactRepository.cs public IQueryable&lt;Contact&gt; GetContacts() { return (from q in SqlContext.Contacts select q).AsQueryable(); } </code></pre> <p>Nice and simple. <code>SqlContext</code> is the instance of your <code>EF Context</code> .. which has an <code>Entity</code> on it called <code>Contacts</code> .. which is basically your sql Contacts class.</p> <p>This means, that method basically is doing: <code>SELECT * FROM CONTACTS</code> ... but it's not hitting the database with that query .. it's only a query right now.</p> <p>Ok .. next layer.. <strong><em>KICK</em></strong> ... up we go (<em>Inception</em> anyone?)</p> <h3>Services Layer</h3> <pre><code>ContactService.cs public ICollection&lt;Contact&gt; FindContacts(string name) { return FindContacts(name, null) } public ICollection&lt;Contact&gt; FindContacts(string name, int? year) { IQueryable&lt;Contact&gt; query = _contactRepository.GetContacts(); if (!string.IsNullOrEmpty(name)) { query = from q in query where q.FirstName.StartsWith(name) select q; } if (int.HasValue) { query = from q in query where q.Birthday.Year &lt;= year.Value select q); } return (from q in query select q).ToList(); } </code></pre> <p>Done.</p> <p>So lets recap. First, we start our with a simple '<em>Get everything from contacts</em>' query. Now, if we have a name provided, lets add a filter to filter all contacts by name. Next, if we have a year provided, then we filter the birthday by Year. Etc. Finally, we then hit the DB (with this modified query) and see what results we get back.</p> <p>NOTES:-</p> <ul> <li>I've omitted any Dependency Injection for simplicity. It's more than highly recommended.</li> <li>This is all pseduo-code. Untested (against a compiler) but you get the idea ....</li> </ul> <h3>Takeaway points</h3> <ul> <li>The Services layer handles all the smarts. That is where you decide what data you require.</li> <li>The Repository is a simple SELECT * FROM TABLE or a simple INSERT/UPDATE into TABLE.</li> </ul> <p>Good luck :)</p>
    singulars
    1. This table or related slice is empty.
    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