Created
March 30, 2016 11:24
-
-
Save aleksandrzak-rafal/140110b4ca455695cb7249c1e9959660 to your computer and use it in GitHub Desktop.
Clever way to map data from DataReader to class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Employee | |
| { | |
| public int Id { get; set; } | |
| public string Name { get; set; } | |
| public static Employee Create(IDataRecord record) | |
| { | |
| return new Employee | |
| { | |
| Id = record["id"], | |
| Name = record["name"] | |
| }; | |
| } | |
| } | |
| public IEnumerable<Employee> GetEmployees() | |
| { | |
| using (var reader = YourLibraryFunction()) | |
| { | |
| while (reader.Read()) | |
| { | |
| yield return Employee.Create(reader); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment