Architecture Domain Model and View Model

和自甴很熟 提交于 2021-01-27 19:40:22

问题


I am trying to build application by spring boot and Domain Driven Design. I have a problem about Domain model (match with fields of table DB) and View Model (response API).

Domain Model:

EX: class Name

@Getter 
@NoArgsConstructor
@AllArgsConstructor
class Name {
  String value;
}

class Product

@Getter 
@NoArgsConstructor
@AllArgsConstructor
class Product{
  Name name;
}

ViewModel:

@Data
@NoArgsConstructor
@AllArgsConstructor
class ProductView {
  //int prodId;
  String prodName;
}

Select data DB by class Product, builder to Response API by class ProductView. When that convert from DomainModel to ViewModel or vice versa, I written static method in ProductView for that. It will become:

@Data
@NoArgsConstructor
@AllArgsConstructor
class ProductView {
  //int prodId;
  String prodName;
  public static ProductView of(Product prod) {
    String productName = prod.getName().getValue();
    return new ProductView(productName)
  }
}

It works well, but when the data becomes more. I think need that as CommonConvert from DomainModel to ViewModel and vice versa.

I have a solution use Mapstruct library. But Mapstruct only support to convert field same type(String with String, ex). What is the best solution for writting CommonConvert?


回答1:


My advice: do not query domain models and translate them to view models for reading.

Domain model classes (e.g. aggregates) are used to represent business data and behaviour with the to purpose to adhere to business invariants when creating or changing such business entities.

For building your view models from your persistent data you can - and in my opinion you should - bypass the domain model. You can safely read the data from your database as you need it without going through domain repositories.

This is okay because you can't violate business rules by just reading data. For writing data go through domain repositories and aggregates.

In your case you can of course use view model entities using JPA annotations by designing those classes to exactly fit your viewing requirements. Keep in mind that view models often don't correlate to domain models as they might only need a subset of the data or aggregate data from different aggregates.

Another catch is that if you need to query many objects for viewing can quickly cause performance issues if you query full domain aggregates via repositories. As such aggregates always load all data from it's child entities and value objects as well to allow for performing business logic with all invariants you would end up performing lots of expensive queries which are suited for loading a single aggregate but not many of them at once.

So by querying only what you need for viewing you also address such performance issues.

When following DDD you should usually create or change only one aggregate within a business transaction. So domain models are not suited for query optimization but for keeping the business invariants in tact when writing business data.

View models and corresponding queries are optimized for reading and collecting all data required.




回答2:


Simply map like this (with mapstruct) :

@Mapping(source = "name.value", target = "prodName")
public abstract ProductView toProductView(Product model);


来源:https://stackoverflow.com/questions/63575868/architecture-domain-model-and-view-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!