3
\$\begingroup\$

I'm in the process of doing some mapping of POCOs via IDataReader. I'd love to hear thoughts on the approach and if they are any good ways to improve what I have done.

public class Code
{
 public int Id { get; set; }
 public string Type { get; set; }
 public string Value { get; set; }
 public string Description { get; set; }
}
public class CodeRepository : GenericRepository
{
 public CodeRepository(IDbConnection connection) : base(connection) { }
 public IEnumerable<Code> GetCodesForDemographics()
 {
 var sql = @"
 Select 
 CODE_TYPE, 
 CODE, 
 DESCRIPTION
 From 
 Codes
 Where 
 CODE_TYPE In ('language', 'lang_service', 'transfer', 'admit_type', 'Value', 
 'admit_src', 'state', 'county', 'ethnic', 'race', 
 'exempt_unit_code', 'alt_care_type_code')";
 using (var manager = new DbCommandManager(this.Connection, sql))
 {
 using (var reader = manager.GetReader())
 return DbAutoMapper.MapReaderToList<Code, CodeFactory>(reader);
 }
 }
public class CodeFactory : IFactory<Code>
{
 public Code CreateTFromReader(IDataReader reader)
 {
 try
 {
 var code = new Code();
 code.Type = (reader["CODE_TYPE"] as string) ?? String.Empty;
 code.Description = (reader["DESCRIPTION"] as string) ?? String.Empty;
 code.Value = (reader["CODE"] as string) ?? String.Empty;
 return code;
 }
 catch (Exception)
 {
 throw new Exception("Error creating Code objects from reader.");
 }
 }
}
asked Sep 13, 2012 at 17:24
\$\endgroup\$
2
  • \$\begingroup\$ I agree with the posted answer except you should only catch specific exceptions or at best ApplicationException. Wrapping a System exception such as insufficient permissions or an OutOfMemoryException as an inner exception obscures what is really going on. \$\endgroup\$ Commented Sep 15, 2012 at 20:53
  • \$\begingroup\$ How would you handle collections as mentioned in stackoverflow.com/questions/20699004/grouping-a-collection? \$\endgroup\$ Commented Dec 20, 2013 at 8:27

1 Answer 1

6
\$\begingroup\$

Most of it looks good. That is until I get to your catch statement:

catch (Exception)
{
 throw new Exception("Error creating Code objects from reader.");
}

really does not explain why you are having. I would suggest either creating your own exception i.e. InvalidCodeDataException, or throwing an ApplicationException. Either of these should take the original exception as an inner exception:

catch (Exception ex)
{
 throw new InvalidCodeDataException("Error creating Code objects from reader.", ex);
}

This way, you do not lose what cause the original exception.

Two suggestions:

  1. don't use the this. statement, I find it overly clutters code up if you are using standard naming conventions, which you are.
  2. Use the object initializer format when creating objects:

    return new Code
     {
     Type = (reader["CODE_TYPE"] as string) ?? String.Empty,
     Description = (reader["DESCRIPTION"] as string) ?? String.Empty,
     Value = (reader["CODE"] as string) ?? String.Empty
     }
    
answered Sep 13, 2012 at 17:56
\$\endgroup\$
1
  • \$\begingroup\$ Good feedback. I appreciate it. Always trying to learn. : ) \$\endgroup\$ Commented Sep 13, 2012 at 18:59

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.