mvvm - Expose an enum from Models in ViewModel in C# -
i have xamarin project that's composed of models project, viewmodels project, , "view" project each platform app released on. models project linked view models project, not (directly) used views projects.
the problem have pair of enums in models project i'd utilize in views, without linking views projects model. there way expose these enum types view?
what i'm doing copying/pasting , type-casting model enums identical enums in viewmodel, feels hack-ish , break if new enum added model not viewmodel. there improve solution, besides creating new constants/enums project link others to?
what i'd do:
//model project enum myenum{ enum1, enum2 } class modelobject{ myenum status; } //viewmodel project class vmobject{ myenum status = modelobject.status; } //view project if(vmobject.status == myenum.enum1){ //... }
what do:
//model project enum myenum{ enum1, enum2 } class modelobject{ myenum status; } //viewmodel project enum myvmenum{ enum1, enum2 } class vmobject{ myvmenum status = (myvmenum)modelobject.status; } //view project if(vmobject.status == myvmenum.enum1){ //... }
well should fall under responsibility of viewmodel. whole point of viewmodel transform model domain view domain able digest. if means making new enum it. automapper helps.
on side note. find using enum, outside of view-domain, bit of bad pattern anyway, brings logic view. don't expose view knowing (directly) enum types. if need have view alter behaviour due specific enum value, have logic in viewmodel.
update:
you goal view should remove , business logic. in case, saying want view alter icon based on model state. issue seem facing don't want view have direct access model enum type. don't, , reverse thinking around. have view uses own enum:
enum viewicons { iconone, icontwo, }
... which, your viewmodel
can set
view.icon = viewicons.iconone
so lets in viewmodel update
void update() { switch(_model.state) { case modelstate.inactive: view.icon = viewicons.iconone ...
why way? well, if remove model domain , replace different (think that), have refactor viewmodel domain. view domain classes non-the-wiser.
c# mvvm enums visual-studio-2013 xamarin
No comments:
Post a Comment