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

2023/7/6 02:13:31

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

<span style=background: white; color: blue;>using <span style=background: white; color: black;>System; <span style=background: white; color: blue;>using <span style=background: white; color: black;>System.Collections.Generic; <span style=background: white; color: blue;>using <span style=background: white; color: black;>System.ComponentModel; <span style=background: white; color: blue;>using <span style=background: white; color: black;>System.Data;

<span style=background: white; color: blue;>public static class <span style=background: white; color: #2b91af;>DataRowExtensions <span style=background: white; color: black;>{ <span style=background: white; color: gray;>///

/// <span style=background: white; color: green;>通过数据行创建一个对象 <span style=background: white; color: gray;>/// <span style=background: white; color: blue;>public static <span style=background: white; color: black;>T MapToObject(<span style=background: white; color: blue;>this <span style=background: white; color: #2b91af;>DataRow <span style=background: white; color: black;>dataRow) { <span style=background: white; color: #2b91af;>Type <span style=background: white; color: black;>type = <span style=background: white; color: blue;>typeof<span style=background: white; color: black;>(T); T convertedObject = System.<span style=background: white; color: #2b91af;>Activator<span style=background: white; color: black;>.CreateInstance(); MapToObject(dataRow, convertedObject); <span style=background: white; color: blue;>return <span style=background: white; color: black;>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;"">;
}

} "