MVC Using Domain Models in View Models

前端 未结 3 1815
长情又很酷
长情又很酷 2021-01-04 11:47

Is the following OK to do? I know Domain Models should never be used in views but is it ok to use Domain Models in your View Models? For some very small models it doesn\'t s

3条回答
  •  误落风尘
    2021-01-04 12:12

    It depends on what you mean by "Domain model". Do you mean EF entities? Or do you mean business layer objects?

    It's never a good idea to pass EF entities to the view, particularly if you're using default model binding. This can create security issues if you are not careful. Although the same issues can occur if you're not careful with business objects passed to the view.

    One of the huge advantages of view models is that you have much finer control over mapping of data, so you can validate more easily that only the correct maps occur.

    It all comes down to your app though. If it's a simple app, then it may not be worth the trouble of doing more complex mappings. If it's a complex app, that must live for a long time, and will likely to be updated a lot.. then you should definitely invest the effort.

提交回复
热议问题