简单的数据库表到对象的ORM映射

2023/7/6 10:13:31

根据微软企业库中数据访问模块改造的一个对象映射类,针对DataRow进行扩展,提供方便的DataTable到对象集合的映射。

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data;

public static class DataRowExtensions { ///

/// 通过数据行创建一个对象 /// public static T MapToObject(this DataRow dataRow) { Type type = typeof(T); T convertedObject = System.Activator.CreateInstance(); MapToObject(dataRow, convertedObject); return convertedObject; }

</span><span style="background: white; color: gray;">/// <summary>
/// </span><span style="background: white; color: green;">通过行集合创建一个对象列表
</span><span style="background: white; color: gray;">/// </summary>
</span><span style="background: white; color: blue;">public static </span><span style="background: white; color: #2b91af;">List</span><span style="background: white; color: black;"><T> MapToList<T>(</span><span style="background: white; color: blue;">this </span><span style="background: white; color: #2b91af;">DataRowCollection </span><span style="background: white; color: black;">dataRowCollection)
{
    </span><span style="background: white; color: #2b91af;">Type </span><span style="background: white; color: black;">type = </span><span style="background: white; color: blue;">typeof</span><span style="background: white; color: black;">(T);
    </span><span style="background: white; color: #2b91af;">List</span><span style="background: white; color: black;"><T> objects = </span><span style="background: white; color: blue;">new </span><span style="background: white; color: #2b91af;">List</span><span style="background: white; color: black;"><T>();
    </span><span style="background: white; color: blue;">foreach </span><span style="background: white; color: black;">(</span><span style="background: white; color: #2b91af;">DataRow </span><span style="background: white; color: black;">dataRow </span><span style="background: white; color: blue;">in </span><span style="background: white; color: black;">dataRowCollection)
    {
        objects.Add(MapToObject<T>(dataRow));
    }
    </span><span style="background: white; color: blue;">return </span><span style="background: white; color: black;">objects;
}

</span><span style="background: white; color: gray;">/// <summary>
/// </span><span style="background: white; color: green;">通过数据行映射现有对象
</span><span style="background: white; color: gray;">/// </summary>
</span><span style="background: white; color: blue;">public static void </span><span style="background: white; color: black;">MapToObject(</span><span style="background: white; color: blue;">this </span><span style="background: white; color: #2b91af;">DataRow </span><span style="background: white; color: black;">dataRow, </span><span style="background: white; color: blue;">object </span><span style="background: white; color: black;">convertedObject)
{
    </span><span style="background: white; color: #2b91af;">Type </span><span style="background: white; color: black;">objectType = convertedObject.GetType();
    System.Reflection.</span><span style="background: white; color: #2b91af;">PropertyInfo</span><span style="background: white; color: black;">[] properties = objectType.GetProperties();
    </span><span style="background: white; color: blue;">foreach </span><span style="background: white; color: black;">(System.Reflection.</span><span style="background: white; color: #2b91af;">PropertyInfo </span><span style="background: white; color: black;">property </span><span style="background: white; color: blue;">in </span><span style="background: white; color: black;">properties)
    {
        </span><span style="background: white; color: blue;">if </span><span style="background: white; color: black;">(dataRow.Table.Columns.Contains(property.Name))
        {
            </span><span style="background: white; color: blue;">object </span><span style="background: white; color: black;">value = dataRow[property.Name];
            </span><span style="background: white; color: blue;">object </span><span style="background: white; color: black;">convertedValue = ConvertValue(value, property.PropertyType);
            property.SetValue(convertedObject, convertedValue, </span><span style="background: white; color: blue;">new object</span><span style="background: white; color: black;">[0]);
        }
    }
}

</span><span style="background: white; color: blue;">public static object </span><span style="background: white; color: black;">ConvertValue(</span><span style="background: white; color: blue;">object </span><span style="background: white; color: black;">value, </span><span style="background: white; color: #2b91af;">Type </span><span style="background: white; color: black;">conversionType)
{
    </span><span style="background: white; color: blue;">if </span><span style="background: white; color: black;">(IsNullableType(conversionType))
    {
        </span><span style="background: white; color: blue;">return </span><span style="background: white; color: black;">ConvertNullableValue(value, conversionType);
    }
    </span><span style="background: white; color: blue;">return </span><span style="background: white; color: black;">ConvertNonNullableValue(value, conversionType);
}

</span><span style="background: white; color: blue;">private static bool </span><span style="background: white; color: black;">IsNullableType(</span><span style="background: white; color: #2b91af;">Type </span><span style="background: white; color: black;">t)
{
    </span><span style="background: white; color: blue;">return </span><span style="background: white; color: black;">t.IsGenericType && t.GetGenericTypeDefinition() == </span><span style="background: white; color: blue;">typeof</span><span style="background: white; color: black;">(</span><span style="background: white; color: #2b91af;">Nullable</span><span style="background: white; color: black;"><>);
}

</span><span style="background: white; color: blue;">public static object </span><span style="background: white; color: black;">ConvertNonNullableValue(</span><span style="background: white; color: blue;">object </span><span style="background: white; color: black;">value, </span><span style="background: white; color: #2b91af;">Type </span><span style="background: white; color: black;">conversionType)
{
    </span><span style="background: white; color: blue;">object </span><span style="background: white; color: black;">convertedValue = </span><span style="background: white; color: blue;">null</span><span style="background: white; color: black;">;

    </span><span style="background: white; color: blue;">if </span><span style="background: white; color: black;">(conversionType.IsEnum)
    {
        conversionType = </span><span style="background: white; color: #2b91af;">Enum</span><span style="background: white; color: black;">.GetUnderlyingType(conversionType);
    }

    </span><span style="background: white; color: blue;">if </span><span style="background: white; color: black;">(value != </span><span style="background: white; color: #2b91af;">DBNull</span><span style="background: white; color: black;">.Value)
    {
        convertedValue = </span><span style="background: white; color: #2b91af;">Convert</span><span style="background: white; color: black;">.ChangeType(value, conversionType);
    }

    </span><span style="background: white; color: blue;">return </span><span style="background: white; color: black;">convertedValue;
}

</span><span style="background: white; color: blue;">public static object </span><span style="background: white; color: black;">ConvertNullableValue(</span><span style="background: white; color: blue;">object </span><span style="background: white; color: black;">value, </span><span style="background: white; color: #2b91af;">Type </span><span style="background: white; color: black;">conversionType)
{
    </span><span style="background: white; color: blue;">if </span><span style="background: white; color: black;">(value != </span><span style="background: white; color: #2b91af;">DBNull</span><span style="background: white; color: black;">.Value)
    {
        </span><span style="background: white; color: blue;">var </span><span style="background: white; color: black;">converter = </span><span style="background: white; color: blue;">new </span><span style="background: white; color: #2b91af;">NullableConverter</span><span style="background: white; color: black;">(conversionType);
        </span><span style="background: white; color: blue;">return </span><span style="background: white; color: black;">converter.ConvertFrom(value);
    }
    </span><span style="background: white; color: blue;">return null</span><span style="background: white; color: black;">;
}

}