A fast .NET Standard Class Library for parsing results from an SQL query to .NET objects
The default hardcode-index-way of creating objects from a Data Reader can be really messy for large objects with many columns.
Morph aims to simplify this process by only needing a single line of code to parse Data Reader results, and by making code bases easily extensible with the ColumnName
Attributes.
A small example:
if (await reader.ReadAsync()) {
var person = new Person() {
FirstName = reader["PERSON_FIRST_NAME"],
LastName = reader["PERSON_LAST_NAME"],
Address = reader["PERSON_ADDRESS"]
};
}
With Morph's extension Method:
var person = await reader.Parse<Person>();
(Requires a using
directive to the mrousavy.Morph
namespace)
Keep in mind to mark the Members you want to parse with the ColumnName
Attribute in Person
:
public class Person {
[ColumnName("PERSON_FIRST_NAME")]
public string FirstName { get; set; } // Will be set to "PERSON_FIRST_NAME" (ColumnName parameter) from the DataBase
[ColumnName]
public string LastName { get; set; } // Will be set to "LastName" (Member name) from the Database
public string Address { get; set; } // Will be ignored and not initialized by the Parser
}
Build with .NET Core 2.0 or higher