Creating Data Transfer Objects – Part 2

I have updated the console application DTO to version 2.0 to use attributes now to decorate the domain classes for code generation. This offers much better control of which domain classes should be used to create the Data Transfer Object classes. In addition, you can also decorate generate public properties that use domain classes as a return value.  

For example, if you decorate the following sample domain classes below with the DTO attribute like so 


using DTOAttributes;
namespace Example
{   
   [DTOAttributes.DTO]   
   public enum CustomerType   
   {       
      Regular,       
      Special   
   } 
   [DTOAttributes.DTO]
   public class Customer
   {

The DTO console app will generate the following code:

public enum CustomerTypeDTO      
{             
   Regular,             
   Special,      

public class CustomerDTO      
{             
   private List<EmailAddressDTO> _emails;
   [XmlElement(IsNullable = true)]
   public List<EmailAddressDTO> Emails
   {                    
      get { return _emails; }
      set { _emails = value; }
   } 

   private CustomerTypeDTO _type;
   [XmlElement(IsNullable = true)]
   public CustomerTypeDTO Type
   {                    
      get { return _type; }
      set { _type = value; }
   }

As you can see, it works pretty well. I’ve included the source code including the sample app. I’ve not update the CodeSmith template, yet as the console app is doing a fine job for me. For now. In my current project, this allows me to generate over 15,000 lines of code to create dozens of DTO classes automatically. Sweet! Enjoy!

You can download the new version here.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s