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.

Tagged as LightSpeed, Products

5 Responses to “DTOs, Data Contacts and code generation”

  • 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!

  • Leave a Reply

Archives

Join our mailer

You should join our newsletter! Sent monthly:

Back to Top