Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

where to handle DTO<->business object conversion

I have developed an application with following layers:

  • Data access layer based on fluent nHibernate
  • Business rules
  • activity layer(more abstract than business rules and use some business rules)
  • service layer based on WCF that sends some DTOs to the outside world and recieves DTOs.

so when some DTO came back, I can map the DTO to business objects in the service layer and made my application to work with business objects. in that case when some function in lower layers executes it does not know any thing about the old object, so it become hard to handle and verify state changing and also there is class explosion for DTO adapters. on the other hand if dto is mapped to business object on the higher layers, when it came down, the lower layers did not know anything about the service which is called, so they can not unserstand how this dto must change the business objects(1 DTO might be used by different services in different ways)

so the question is what is the real solution??

like image 361
mehdi.loa Avatar asked Oct 22 '22 03:10

mehdi.loa


1 Answers

From your specs, I'm kind of assuming you are aiming for a DDD based implementation

First, some assumptions to help map this to more common terminology: I assume your "Business Rules" layer is just used by your activity layer, and thus can be considered as part of the domain layer.

You mention business objects. I assume then that you have a domain layer. This might be your "activity layer". This should be the layer that knows how to update objects and return them to the service layer.

The service layer (or "application layer" in DDD terms) should be mapping the DTOs, and invoking domain services. MS has a decent diagram here. But basically the workflow should be:

  1. Send DTO to service layer
  2. Service layer invokes DTO adapters to create domain objects/entities out of DTOs.
  3. Service layer invokes domain services to perform business logic (invokes rules)
  4. Domain services update domain objects as a result of business rules
  5. Persistance layer is invoked by domain services as needed
  6. Domain services return updated domain objects to service layer
  7. Service layer maps domain objects back to DTO and returns them

There are of course many variations on this theme, but this should be your starting point.

like image 64
Rob Conklin Avatar answered Oct 23 '22 18:10

Rob Conklin