Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF Message & Data Contract, DTO, domain model, and shared assemblies

I have a web client that calls my WCF business service layer, which in turn, calls external WCF services to get the actual data. Initially, I thought I would use DTOs and have separate business entities in the different layers... but I'm finding that the trivial examples advocating for DTOs, to be, well, trivial. I see too much duplicate code and not much benefit.

Consider my Domain:

Example Domain I have a single UI screen (Asp.net MVC View) that shows a patient's list of medications, the adverse reactions (between medications), and any clinical conditions (like depression or hypertension) the patient may have. My domain model starts at the top level with:

   MedicationRecord
      List<MedicationProfile> MedicationProfiles
      List<AdverseReactions> Reactions
      List<ClinicalConditions> ClinicalConditions

   MedicationProfile is itself a complex object
      string Name
      decimal Dosage
      Practitioner prescriber 

   Practioner is itself a complex object
      string FirstName
      string LastName
      PractionerType PractionerType
      PractionerId Id
      Address Address    

      etc.

Further, when making the WCF requests, we have a request/response object, e.g.

   MedicationRecordResponse
      MedicationRecord MedicationRecord
      List<ClientMessage> Messages
      QueryStatus Status

   and again, these other objects are complex objects
   (and further, complicates matter is that they exist in a different, common shared namespace)

At this point, my inclination is that the MedicationRecordResponse is my DTO. But in pure DataContracts and DTO and separation of design, am I suppose to do this?

   MedicationRecordResponseDto
      MedicationRecordDto
      List<ClientMessageDto> 
      QueryStatusDto

   and that would mean I then need to do
      MedicationProfileDto
      PractitionerDto
      PractitionerTypeDto
      AddressDto
   etc.

Because I have show almost all the information on the screen, I am effectively creating 1 DTO for each domain object I have.

My question is -- what would you do? Would you go ahead and create all these DTOs? Or would you just share your domain model in a separate assembly?

Here's some reading from other questions that seemed relevant:

  1. WCF contract know the domain
  2. Alternatives for Translation Layer in SOA: WCF
  3. SOA Question: Exposing Entities
like image 840
Raymond Avatar asked Jul 27 '12 04:07

Raymond


People also ask

What is WCF message?

Windows Communication Foundation (WCF) is a framework for building service-oriented applications. Using WCF, you can send data as asynchronous messages from one service endpoint to another. A service endpoint can be part of a continuously available service hosted by IIS, or it can be a service hosted in an application.

Is WCF still in use?

NET Framework. With a lot of WCF code still in use, it's good to see a supported route that helps bring that code to newer platforms and the . NET cross-platform world where WCF code can now run on Linux servers as well as on Windows.

Is WCF an HTTP?

WCF supports HTTP, UDP, and custom transport protocol whereas Web API supports only HTTP protocol. WCF offers Text, MTOM, and Binary Encoding support whereas Web API supports the UTF-8 encoding format. WCF supports message queues, message security, duplex communication, transaction whereas Web API doesn't support.


2 Answers

Take a look on excellent articles

  • Why You Shouldn’t Expose Your Entities Through Your Services
  • DTO’s Should Transfer Data, Not Entities

above links don't work, looks like a domain issue (I hope it'll be fix), here is the source:

  • DTO’s Should Transfer Data, Not Entities
  • Why You Shouldn’t Expose Your Entities Through Your Services
like image 128
GSerjo Avatar answered Sep 28 '22 15:09

GSerjo


I've always had an aversion to the duplicate class hierarchy resulting from DTOs. It seems to be a flagrant violation of the DRY principle. However, upon closer examination, the DTO and the corresponding entity or entities serve different roles. If you are indeed applying domain-driven design then your domain entities consist of not only data but behavior. By contrast, DTOs only carry data and serve as an adapter between your domain and WCF. All of this makes even more sense in the context of a hexagonal architecture also called ports and adapters as well as the onion architecture. Your domain is at the core and WCF is a port which exposes your domain externally. A DTO is part of how WCF functions and if you agree that it is a necessary evil your problem shifts from attempting to eliminate them to embracing them and instead focusing on how to facilitate the mapping between DTOs and domain objects. A popular solution is AutoMapper which reduces the amount of boiler plate mapping code you need to write. Aside from the drawbacks, DTOs also bring a lot of benefits. One is that they furnish a buffer between your domain entities and the outside world. This can be of great help in refactoring because you can keep your core domain very well encapsulated. Another benefit is that you can design your DTOs such that they fulfill requirements of the service consumer, requirements which may not always be in full alignment with the shape of your domain objects.

like image 32
eulerfx Avatar answered Sep 28 '22 15:09

eulerfx