debugcsharpMinor
Is there a better way to test for the exception conditions?
Viewed 0 times
theexceptionwaybetterfortestconditionsthere
Problem
This method assigns the value of a specified column in a
I have 3 conditionals handling exceptions, and I want these conditions to throw exceptions.
The final condition just allows the use of a conversion method if it exists. Is there a better way to test for and throw the exceptions?
Incorporating @Leonid and @SmartLemons comments/answers into consideration, I changed it to this:
```
public virtual void Map(T obj, DataRow row)
{
PropertyInfo property = obj.GetType().GetProperty(this.PropertyName);
ValidateArguments(property, row);
var value = row.IsNull(this.ColumnName) ? row[this.Colu
DataRow to specified property in an object. I have 3 conditionals handling exceptions, and I want these conditions to throw exceptions.
The final condition just allows the use of a conversion method if it exists. Is there a better way to test for and throw the exceptions?
public class ColumnToPropertyMap
{
public string ColumnName { get; private set; }
public string PropertyName { get; private set; }
private Func conversion;
public virtual void Map(T obj, DataRow row)
{
if (!row.Table.Columns.Contains(this.ColumnName))
{
throw new ArgumentException(
String.Format(@"Column ""{0}"" does not exist in the row.", this.ColumnName));
}
PropertyInfo property = obj.GetType().GetProperty(this.PropertyName);
if (property == null)
{
throw new ArgumentException(String.Format(@"Property ""{0}"" does not exist for object.", this.PropertyName));
}
var value = row.IsNull(this.ColumnName) ? row[this.ColumnName].GetDefaultValue(typeof(colType)) : row[this.ColumnName];
if (conversion == null && property.PropertyType != row.Table.Columns[this.ColumnName].DataType)
{
throw new InvalidCastException(
string.Format(@"Mapping for {0} to {1} failed. Unable to convert {2} to {3}.", this.ColumnName, this.PropertyName, value.GetType(), property.PropertyType));
}
if (conversion != null)
{
value = conversion((colType)value);
}
property.SetValue(obj, value, null);
}
}Incorporating @Leonid and @SmartLemons comments/answers into consideration, I changed it to this:
```
public virtual void Map(T obj, DataRow row)
{
PropertyInfo property = obj.GetType().GetProperty(this.PropertyName);
ValidateArguments(property, row);
var value = row.IsNull(this.ColumnName) ? row[this.Colu
Solution
Your code is as simple as it can get... but if you are wanting to clean up the method then you could do something like this to make it look nicer.
public string ColumnName { get; private set; }
public string PropertyName { get; private set; }
private Func conversion;
public virtual void Map(T obj, DataRow row)
{
Conditions.Check1(row, this.ColumnName).argerror(String.Format(@"Column ""{0}"" does not exist in the row.", this.ColumnName));
PropertyInfo property = obj.GetType().GetProperty(this.PropertyName);
Conditions.Check2(property).argerror(String.Format(@"Property ""{0}"" does not exist for object.", this.PropertyName));
var value = row.IsNull(this.ColumnName) ? row[this.ColumnName].GetDefaultValue(typeof(colType)) : row[this.ColumnName];
Conditions.Check3(conversion, property, row, this.ColumnName).casterror(string.Format(@"Mapping for {0} to {1} failed. Unable to convert {2} to {3}.", this.ColumnName, this.PropertyName, value.GetType(), property.PropertyType));
if (conversion != null)
{
value = conversion((colType)value);
}
property.SetValue(obj, value, null);
}
public static class Conditions
{
public class Error
{
private bool error = false;
public bool isError { get {return error;}}
public Error(bool r)
{
error = r;
}
public bool argerror(string message)
{
if (error)
throw new ArgumentException(message);
return error;
}
public bool casterror(string message)
{
if (error)
throw new InvalidCastException(message);
return error;
}
}
public static Error Check1(dynamic a, string cn)
{
return new Error(a.Table.Columns.Contains(cn));
}
public static Error Check2(dynamic a)
{
return new Error(a == null);
}
public static Error Check3(dynamic a, dynamic b, dynamic c, string cn)
{
return new Error(a == null && b.PropertyType != c.Table.Columns[cn].DataType);
}
}Code Snippets
public string ColumnName { get; private set; }
public string PropertyName { get; private set; }
private Func<colType, propType> conversion;
public virtual void Map<T>(T obj, DataRow row)
{
Conditions.Check1(row, this.ColumnName).argerror(String.Format(@"Column ""{0}"" does not exist in the row.", this.ColumnName));
PropertyInfo property = obj.GetType().GetProperty(this.PropertyName);
Conditions.Check2(property).argerror(String.Format(@"Property ""{0}"" does not exist for object.", this.PropertyName));
var value = row.IsNull(this.ColumnName) ? row[this.ColumnName].GetDefaultValue(typeof(colType)) : row[this.ColumnName];
Conditions.Check3(conversion, property, row, this.ColumnName).casterror(string.Format(@"Mapping for {0} to {1} failed. Unable to convert {2} to {3}.", this.ColumnName, this.PropertyName, value.GetType(), property.PropertyType));
if (conversion != null)
{
value = conversion((colType)value);
}
property.SetValue(obj, value, null);
}
public static class Conditions
{
public class Error
{
private bool error = false;
public bool isError { get {return error;}}
public Error(bool r)
{
error = r;
}
public bool argerror(string message)
{
if (error)
throw new ArgumentException(message);
return error;
}
public bool casterror(string message)
{
if (error)
throw new InvalidCastException(message);
return error;
}
}
public static Error Check1(dynamic a, string cn)
{
return new Error(a.Table.Columns.Contains(cn));
}
public static Error Check2(dynamic a)
{
return new Error(a == null);
}
public static Error Check3(dynamic a, dynamic b, dynamic c, string cn)
{
return new Error(a == null && b.PropertyType != c.Table.Columns[cn].DataType);
}
}Context
StackExchange Code Review Q#23742, answer score: 2
Revisions (0)
No revisions yet.