MVVM ViewModel and String code smell

MVVM ViewModel and String code smell

Old forum URL:

jamie.clayton posted on Tuesday, August 16, 2011


I see there have been a few discussions around using strings in the ViewModel parts of Csla.Xml namespace.

I wonder if one of the ways around the use of strings in the ViewModel Async method calls might be to use a code generator (T4) to write all the strings out to a MetaData file. For instance Daneil Vaughan has created some templates already.

What do you think?

RockfordLhotka replied on Tuesday, August 16, 2011

Yeah, with the sync methods we were able to accept a lambda expression, and so the factory method can be passed as a parameter.

The async factory methods are complex, because they require a callback parameter that includes a double-indirected generic type. If I remember correctly, you can still use a lambda in .NET (WPF), but not Silverlight. It has been a while since I looked at this though, so I could be mis-remembering.

Code generation is often a solution for magic strings - at least of a sort. Not ideal, but sometimes as good as can be done.

Peran replied on Wednesday, August 17, 2011

Csla.Xaml.ViewModelBase<T> supports lambda expressions to avoid the use of strings for async and sync factory methods


            this.BeginRefresh(handler => ClientEdit.BeginGetItem(clientId, userState, handler));


            this.DoRefresh(() => ClientEdit.GetItem(clientId));


Copyright (c) Marimer LLC