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
Categories
BrainDump (1)
Community Code (4)
Events (16)
F# (14)
General (53)
Lab Samples (2)
LightSpeed (268)
MegaPack (8)
News (71)
NHibernate Designer (26)
Nightly news (53)
Phone Elements (24)
Products (87)
Projects (5)
Screencast (6)
SharePoint (3)
Silverlight (14)
Silverlight Elements (66)
SimpleDB Management Tools (20)
Visual Studio (9)
VS File Explorer (7)
Web Workbench (39)
WPF (44)
WPF Diagrams (57)
WPF Elements (110)
WPF Property Grid (32)


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!