Wednesday, July 10, 2013

Awesome clean ViewModels via Fody

Sometimes ViewModels can be a bit verbose... e.g. sometimes achieving a simple FirstName and LastName combination can require:

public class FirstViewModel
: MvxViewModel
{
private string _firstName;
public string FirstName
{
get { return _firstName; }
set { _firstName = value; RaisePropertyChanged(() => FirstName); RaisePropertyChanged(() => FullName); }
}
private string _lastName;
public string LastName
{
get { return _lastName; }
set { _lastName = value; RaisePropertyChanged(() => LastName); RaisePropertyChanged(() => FullName); }
}
public string FullName
{
get { return string.Format("{0} {1}", _firstName, _lastName); }
}
}


Because of this, sometimes some of us find ourselves thinking wouldn't it be nice if our ViewModels were thinner - e.g. a bit more like:

public class FirstViewModel
: MvxViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName
{
get { return string.Format("{0} {1}", FirstName, LastName); }
}
}


Well, @TwinGuille didn't just think this - he went off and coded it!

Using the tools from @SimonCropp's excellent Fody - https://github.com/Fody - @TwinGuille posted an awesome blog post on how to achieve thinner, slimmer, easier-to-code, easier-to-understand ViewModels.

This is 100% awesome - check it out at http://twincoders.com/blog/codigo-limpio-con-fody/ - well deserving of a badge of awesomeness.


No comments:

Post a Comment