Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have seen systems that use Reflection and attributes on Properties or fields to maps DataReaders to objects. (A bit like what LinqToSql does.) They save a bit of typing and may reduce the number of errors when coding for DBNull etc. Once you cache the generated code they can be faster then most hand written code as well, so do <em>consider</em> the “high road” if you are doing this a lot.</p> <p>See <a href="http://www.simple-talk.com/dotnet/.net-framework/a-defense-of-reflection-in-.net/" rel="noreferrer">"A Defense of Reflection in .NET"</a> for one example of this. </p> <p>You can then write code like</p> <pre><code>class CustomerDTO { [Field("id")] public int? CustomerId; [Field("name")] public string CustomerName; } </code></pre> <p>...</p> <pre><code>using (DataReader reader = ...) { List&lt;CustomerDTO&gt; customers = reader.AutoMap&lt;CustomerDTO&gt;() .ToList(); } </code></pre> <p>(AutoMap(), is an extension method)</p> <hr> <p>@Stilgar, thanks for a <strong>great</strong> comment</p> <p>If are <strong>able to</strong> you are likely to be better of using <a href="https://stackoverflow.com/questions/1377236/nhibernate-entity-framework-active-records-or-linq2sql/">NHibernate, EF or Linq to Sql, etc</a> However on old project (or for other (sometimes valid) reasons, e.g. “not invented here”, “love of stored procs” etc) It is not always possible to use a ORM, so a lighter weight system can be useful to have “up your sleeves”</p> <p>If you every needed too write lots of IDataReader loops, you will see the benefit of reducing the coding (and errors) <strong>without having to change the architecture</strong> of the system you are working on. That is not to say it’s a good architecture to start with..</p> <p>I am assuming that CustomerDTO will not get out of the data access layer and composite objects etc will be built up by the data access layer using the DTO objects.</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