LightSpeed, WCF and Serialization
It seems that developing with LightSpeed and having distributed scenarios in mind is becoming very popular based on all the queries we have received recently in the forums around how LightSpeed plays with WCF :)
In the earlier releases of LightSpeed 2, we supported distributed scenarios in 2 ways. Firstly LightSpeed entities could be serialized by using either the XmlSerializer (e.g. with ASP.NET ASMX web services) or using the DataContractFormatter with WCF. Generally we would encourage you to use WCF due to the serializer being a bit smarter, particularly at handling graphs of objects which you typically encounter in your models.
In LightSpeed 2.2 we are adding some interim support to the generator to allow some standard Data Transfer Objects (DTO’s) and convertor functions for mapping between the entities and DTO’s to help save you some time.
An example would be, given an Employee entity such as the one we use in our samples, we would generate you a DTO called EmployeeDTO already decorated for use with WCF;
[DataContract] public class EmployeeDTO { [DataMember] public string LastName { get; set; } [DataMember] public string FirstName { get; set; } // .. and so on for each entity property }
and then a convertor function such as;
public static Converter<employee , EmployeeDTO> ConvertEmployee = (employee) => { return new EmployeeDTO() { FirstName = employee.FirstName, LastName = employee.LastName // ... etc }; };
So you can perform operations such as;
EmployeeDTO[] allEmployees = unitOfWork.Find<employee>().ToArray(DTOConvertor.ConvertEmployee);
or
EmployeeDTO singleEmployee = DTOConvertor.ConvertEmployee(unitOfWork.FindOne<employee>(1));
Going the other way you will likely want to map the fields on the inbound DTO back on to an existing or new LightSpeed entity, so we generate a similar style extension method such as;
Employee employee = singleEmployee.MapTo(unitOfWork.FindOne<Employee>(1));
In LightSpeed 3.0 we will be catering for distributed scenarios in a much more structured and supported way and we will provide some more concrete example of these closer to release :) If you are working with LightSpeed in a distributed scenario currently or are thinking of doing so, drop us a line in the forums – we would love to hear from you and work with you during the 3.0 timeframe to get the bits you need in there!
5 Responses to “LightSpeed, WCF and Serialization”
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 Jeremy Boyd on 20 January 2009



[...] 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 [...]
Hi Jeremy!
We are very excited using LightSpeed. We went furthermore and implemented a WCF, but when I was looking for the DTOConvertor class (even in the last nightly build •Nightly Build for 13 May 2009 – LightSpeedExpress-20090513.msi) it wasn’t present.
Do you have any idea when it will be available, because we are very interested in using LightSpeed with WCF in our project (’cause it’s a must have for us).
Hope to hear from you soon. Thanks in advance!
Juan – as Marko mentioned on the forums, the converter functions got renamed to AsDto, and the class they’re contained in got renamed to XxxDtoExtensions, where Xxx is the name of your model. Also note this will only get generated if your project references System.ServiceModel.dll. Hope this helps!
Say I have a service called getAllEmployees and I use your code to implement my service so it returns an array of EmployeeDTOs ie.
EmployeeDTO[] allEmployees = unitOfWork.Find().Select(e => e.AsDto()).ToArray() ;
Then on the client side someone edits an employee and it is posted back using a service called setEmployee that has a parameter of EmployeeDTO.
And I use your code to create an Employee Entity. i.e
State state = stateDTO.CopyTo(new State())
state.Save() ;
How can the save update the entity, as the EmployeeDTO does not have even a private Id field?
How are people using these DTO objects
Hi Kendall,
To update an existing employee using the DTO, you would load the Employee using IUnitOfWork.FindOne(id), then perform a CopyTo from the DTO into this Employee, then save. Regarding how you would get the ID to pass to FindOne, see http://www.mindscape.co.nz/forums/Post.aspx?ThreadID=2409&PostID=7000.
To create a new Employee using the DTO, you would call new Employee(), then perform a CopyTo into this Employee, then add it to the UOW, then save. This is in fact why DTOs don’t have an Id field: Id is assigned by LightSpeed when it does the add — that is, the Id is *not* assigned by the client.
Hope this clarifies things!