Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To address only the question about redundancy, without commenting on the whole context-per-object philosophy or Rick's code, which I haven't reviewed,</p> <p>You can eliminate the two lines with</p> <pre><code>var customer = new Customer().GetCustomer(primaryKeyId); </code></pre> <p>Alternatively, create a factory that acts as a static gateway:</p> <pre><code>public static class CustomerFactory { public static Customer BuildCustomerWithId(_&lt;int/short/long&gt;_ primaryKeyId) { var customer = new Customer(); return customer.GetCustomer(primaryKeyId); } } </code></pre> <p>Not only does this clean up your object creation (now you just call <code>var customer = CustomerFactory.BuildCustomerWithId(1);</code>), but now you can modify the <code>BuildCustomerWithId()</code> method, if necessary, without changing the consuming classes. For example, you might later decide you don't like instantiating the DataContext in the business objects, and you refactor it all out. You can instantiate the DataContext in the factory instead, and you don't have to change any code that calls <code>BuildCustomerWithId()</code>. Static gateway makes unit testing slightly more challenging, but still much easier than instantiating <code>Customer</code> objects directly.</p> <p>I realize that this doesn't solve the problem of first instantiating and then calling the getter method, but frankly, I don't see that as a problem from a performance standpoint -- only in terms of syntax/readability.</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