Clean Architecture: Are some use cases simply converting objects?

Hi.

Say we have an entity Item, and a use case “getItem(id)”. The use case fetches data from the database, and prepares a response model. So far so good.

An Item entity looks like this:

type ItemEntity = { id: number name: string } 

When we’re crossing boundaries, we’re advised to use data transfer objects (DTOs), so when the use case gets back data from the database, the data looks something like this:

type ItemRepoDTO = { id: number createdAt: string, name: string } 

The response model looks something like this:

type ItemResponseModel = { id: number name: string } 

So the use case gets data from the database, and should return an ItemResponseModel. I’d simply implement the use case something like this:

class ItemsUseCase { const itemsRepo: ItemsRepoInterface constructor(itemsRepo) { this.itemsRepo = itemsRepo } function getItem(id) { const item = this.itemsRepo.getItemById(id) const requestModel: RequestModel = { id: item.id, name: item.name } return requestModel } } 

So the use case doesn’t even create an Item entity object, it simply converts the ItemRepoDTO to a RequestModel object.

To me it seems I’m missing something here – have a Items use case that doesn’t even reference any Item entity doesn’t sound right.

I could of course create an object of type Entity, but what would be the point?

If anyone could shed some light on how use cases, DTOs and entities should play together, I’d really appreciate it.

submitted by /u/protoken
[link] [comments]


Go to Source of this post
Author Of this post: /u/protoken
Title Of post: Clean Architecture: Are some use cases simply converting objects?
Author Link: {authorlink}