Click here to Skip to main content
Click here to Skip to main content

DTO from Entity Framework Class

, 11 Mar 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
To inherit an Entity Framework class, to extend it, bind to data and unmap it from DBContext.

Introduction

Suppose you work with an ORM (e.g.: Entity Framework) and you usually use DTO (Data Transfer Object) pattern to transfer data.

When creating a DTO class from a binding table, you need most of mapped properties, so that you must define them again in your new class. Also when you have to transfer object to a view, you need to bind it with data base values.

If you want to avoid the tedious task of fill object, you can use reflection or an IOC container as AutoMapper, but you still need to declare all properties to object.

Using inheritance you won’t need it, but, by default the class will be linked by Entity Framework to data context; also since it’s a subclass it’s not possible to cast it from its parent class.

The solution is as follows:

  1. To declare an inherited class.
  2. To unmap class from Entity Framework by using NotMapped keyword.
  3. To bind properties through reflection with its parent class.

Background

If you are going to use this code, it’s supposed you are programming with C# and Entity Framework, as well as you have knowledge about DTO pattern and reflection.

Using the Code

Your DTO Class:

[NotMapped]
public class YourDTO: YourDBContextClass
{
    //Feel free to add properties. EF Won’t try to bind it with database
    Public property string FullName { get{ return (Name + “ “ + Surname); } }
} 

The Controller:

public ActionResult Print(int id)
{
   DTO.YourDTO dtoObj = new DTO.YourDTO();
   dtoObj.CopyObject(dbContext.ParentClass.First(l => l.id==id));
   return (dtoObj);
} 

The Reflection Method

CopyObject as an Extension Method

 public static class ObjectExtensions
 {
    public static void CopyObject(this object objTo, object objFrom)
    {
        Type tObjFrom = objFrom.GetType();
        Type tObjTo = objTo.GetType();

        var listPropObj1 = tObjFrom.GetProperties().Where(p => p.GetValue(objFrom) != null).ToList();

         foreach (var item in listPropObj1){
             if (tObjTo.GetProperty(item.Name) != null){
                 tObjTo.GetProperty(item.Name).SetValue(objTo, item.GetValue(objFrom));
             }
         }
    }
 } 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

meula
Software Developer (Senior)
Unknown
No Biography provided
Follow on   LinkedIn

Comments and Discussions

 
GeneralMy vote of 1 PinprofessionalJV999912-Mar-14 2:53 
GeneralRe: My vote of 1 Pinmembermeula12-Mar-14 3:34 
GeneralRe: My vote of 1 PinprofessionalJV999912-Mar-14 4:38 
GeneralRe: My vote of 1 Pinmembermeula12-Mar-14 5:06 
SuggestionReflection, too slow. PinmemberRalph Varjabedian12-Mar-14 2:49 
GeneralRe: Reflection, too slow. Pinmembermeula12-Mar-14 3:36 
GeneralRe: Reflection, too slow. PinmemberRalph Varjabedian18-Mar-14 6:45 
GeneralMy vote of 5 PinmvpAkhil_Mittal11-Mar-14 22:24 
GeneralRe: My vote of 5 Pinmembermeula12-Mar-14 0:23 
QuestionI was looking for solution like this. Pinmemberdzimiq9-Mar-14 23:26 
AnswerRe: I was looking for solution like this. Pinmembermeula10-Mar-14 0:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 11 Mar 2014
Article Copyright 2014 by meula
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid