Friday, March 21, 2014

Entity Framework wrapper to return custom objects using LINQ

The complex data types which are automatically generated by Entity Framework are very useful, but sometimes the data may need to be placed in a custom object which is reusable across multiple areas of the code. The entity framework custom type may also have additional unneeded columns or strange naming conventions on the columns which can lead to messy code, so a cleaner custom type is beneficial in this situation.

In the example below there is a wrapper class for getting a single object as well as a list of objects. Instead of using the default complex types (such as SelectEmployeeByDepartment_Result or SelectEmployee_Result), LINQ is used to parse the results into a custom object.

Business logic and other rules can also be placed at this layer (such as converting a SQL bit column into a C# bool object).

No comments:

Post a Comment