Use serialized domain model entities as web service messages?

I am wondering about the viability of using domain entity classes as the return types and method arguments in web services. The motivation is to avoid having to create a layer of data-transfer/message classes, and the mapping of domain objects to/from them. Technically it works - the IDE autogenerates the proxy classes and does all the serialization. But when you get into the particulars of the domain entity classes (their relationships with each other, aggratetions, exposed collectons, etc), I can see there being practical problems.

[547 byte] By [ALFKI] at [2007-12-28]
# 1
This is done fairly commonly but it seriously compromises the principle of loose coupling because both the caller and the service must have the class definition to be able to serialize and deserialize the messge contents. One of the major advantages of using an XML document as the message body is that any application that can consume the WSDL file can use the service. Using serialized objects as the message contents means the two endpoints must share the same class definition so a lot of flexibility is lost.
Roger_Wolter_MS at 2007-9-4 > top of Msdn Tech,Architecture,Architecture General...
# 2

I agree with Roger that a pure message based (Xml based) interface for your web service is the best way to go, but I have found need to compromise on this on more than one occasion. Usually I do not choose to compromise, but it forced on my the consuming application. For example: Using the Business Data Catalog with Microsoft Office SharePoint server forces you to compromise on this front as it is considerbly harder to build a BDC connection to a message based web service than to an entity based web service.

However, if I am building a enterprise application for a closed system or a controlled system, I would always default to a message based interface as it provides the greatest flexibility for integration, versioning and compatability. Exposing your business entities through a web services only opens you up to more issues, especially when you are building proxies against a Wsdl rather than distributing them yourself. It is far too easy for a consumer to break your business rules, which then requires you to spend extra time building in rule checks in your web service. In the end this can all be avoided with a well thought out messaging interface.

-Graham

GrahamSibley at 2007-9-4 > top of Msdn Tech,Architecture,Architecture General...