guidsen
guidsen

Reputation: 2403

Combine two domain objects to a view

How would you combine two domain models in application design?

I got a domain model called Team, that includes: - id - name - description

And I got a domain model called TeamUser, that includes: - userId - teamId - roleType

The API consumer want a view that contains: - id (id of the team) - name - roleType

So this is a combination of data from the Team and TeamUser domain models, because the view contains roleType (and maybe more later).

How would I model this? And in which layer will this happen (service, repository, controller)? Because there is a difference in my database design and the actual presentation view.

Upvotes: 2

Views: 345

Answers (2)

msmani
msmani

Reputation: 720

Adding to what Jan Muncinsky said, you can explore CQRS, where you will be using two separate models, one for command and one for query, in that way your domain model won't be constrained by the constantly changing UI queries.

Upvotes: 0

Jan Muncinsky
Jan Muncinsky

Reputation: 4408

How would I model this?

You should create new DTO projection model for dedicated view that fits exactly its needs. That means something like - TeamDto with attributes - id - name - roleType

And in which layer will this happen (service, repository, controller)?

You will project result of your database query (querying only needed data) to DTO in your repository. If you do it in latter stage (service, controller), that most probably means that you read inefficiently data from DB (e.g. loading whole Team and TeamUser and than combining them together)

Upvotes: 2

Related Questions