Replacing ViewModels with Tuples

醉酒当歌 提交于 2020-01-25 08:31:05

问题


I've just started on a new project which currently contains a fair few ViewModel DTO classes.

I'm wondering whether it would be useful and even good practice to replace many of these DTOs with Tuples and to use that as my method of transfering a bunch of objects to my views.

What do you think? Is this a correct usage of the tuple or am I off the mark here?


回答1:


Usually view models have metadata associated with them which among other allows you to perform validation and integrates with editor/display templates. Tuples don't seem appropriate. Another disadvantage is that they express less clearly the purpose of each property in the view model (Model.Item1, Model.Item2, ..., no-one else than the original developer can make any sense of this, and if he is like me after few days even he won't be able to make sense).




回答2:


One of the issues I see with using a Tuple, is that in your view:

inherits="ViewPage<Tuple<Model1, Model2>>"

...etc, when you use these models you have to access them as:

Model.Item1,
Model.Item2

...which isn't as clear as using a nice strongly-typed view model.




回答3:


In C#7 you can now name the items - below code is pretty much a free mapper which I think can replace DTOs that don't need any behaviour being attached to them.

    public (string first, string last, int age) GetTuple()
    {
        return ("John", "Connor", 1);
    }


来源:https://stackoverflow.com/questions/3188360/replacing-viewmodels-with-tuples

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