I have a C# client and a java server. I have data object that go on the wire back and forth. Lets call them FooData.cs where everything is just a get and a set (no logic)
FooData data = new FooData(); data.Price = 10; data.Quantity = 20;
i have other derived fields that i want to use in the application but dont need to be sent on the wire so i have anothe class
FooWrapper.cs.
I inject the data object into the wrapper
FooWrapper wrapper = new FooWrapper(Foodata); wrapper.Price = 10; wrapper.Quantity = 20; double total = wrapper.GetTotal();
and the wrapper has a lot of the same properties as the data object (and we just delegate down) or the wrapper also has a number of calculated properties. The only state the wrapper has is the data object (no other member variables)
We were having a debate about using this model versus using converters. The converter way would be to instead of having FooWrapper, have a FooBusinessObject and instead of injecting the ‘on the wire’ object, we call a convert method that passes all of the data from the on the wire object to the business object.
FooData data = new FooData(); FooBusinessObject busObj = new FooBusinessObject(); busObj.Price = data.Price; busObj.Quant= data.Quantity; double total = busObj.GetTotal();
Any thoughts on what is better (wrapper versus business object / converter)
(edit) What version of C# are you using? With C# 3.0 you could place the ‘logic’ bits (calculations etc) in extension methods in a busines logic assembly, but still see them via the DTO – i.e.
(data layer)
(business layer)
(ui layer)
What model are you using to transfer the data? web services? xml? data-contracts? binary? Most would allow you to ignore the extra properties without any extra code (i.e. no need for a wrapper/facade object):
XmlSerializer:DataContractSerializer:If you are talking binary, there are portable java/C#-friendly models such as ‘protocol buffers‘ – Jon Skeet has a C# port of the official java version (allowing you to use very similar code on both sides) – or I have a more C#-idiomatic version (protobuf-net):
ProtoSerializer: