索引器的扩展方法,它们会好吗?

jud*_*dek 23 c# methods indexer data-access-layer

索引器的扩展方法,它们会好吗?

我正在玩一些重新补充POCO的代码.

代码迭代从SqlDataReader返回的行,并使用反射从列值分配属性.在我的调用堆栈中,我有一个像这样的代码: -

poco.Set("Surname", "Smith"); // uses extension method ...
Run Code Online (Sandbox Code Playgroud)

Set方法被编写为扩展方法.

能够编写这样的代码会很棒

poco["Surname"] = "Smith";  // extension methods for indexers ?
Run Code Online (Sandbox Code Playgroud)

即我想为索引器编写扩展方法

有没有充分的理由说.Net没有索引器的扩展方法?其他人对扩展方法索引器有其他好的用途吗?

如果我们可以为索引器编写扩展方法,那么我们可以编写这样的代码......

var poco = PocoFactory();  
    poco.Surname = “Smith”; // is this JavaScript ...
    poco[Surname] = “Smith” ; // … or is this c# or both
Run Code Online (Sandbox Code Playgroud)

我的代码中的一些片段

/////////////////////////////////////////////
// Client calling code
IDab dab = DabFactory.Create( "Northwind" );
string sql = @"select * from Customers ";
var persons = dab.ExecuteReader<NorthwindCustomer>(sql);
if (dab != null{
   Assert.That(persons[0].CustomerID , Is.EqualTo("ALFKI"));}
/////////////////////////////////////////////
List<T> IDab.ExecuteReader<T>(string commandText) 
{
    List<T> pocos = new List<T>();
    // setup connection
    SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
    while (reader.Read())
    {
            Dictionary<string, int> colMappings = null ;
            if (colMappings == null){
                colMappings = reader.GetSqlDataReaderColumnMappings();}
            T poco = new T();
            poco.DbToMem<T>(reader, colMappings);
            pocos.Add(poco);
        }
    }
    // connection cleanup ...
    return pocos ;
}

// the set extension method signature
public static void Set<T>(this T thisClientObject, string thisPropertyName, object newValue) where T : class
Run Code Online (Sandbox Code Playgroud)

Mar*_*ell 12

索引器与属性共享许多共性(在引擎盖下,索引器具有索引的属性),并且扩展属性不存在.同意,有些情况下它们很方便.

重新提出的情景 - 在某些方面,这很像dynamic.当然,如果你声明一个具有字符串索引器的接口,那么你的读者代码可以直接使用它 - 但重复实现这个接口将是很多不必要的工作!

重新扩展方法; 这是否使用定期反射?你可能想看一些技巧HyperDescriptor,如果你做了很多这样的话可能会节省大量的CPU时间.那么典型的用法是:

PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));
while (reader.Read())
{
     T poco = new T();
     // abbreviated...
     (per prop)
        props[propName].SetValue(poco, cellValue);
}
Run Code Online (Sandbox Code Playgroud)

您可以通过首先查看返回的列(每个网格一次,而不是每行)来进一步优化,并且只访问匹配的列...

或者,看看ORM工具; Expression也可以用来做数据读取(我在usenet上有一个完整的例子,用于DbLinq)