Should I map a domain object to a view model using an optional constructor?

后端 未结 2 1843
余生分开走
余生分开走 2021-02-09 17:19

I\'d like to be able to map a domain model to a view model by newing up a view model and passing in the contributing domain model as a parameter (like the code below). My motiva

2条回答
  •  南笙
    南笙 (楼主)
    2021-02-09 18:11

    Your friend is right. Views should be dumb and not know anything about your domain model.

    Have you tried using Automapper to map your business/domain entities/models to your dto/viewmodels?

    More details because of comment:

    Putting mapping code in your viewmodels violates the separation of concern, the SOLID single responsibility principal, the MVC pattern, and domain driven design principals. Views have one responsibility, get data to the screen, thats it. IMHO there isn't much to argue about. Its simply a bad idea that violates a lot of core software development principals.

提交回复
热议问题