Do you have the solution?

Given that System.Data.IDataRecord and System.Data.DataRow both share the following accessors:

IDataRecord Interface

object this[ string name ] { get; }
object this[ int i ] { get; }

DataRow

public object this[ int columnIndex ] { get; set; }
public object this[ string columnName ] { get; set; }

When I'm building my DAL objects, I invariably have to build two constructors for my objects--one which takes the IDataRecord interface, and another which takes the DataRow object.  The constructors are usually identical except for the type of the argument being passed to it. If a common interface could be extracted and shared between the DataReader and DataRow I'd only have to use one constructor.

I can see that the primary problem facing interface extraction is the fact that the DataReader is readonly, and the DataRow is read/write. The only way I see around this problem is if the compiler treated a read/write property as satisfying the Readonly condition of the interface.  Or maybe we need a new scope term for Interfaces, such as "CanRead" and "CanWrite". A standard read/write property and a readonly property would satisfy the CanRead scope modifier. Likewise for the CanWrite portion. In other words, the Interface that whatever access level is given to the property, it must be Read-able, or Write-able, or both even.

Has anyone found a way around my core problem? What do you think the best way to solve it in future versions of the framework would be?

Published Friday, July 29, 2005 2:00 PM by taganov
Filed under:

Comments

# re: Do you have the solution?

Friday, July 29, 2005 2:18 PM by Ayende Rahien

You can define an interface that just have the accessors and use something like this to use it:
http://www.ayende.com/Blog/DynamicCasterIsDone.aspx

# re: Do you have the solution?

Friday, July 29, 2005 3:30 PM by Frans Bouma

object[] values = new object[datareader.FieldCount];
datareader.GetValues(values);

then in your bo, pass in the values array, and use datarow.ItemArray = values;
to set the values all at once.

it can't get any faster than that. Positive side effect: you don't pass around the datareader reference.

# re: Do you have the solution?

Friday, April 11, 2008 10:36 AM by Memmorium

     Good idea!

P.S. A U realy girl?

Leave a Comment

(required) 
(required) 
(optional)
(required)