DTOs, Data Contacts and code generation
Recently a new LightSpeed customer asked about adding code generation support for creating data transfer objects (DTOs) and code to help in sending LightSpeed entities over the wire. Further to this, Jeremy recently posted a guide to working with WCF and LightSpeed due to an increasing interest in working with LightSpeed in distributed environments.
So we set about adding this type of code generation and it is now available in the LightSpeed nightly builds (grab a copy here).
What code gets generated?
For each entity, a DTO decorated with WCF attributes:
[System.Runtime.Serialization.DataContract(Name="Customer")] public partial class CustomerDto { [System.Runtime.Serialization.DataMember] public string Username { get; set; } [System.Runtime.Serialization.DataMember] public string FirstName { get; set; } [System.Runtime.Serialization.DataMember] public string LastName { get; set; } [System.Runtime.Serialization.DataMember] public string Address1 { get; set; } [System.Runtime.Serialization.DataMember] public string Suburb { get; set; } [System.Runtime.Serialization.DataMember] public string City { get; set; } [System.Runtime.Serialization.DataMember] public string Country { get; set; } [System.Runtime.Serialization.DataMember] public string Phone { get; set; } [System.Runtime.Serialization.DataMember] public string Email { get; set; } [System.Runtime.Serialization.DataMember] public string Address2 { get; set; } }
An extension method to convert the entity to its associated DTO:
public static CustomerDto AsDto(this Customer entity) { return new CustomerDto { Username = entity.Username, FirstName = entity.FirstName, LastName = entity.LastName, Address1 = entity.Address1, Suburb = entity.Suburb, City = entity.City, Country = entity.Country, Phone = entity.Phone, Email = entity.Email, Address2 = entity.Address2, }; }
An extension method to convert the DTO to it’s associated entity:
public static Customer CopyTo(this CustomerDto source, Customer entity) { entity.Username = source.Username; entity.FirstName = source.FirstName; entity.LastName = source.LastName; entity.Address1 = source.Address1; entity.Suburb = source.Suburb; entity.City = source.City; entity.Country = source.Country; entity.Phone = source.Phone; entity.Email = source.Email; entity.Address2 = source.Address2; return entity; }
As you can see, this saves a huge amount of code that would be tedious for you to write and therefore fits the purpose of our products – to help you get more done, faster.
How do to this?
If you’re already a LightSpeed user, grab the latest nightly so that the designer includes this feature.
- First off, fire up Visual Studio and create a project for your model.
- Add references to System.ServiceModel and System.Runtime.Serialization (this is important, the designer will only generate this code if it detects you’re working with WCF)
- Create your model as you normally would, hit save. If you have an existing model, open it and save it.
- Done!
We always appreciate getting feedback about what features would help you. If there’s something we could generate to save you some coding effort then leave us a comment.
5 Responses to “DTOs, Data Contacts and code generation”
Leave a Reply
![]()
BrainDump (1)
Community Code (1)
Events (6)
General (31)
Lab Samples (2)
LightSpeed (132)
MegaPack (3)
News (48)
Products (64)
Projects (4)
Screencast (6)
SharePoint (1)
Silverlight (5)
Silverlight Elements (12)
SimpleDB Management Tools (11)
Visual Studio (4)
VS File Explorer (5)
WPF (31)
WPF Diagramming (14)
WPF Elements (22)
WPF Property Grid (24)
![]()
June 2010
May 2010
April 2010
March 2010
February 2010
January 2010
December 2009
November 2009
October 2009
September 2009
August 2009
July 2009
June 2009
May 2009
April 2009
March 2009
February 2009
January 2009
December 2008
November 2008
October 2008
September 2008
August 2008
July 2008
June 2008
May 2008
April 2008
March 2008
February 2008
January 2008
December 2007
November 2007
September 2007
August 2007
July 2007
June 2007
May 2007
April 2007
March 2007



Tagged as 

Posted by John-Daniel Trask on 28 January 2009



Hi,
this will be a good timesaver and give finer grained control by including rather than excluding properties essential for serialization, which XmlIgnore (and ScriptIgnore) sufferred from a lack of. Will it be possible to include generated fields like LockVersion and ID, which would be essential in allowing serialized entities to be updated remotely and then reattached to a Unit Of Work? Would be nice to see a working sample where JSON is used as the transmission format, perhaps even using JSON.NET rather than WCF.
Cheers,
Donovan.
Tested it out a little bit! Would be good if inherited properties from the base type were also include in the code generation. Eg. Event.HelpText is include in the AskingPriceEvent DTO.
Another idea would be flattening out related entities’ properties to a certain specified depth. This could get complicated but would be very useful. Some inspiration from here maybe?
http://structuremap.sourceforge.net/Generics.htm
Tried manually editing the DataContracts.vm, but don’t want to reverse engineer your templating language! Is it NHaml-derived by any chance?
Hi Donovan,
Thanks for your feedback! We will be making significant enhancements to LightSpeed’s code generation and distributed features (DTOs, WCF support etc) in LightSpeed 3.0 which we begin development on next week. This feedback will likely directly result in enhancements that you’re asked for here.
This features was added fairly quickly to address a request that an existing customer had and we thought it was a good start. It is just that though – a start :-)
The templates use NVelocity for the templating engine – there is documentation floating around regarding this online. We’ll be improving our guidance around working with the templates in the future as we continue to move down the code-generation road. Do feel free to hack around with them but do back them up so you don’t accidentally revert them when you install a newer version of LightSpeed.
Hi Donovan,
Just to let you know that the inheritance issue will be addressed in LightSpeed 2.2, which should be out very soon.
Brilliant!