Using View-Models with Repository pattern

后端 未结 3 577
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-01 05:51

I\'m using Domain driven N-layered application architecture with EF code first in my recent project, I defined my Repository contracts, In Domain

3条回答
  •  北荒
    北荒 (楼主)
    2021-02-01 06:13

    Well, to me I would map the ViewModel into Model objects and use those in my repositories for reading / writing purposes, as you may know there are several tools that you can do for this, in my personal case I use automapper which I found very easy to implement.

    try to keep the dependency between the Web layer and Repository layers as separate as possible saying that repos should talk only to the model and your web layer should talk to your view models.

    An option could be that you can use DTO's in a service and automap those objects in the web layer (it could be the case to be a one to one mapping) the disadvantage is that you may end up with a lot of boilerplate code and the dtos and view models could feel duplicated.

    the other option is to return partial hydrated objects in your model and expose those objects as DTOs and map those objects to your view models this solution could be a little bit obscure but you can make the projections you want and return only the information that you need.

    you can get rid of the of the view models and expose the dtos in your web layer and use them as view models, less code but more coupled approach.

提交回复
热议问题