Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I would rethink your Entity Framework model.</p> <p>That domain model you have provided looks perfect for <strong>Table-per-type Inheritance</strong>.</p> <p>Then you could use the LINQ <code>.OfType&lt;T&gt;()</code> method to filter different transaction types, based on a generic type parameter on your Repository:</p> <pre><code>public class TransactionRepository : IRepository&lt;Transaction&gt; { public TTransaction Find&lt;TTransaction&gt;(int transactionId) where TTransaction : TransactionBase, new() { return _ctx.Transactions.OfType&lt;TTransaction&gt;().SingleOrDefault(tran =&gt; tran.TransactionId == transactionId); } } </code></pre> <p>Then you could do this:</p> <pre><code>var ccTran = _repository.Find&lt;CreditCardTransaction&gt;(x =&gt; x.TransactionId == 1); var cTran = _repository.Find&lt;CheckTransaction&gt;(x =&gt; x.TransactionId == 2); </code></pre> <p>As for your questions:</p> <blockquote> <p>So, the client should be able to search on CardNumber, RoutingNumber, Amount, etc., all from one method</p> </blockquote> <p>I don't think that's possible with one method, well certainly not without some kind of if/switch statement. </p> <p>The kicker is the filtering - it all comes down to the signature of the repository method - what is provided, what generic constraints, etc.</p> <p>If your saying each sub-type has it's own repository, then does it really make sense to have a single method which serves all three repositories? Where should this magical method live?</p> <p>So overall, i think you've reached the point many have reached, where your domain is fighting against Entity Framework.</p> <p>Basically, if your working on an objectset of type AbstractA, you can't "downcast" to a objectset of type DerivedA in order to do the filtering.</p> <p>It comes down to how much of your domain model your willing to compromise. I myself had a similar problem, and i ended up using TPT inheritance (then switching to TPH because the performance was better).</p> <p>So without knowing too much about your domain other than what you've mentioned, i think you need to re-think the statement "Each sub-class has it's own repository since each has its own properties and way of being fetched".</p> <p>It looks like you should have a single repository, TPT/TPH in your EF model, and the "search" method on your Repository with a generic type constraint on the transaction type.</p> <p>If your gun-ho on having the magic single method, you would need a nasty switch/if statement, and delegate the filtering to a specific method.</p>
 

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