patterncsharpMinor
Using C# generics and factory classes to map IDataReader to POCO?
Viewed 0 times
idatareadermapfactorygenericsusingclassesandpoco
Problem
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 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(reader);
}
}
public class CodeFactory : IFactory
{
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.");
}
}
}Solution
Most of it looks good. That is until I get to your catch statement:
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:
This way, you do not lose what cause the original exception.
Two suggestions:
you are using standard naming conventions, which you are.
-
Use the object initializer format when creating objects:
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:
- don't use the this. statement, I find it overly clutters code up if
you are using standard naming conventions, which you are.
-
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
}Code Snippets
catch (Exception)
{
throw new Exception("Error creating Code objects from reader.");
}catch (Exception ex)
{
throw new InvalidCodeDataException("Error creating Code objects from reader.", ex);
}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
}Context
StackExchange Code Review Q#15583, answer score: 6
Revisions (0)
No revisions yet.