Question

I have data that represents a button. These data are composed of elements like id (string), text (string), position on screen (numeric values), image url (string), size (numeric value). It also has some domain-related data. At first, I wanted to create two classes, the first one having all the non graphical data, the second one having all the graphical data (so I would avoid mixing unrelated kind of data). But then, I find myself with two classes.

Should I leave things like that, or should I merge them, so my life is easier (but then, it would not be very compliant to the Single Responsability Rule...) ?

Was it helpful?

Solution

This is in fact, a very common practice. They are called ViewModels, and they are exactly for what you're using them for - view specific state.

To help ease conversion, there are libraries like AutoMapper that can automatically map between a ViewModel and a Domain Model. This saves you wiring it all up yourself and writing even more boilerplate code.

TLDR: It's common to have the two classes. Don't stress about it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top