ViewModels, CQRS and Entities

狂风中的少年 提交于 2019-12-06 04:31:30

One possible way to deal with less classes and mappings / projections / conversions:

For all the views used in the WRITE-SIDE of your application (the views that allow the user to submit a form or so), let the Command to be their Model (View Model).

That is, you can have:

[HttpPost]
public ActionResult Create(CreateStudent command) {
    commandHandler.Execute(command);
}

As for the get action, I see that you have to populate a drop-down list...

[HttpGet]
public ActionResult Create() {
    // var model = create the view model somehow
    return View(model);
}

Now, as for the model in the latter snippet, you may have these options (and perhaps others):

  • let the Command object (CreateStudent) be the model of the view (as the view is a... view for a command, a view for a request) and pass the items for the drop-down using the ViewBag
  • derive from CreateStudent command just to make of it a view model that can also hold the items for the drop-down

Notice that there is no need to add the suffix "Command" to your command object. Just give it a name that means doSomething - a verb phrase (verb + object) - and it should be fine.

In the end, some command objects really are the models for some views - no need to define another view-models for those and then have a lot of duplication etc.

Also, you may find these interesting:

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