architecture - Which is a better approach to load data from reader into objects -


have @ below code load list

while (datareader.read())             {                 city city;                 if (city.tryload(datareader, out city))                 {                     defaultcities.add(city);                 }             } 

tryload reads reader , loads dataobject , returns true when successful , false when unsuccessful. benefit of this, told, code not throw error if code fails reason when loading object. if 1 row of data corrupt, not added default connection. moreover, in try load can log particular row threw , error , fix that.

on other hand approach followed earlier loading objects , adding them collection.

 while (datareader.read())                 {                     city city = new city();                     city.name = reader["name"].tostring();                     .                     .                     defaultcities.add(city)                  } 

while second approach may fail due corrupt value in database, wouldn't want that? wont catching bugs due missing value become difficult in first approach?

just wanted opinion of others on pros cons of 2 approaches.

also, please in tagging question appropriately.

if can permit exception thrown on bad data, second approach best. common in situations administrators have control of quality of data.

however, it's true administrators can't guarantee quality of data. in these cases, it's requirement application handle malformed data gracefully. when faced need, first approach elegant way that.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -