Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 618 Vote(s) - 3.57 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Which layer of the application should contain DTO implementation

#1
Lately I've been hearing a lot about DTOs and how useful they are but I can't find a good example of using it in ASP.NET context.

Let's say I use three tier architecture:

1. Data layer(using Entity Framework)
2. Business Layer(WCF Service)
3. Presentation Layer (MVC 4.0 web application)

Where should I convert from the EF Employee object to an EmployeeDTO POCO?

Lets say I do the conversion in the Data Access layer but what happens in the WCF service? Should it then be converted to another `DataMember` object and when it get's to the UI layer(MVC web app) should it then be converted for the third time to a model? I would appreciate it if someone could please clear this for me


Reply

#2
An approach that I'm particularly fond of is to the DTO conversion in your Business Layer.

*Scenario:* Your Presentation Layer calls your Business Layer passing a DTO. You do some logic & validation, then convert the DTO to an entity and send it to your Data Access Layer.

i.e. **UI --> Bus. Layer (convert to Entity) --> Data Layer**

I like this approach as I believe the Data Layer should not have any conversion logic and should receive and handle entities as needed. Another reason why this is effective is that you can now define specific business rules / validation logic during the conversion process before sending it to the Data Layer.
Here's an old [MSDN article](

[To see links please register here]

), but has some great details explaining a similar approach.
Reply

#3
There should not be any conversion. You would just use Poco Objects as DTO.

EF works with Poco and they can be serialized by WCF.

They should be defined in an assembly that is refernced by all projects.

In ASP.NET MVC you would mapp to a ViewModel by using the Poco - DTO.
Reply

#4
Your service layer exposes DTO's. This means that in the service layer you define data contracts as you would like them to be exposed to the outside world. In most cases they are flattened entities that not necessarily have the same structure as your database entities.

It is the responsibility of your service layer to use business/data layer and construct the DTO's that you expose to the outside world.

What you use in your business and data layer depends on the architecture. You could have a domain model that is mapped with code first. In that case, the service layer maps domain entities to data contracts (DTO's). If you don't have a domain model (anemic model), then you could as well just map the database directly to your DTO's.

The ASP.NET MVC site consumes the service, and maps the DTO's it receives to dedicates view models that are then passed to the specific view.

In addition, you may decide to also split queries from commands. This is a good approach because the DTO's that you get back as the reqult of a query are totally different than commands that you send to the service. A command only contains what's needed to execute the command and contain the business intend what you want to achieve, while a query returns a flattened model of what you need in the UI.

Other remarks:

- Don't expose your database entities.
- Don't convert in business layer, as it's not business logic.
Reply

#5
Have a look at

[To see links please register here]

added here as I don’t have enough reputation to add comments.

Personally I would pass entitys between your Persistence layer and your business layer. As you are using MVC chances are your are going pass view models to your controllers. At which point I would map your view model to your DTOs(s).

If you plan to use DTO between all of your layers then create a cross cutting project which you can then reference.
Reply

#6
In similar situation I used to put dto's into *Core* which is known to all three. So you have
<pre>
Core
|
------------
| | |
DAL BL PL
</pre>

Each layer can operate with `Core.Dto.Employee`. Each layer also exposes `Core.Dto.Employee` externally in its API. But internally each layer can transform/adapt `Core.Dto.Employee`, e.g. you read from database `EF.Employee` and later convert it to `Core.Dto.Employee`. Transformation is contained by the layer's boundary.

If you have several different models to represent same thing throughout the layers, for example PL wants `PL.Employee` and DAL operates on `EF.Employee`, you will end up with a mess.
Reply

#7
I use a project named **Shared** for such purposes, spesifically to share object with all layers. Regardless of the name, it should be visible by all layers.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through