Click here to Skip to main content
15,884,099 members
Articles / Programming Languages / C#
Tip/Trick

DTO from Entity Framework Class

Rate me:
Please Sign up or sign in to vote.
4.14/5 (8 votes)
11 Mar 2014CPOL1 min read 53.8K   14   11
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:

C#
[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:

C#
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

C#
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)


Written By
Software Developer (Senior)
Unknown
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
JV999912-Mar-14 1:53
professionalJV999912-Mar-14 1:53 
GeneralRe: My vote of 1 Pin
Jaume González12-Mar-14 2:34
Jaume González12-Mar-14 2:34 
GeneralRe: My vote of 1 Pin
JV999912-Mar-14 3:38
professionalJV999912-Mar-14 3:38 
I mean that a DTO should only be a transfer object and contain no other logic of whatsoever. In your case it inherits from EF and therefor includes data logic which makes it not a DTO.

What's hard about automapper compared to your solution btw? The same scenario in Automapper is:
C#
Mapper.CreateMap<A,B>();
A a = new A();
B b = Mapper.Map<A,B>(a);

GeneralRe: My vote of 1 Pin
Jaume González12-Mar-14 4:06
Jaume González12-Mar-14 4:06 
SuggestionReflection, too slow. Pin
Ralph Varjabedian12-Mar-14 1:49
Ralph Varjabedian12-Mar-14 1:49 
GeneralRe: Reflection, too slow. Pin
Jaume González12-Mar-14 2:36
Jaume González12-Mar-14 2:36 
GeneralRe: Reflection, too slow. Pin
Ralph Varjabedian18-Mar-14 5:45
Ralph Varjabedian18-Mar-14 5:45 
GeneralMy vote of 5 Pin
Akhil Mittal11-Mar-14 21:24
professionalAkhil Mittal11-Mar-14 21:24 
GeneralRe: My vote of 5 Pin
Jaume González11-Mar-14 23:23
Jaume González11-Mar-14 23:23 
QuestionI was looking for solution like this. Pin
dzimiq9-Mar-14 22:26
dzimiq9-Mar-14 22:26 
AnswerRe: I was looking for solution like this. Pin
Jaume González9-Mar-14 23:31
Jaume González9-Mar-14 23:31 

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

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