Wednesday, October 6, 2010

Using Data Transfer Objects in Layered Architecture

1 Introduction

A layered application consists of three principal layers: presentation, domain and data source.

The presentation layer is about the interaction between the user and the application, while the data source layer for most applications consists of a relational database and the logic needed to read and write data in the database.

The domain layer contains the domain logic, also referred to as business logic. This is the work the application needs to do. It is important to notice that each layer has its own responsibilities and that communication between the layers must occur via well-defined interfaces.

The domain layer can be subdivided into a Domain Model and Service sub-layer depending on the complexity of the application and the patterns used to implement the domain logic.

The domain logic can use the following patterns:

· Transaction Script

· Domain Model

· Table Module

2 Introducing Data Transfer Object

DTO is an enterprise design pattern to implement the interface between the presentation and the domain layer.

Using DTO’s assures that the domain model (and possibly the domain logic) is not exposed to the presentation layer. Although the use of this pattern is certainly not advocated by Microsoft .NET tooling and Microsoft example code, it is imho a necessary pattern to achieve the needed separation between the presentation and the domain layer.

The discussion whether or not to use DTO’s is out of the scope of this document as there are pro’s and con’s for each approach.

Advantages of DTO’s:

· Isolation of logic

· SOA requirement

· Reduced number of remote calls

· Improved performance

· Testablility

· Deterministic content

Disadvantages of DTO’s:

· Possible class explosion

· Additional computing

· Additional code effort

Most of the disadvantages can be resolved by code generation of the DTO’s and the associated mappers between the DTO’s and the domain entities. Additional computing is in most remote scenario’s (presentation layer is remote) not an issue because serialization of more complex domain entities will require more computing than serializing the simple DTO structures.

How to use DTO’s efficiently is the subject of this document.

3 Packaging and dependencies

Following figure shows the packages and dependencies in a layered application.

Drawing1

The domain model is the heart of the application. It contains the domain entities and the repository interface definitions. By defining the repository interfaces, the domain model describes the operations it requires to bring entities in memory and persist entities to the database. The business logic is either contained in the domain model (DDD – approach) or in the services when using the Transaction Script pattern. The domain model has no dependencies on any other part of the architecture and is technology agnostic.

The service layer and the data layer have a dependency on the domain model. The data layer because it needs to implement the repositories and handle the persistence of the domain entities. The service layer because it is responsible for orchestrating the business logic.

By defining the repository interfaces in the domain model, the service layer becomes independent of the data layer and is as such independent of the used storage technology. To instantiate the repositories, the service layer uses the inversion of control pattern, eventually accompanied by dependency injection (IoC/DI).

Because the presentation layer should by ignorant of the domain model, it communicates with the service layer using the Data Transfer Object pattern. Since the DTO’s could be shared between the presentation and the service layer (in a .NET only scenario), the DTO’s should not know about the domain entities. In order to map data between the DTO’s and the domain model, mapper or DTO factories must be created in the service layer (because the service layer is the only one that knows both the domain entities and the DTO’s.

4 Sending data to the Presentation Layer

Data is delivered to the client in the form of a DTO. This is simplified representation of part of the domain model and is designed to fit the display needs of the client.

If the service would simply use the domain model to retrieve the data and map the needed data from the retrieved entity object graph, way too much data would probably have been retrieved from the database, transported and brought into memory on the application server. We might solve this problem by making the domain model and data layer dependent on the DTOs. But doing this will make it impossible to share the DTOs with the client and will also introduce pollution in the domain model.

Following technique illustrates how we can use LINQ to solve this problem and still retrieve exactly what we need from the database.

4.1 Simple scenario

We have a database with two tables (Nursery and NurseryPlant) having a one-to-many relation (a Nursery has one or more Plants).

In the domain model this results in two entities (Nursery and NurseryPlant).

image

When the client needs to display an overview of the Nurseries, it receives a ‘NurseryOverviewResponse’ message containing one or more ‘NurseryOverviewDto’ instances.

image

The NurseryOverviewDto contains the NurseryId, Usage and Year properties from the Nursery entity and the ForDisplay property which is a calculated property on the DTO (concatenating the other three properties).

The NurseryOverviewDto is produced by code generation and looks as follows:

clip_image002

The same code generator also produced the ‘NurseryOverviewDtoFactory’ type in the service layer.

clip_image004

This type contains mapping function between the Nursery entity and the DTO. It also contains an ‘ExpressionSelector’ which is an LINQ expression that can be used to perform projection in the DTO.

On the repository, following method is defined:

clip_image006

When called from the service implementation, this results in the following query on the database

clip_image008

clip_image010

4.2 Retrieving DTO with master-detail relation

In this scenario the DTO has a master-detail relationship (Nursery – NurseryPlant).

clip_image012

In order to do the correct projection we need (linked) factories for both types.

clip_image014

clip_image016

Remark that the factory for the master set contains a ‘Queryable<T>’ of the detail set.

The repository is the following:

clip_image018

This results in one query retrieving exactly the needed data from the database.

clip_image020

Conclusion

Using LINQ expressions in the service layer allows us to efficiently populate DTO’s and keep the DTO’s independent from any other component on the server side.

In a next post I will explain how the code generation is implemented.