Perpetualcoder
Perpetualcoder

Reputation: 13571

Is using DTO's and Entities breach of DRY principle?

I was looking at a library called Automapper. I am having a few concerns with this:

  1. We dont want to expose our data model (GOOD). Why should the datamodel closely resemble your DB?

  2. using lightweight DTOs instead of your entities. (GOOD)

  3. Now I need to map my entities to these DTOs. Am i respecting the DRY principle??

Upvotes: 5

Views: 3360

Answers (2)

cbkadel
cbkadel

Reputation: 264

Depends on the applications. Transaction applications, and depending on the business logic requirements, exposing your data model to upper layer code can make sense for projects of a certain scale. I think DRY becomes important the larger the application, but I don't know enough the context from which you're asking this question.

Upvotes: 0

Kevin Pang
Kevin Pang

Reputation: 41442

One could argue that DTOs violate DRY, but if it makes sense for your situation then I wouldn't think twice about it.

DRY, like most programming best practices, isn't a silver bullet. Sometimes you have to compromise. In this case, I'd argue that violating DRY is perfectly acceptable in order to prevent issues that can arise from leaking your domain details to callers that don't need it (e.g. N+1 lazy-loading performance issues).

Upvotes: 4

Related Questions