DTO Generator for EF 4 Entity model

v00d00 picture v00d00 · Jul 6, 2011 · Viewed 7k times · Source

Is it possible to write t4 template ( or if it already exists ) which will be able to generate DTO classes based on the data in the *.edmx file?

I have to write DTO classes for the current project, and this process is kinda tiresome.

What I trying to acquire is to get DTO classes which will have scalar properties defined as simple auto properties, and navigation parameters as incapsulated instances of other DTO classes.

Example :

public class SomeClassDTO
{
    public byte Id { get; set; }

    public string Description { get; set; }        

    public OtherClassDTO SomeProperty {get;set;}

    public IList<AnotherClassDTO> Objects {get;set;}
}

This is a good starting point, what is more desirable may look like following sample:

/// <summary>
/// Employee details DTO.
/// </summary>
public class EmployeeDetailsDTO
{
    [Key] 
    public long Id { get; set; }

    [Required] 
    public string FirstName { get; set; }

    [Required]
    public string Surname { get; set; }

    ...

    public long? PhotoId { get; set; }

    // Home address properties.


    public string HomeAddressAddressLine1 { get; set; } // This is just name of field, not flattened list 
    public string HomeAddressAddressLine2 { get; set; }
    public string HomeAddressAddressLine3 { get; set; }
    public string HomeAddressPostcode { get; set; }
    public short? HomeAddressCountryId { get; set; }
    public long? HomeAddressCountyId { get; set; }
    public long? HomeAddressTownId { get; set; }


    public short? HomeTelephoneCountryId { get; set; }        
    public string HomeTelephoneNumber{ get; set; }
    public string HomeTelephoneExtension { get; set; }


    public short? PersonalMobileCountryId { get; set; }      
    public string PersonalMobileNumber { get; set; }
    public string PersonalMobileExtension { get; set; }

}

As you can see this is a flatten DTO which represent composite structure and may be injected back to entities through ValueInjector SameNameFlat/UnFlat injections.

This is the ultimate goal, though any advices would be appreciated.

Answer

kzfabi picture kzfabi · Jul 24, 2011

I recently published an Entity Framework DTO Generator named EntitiesToDTOs at CodePlex, it is free and open source, and it is used as an AddIn for Visual Studio 2010 and 2012. I think it will be of help for you.

Go to http://entitiestodtos.codeplex.com to download it, and let me know what you think ;)