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?
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).
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.
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);
}