3

I am writing a WCF service and I want to use DataTransferObjects that basically clone all fields of my classes. For example I have a class Person

class Person
{
    public int Id;
    public string Name;
    public Person()
    {
        //Some complex stuff here
    }
}

class PersonDTO
{
    //Copy all public fields and properties of Person
    public int Id;
    public string Name;
}

Is there a way to generate my DTO class based on other class? A code-snippet or something like that?

I am not trying to copy the values of the fields. I want to generate a class with same fields as the "Parent" class. But I don't want to inherit my DTO class

Anarion
  • 2,406
  • 3
  • 28
  • 42
  • Why don't you inherit `PersonDTO` from `Person`? – Raging Bull Mar 01 '14 at 09:26
  • 1
    Because I don't want `PersonDTO` changed if `Person` gets changed – Anarion Mar 01 '14 at 09:27
  • Yeah, but inheritance will just get the members, properties, and functions in the child class. The values that the members and properties hold will not be copied. – dhrumilap Mar 01 '14 at 09:28
  • You are looking for a code generator. Sounds like a job for visual studio macro. Are you gonna use library in mapping between DTO and business object? – Panu Oksala Mar 01 '14 at 09:33
  • @Mino, yes, I am looking for a code generator (and I stated it in the question), the question is - which code generator should I use (and how). – Anarion Mar 01 '14 at 09:37
  • There is one sort of answer for this: http://stackoverflow.com/questions/832578/generate-dtos-from-object – Panu Oksala Mar 01 '14 at 09:43
  • @Mino, there is a dirty hack which I will use. Thank you ) – Anarion Mar 01 '14 at 09:44

3 Answers3

1

Take a look at T4 templates, that will help you to generate code. It will not be that hard, but not easy too. Once you done for single class you can re-use it.

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
1

You can use T4 templates to generate DTOs from your entities. Here is article Code generation with T4, Entities to DTOs example which have sample project attached for generating DTOs from entities.

Main idea is providing path to assembly with entities:

var entitiesAssembly = @"bin\Debug\EntitiesToDTO.dll";

Then getting all entity types from that assembly:

var typesToRegister = 
    from t in LoadProjectAssembly(entitiesAssembly).GetExportedTypes()
    where t.Namespace == entitiesNamespace && t.IsClass && !t.IsAbstract
    select t;

And processing them in a loop:

<#
foreach (var type in typesToRegister.Where(t => t.IsClass && !t.IsAbstract))
{#>
   [DataContract(Name="<#= ConvertToCamelCase(type.Name) #>]
   public partial class <#= GetDTOClassName(type.Name) #>
   {
      <# foreach (var property in type.GetProperties())
      {#>
         // analyze property type
         // and generate appropriate DataMember property
      <#}#>
   }
<#}#>
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
0

Sounds to me that what you're looking for is deep cloning. You basically serialize the object you want to copy, then deserialize the result.

Take a look here: Deep cloning objects

Community
  • 1
  • 1
Omri Aharon
  • 16,959
  • 5
  • 40
  • 58
  • 1
    Not really. I have EF classes which I don't want to expose via WCF, and I want to create DTO objects which have less firlds, and they will be exposed via WCF contract. – Anarion Mar 01 '14 at 09:31