Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to search sub-classes using the repository pattern?
    primarykey
    data
    text
    <p>I have several sub-classes of a payment transaction base type (credit card, check, cash, billMeLater, etc). Each sub-class has it's own repository since each has its own properties and way of being fetched. I need to be able to search payment transactions and I've gone down a road that has ended up causing more headaches. The trick is that sometimes the client needs to search on common properties like amount or customer name AND sometimes the client needs to search on payment-type-specific properties like credit card number or bank routing number... but the domain-level search method needs to be able to return all types of the transaction base.</p> <p>I have the following levels of abstraction:</p> <ul> <li><p>WCF Layer with a SearchTransactions() method.</p></li> <li><p>Domain layer with a SearchTransactions() method.</p></li> <li><p>Data layer with multiple repositories, each with a Search() method (payment type specific).</p></li> <li><p>Database (via EF) with typical DBA-required unintelligible mess</p></li> </ul> <p>How would you do this?</p> <p><strong>EDIT:</strong></p> <p>For added context, here are some examples of possible payment types and their base:</p> <pre><code>public abstract class TransactionBase { public int TransactionId { get; set; } public decimal Amount { get; set; } } public class CreditCardTransaction : TransactionBase { public string CardNumber { get; set; } public int ExpirationMonth { get; set; } public int ExpirationYear { get; set; } } public class CheckTransaction : TransactionBase { public string BankAccountNumber { get; set; } public string RoutingNumber { get; set; } } </code></pre> <p>So, the client should be able to search on CardNumber, RoutingNumber, Amount, etc., all from one method. If the client searches on Amount (param on the base), the method should return both CreditCardTransaction's and CheckTransaction's. If the client searches on BankAccountNumber, it should only return CheckTransactions.</p> <p><strong>CLIENT REQUIREMENTS and EARLIER SOLUTION:</strong></p> <p>The client requires that there be one call to search multiple transaction types. The client doesn't care much what they pass in as arguments unless they require more than one call to cover all payment types. One idea I had earlier on was to use classes that carried search criteria. Then I could have sub-classes of the search criteria classes that searched the more specific payment type properties. Like this:</p> <pre><code>public class TransactionSearchCriteriaBase { public int TransactionId { get; set; } public decimal Amount { get; set; } } public class CreditCardTransactionSearchCriteria : TransactionSearchCriteriaBase { public string CardNumber { get; set; } public int ExpirationMonth { get; set; } public int ExpirationYear { get; set; } } </code></pre> <p>So, if the client wants to search on common properties like Amount, they pass in the TransactionSearchCriteriaBase. If they pass in the CreditCardTransactionSearchCriteria, they end up searching credit card transactions. For instance:</p> <pre><code>var listOfTransactions = _transactionService.Search(new CreditCardTransactionSearchCriteria{ Amount = 10m, CardNumber = "1111" }); </code></pre> <p>I replaced the almost-inevitable switch/if block with a repository factory that handed back a list of applicable repositories based on the type of criteria object passed in to the factory. </p> <p>The rabbit hole goes deeper. I'd like less of a rabbit hole. </p> <p><strong>ANOTHER PIECE OF INFO:</strong></p> <p>Since we're doing this in EF 3.5, we don't have POCO support. So, we don't consider the objects that EF generates as domain objects. Our repositories map the various disconnected EF objects to domain objects and returns them to the domain services that call them. </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. 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