0

I find myself writing a lot of WPF apps with MVVM pattern lately. A common, and tedious, task is wrapping a domain object in a view model. I do this a lot:

public class DomainObject
{
    public virtual int IntProperty { get; set; }
    public virtual string StringProperty { get; set; }
}

public class DomainObjectViewModel : ViewModelBase
{
    public int IntProperty
    {
        get { return DomainObject.IntProperty; }
        set
        {
            if (Equals(value, DomainObject.IntProperty)) return;
            DomainObject.IntProperty = value;
            OnPropertyChanged(nameof(IntProperty));
        }
    }

    public string StringProperty
    {
        get { return DomainObject.StringProperty; }
        set
        {
            if (Equals(value, DomainObject.StringProperty)) return;
            DomainObject.StringProperty = value;
            OnPropertyChanged(nameof(StringProperty));
        }
    }
}

MVVM by it's nature requires a lot of typing. I have a bunch of live templates and other tricks to reduce that, but something I would love is the ability to copy my domain object properties, paste them into the view model class, highlight them, and transform them as needed. Is it possible?

Michael
  • 1,803
  • 1
  • 17
  • 26
  • 1
    You don't _need_ to wrap Models with ViewModels. Simply exposing models directly to View has no drawbacks as far as I know. I have been unit testing and doing other things without issues. – wingerse Apr 29 '16 at 18:19
  • Need to support INotifyPropertyChanged to ensure changes made are propagated to the view. My domain is POCO, and I don't want to pollute it with PropChanged - and sometimes I just can't. – Michael Apr 29 '16 at 18:21
  • You should create Model that has INotifyPropertyChanged. Otherwise you are really going to cripple yourself. Then if something needs your Domain object, you can Map to it, but normally the Model is a business object with logic and the only mapping required to a POCO is for your DTO. MVVM - Model | View | ViewModel – TyCobb Apr 29 '16 at 18:27
  • @TyCobb The Model is used in a lot of situations, the UI presentation is just one of many. Making a method call, checking for subscribers, and raising an event every time a property is changed on the domain object is wasteful, and not needed 90% of the time. Additionally, sometimes I cannot change the domain. Either way - I am of the school of thought that INotifyPropChanged doesn't belong on the Model. – Michael Apr 29 '16 at 18:34
  • 1
    Do you consider using aspect-oriented paradigm (PostSharp as the most known representative)? With it you just decorate your class with one attribute and done. You can also consider various methods to create dynamic proxies for your POCO objects. – Evk Apr 29 '16 at 18:35
  • It's really your call. http://stackoverflow.com/questions/6922130/in-mvvm-model-should-the-model-implement-inotifypropertychanged-interface – TyCobb Apr 29 '16 at 18:35
  • So maybe we can focus on the actual question rather than trying to impart one's programming style? I did not ask for advice on how to design my software, I asked a question about resharper capabilities. – Michael Apr 29 '16 at 18:39
  • Not through ReSharper. Best bet would be to install AutoHotkey or something similar and write code to read your class from the clipboard and then create required text either as a new file or set it back to the clipboard. I would say that we were trying to answer your question by trying to warn and suggest ways that won't pigeon-hole you. Your ViewModel as an example, looks more like a wrapper for your domain rather than a view-model that controls the view. – TyCobb Apr 29 '16 at 18:45
  • @TyCobb It is a contrived example, to be sure. More of a vessel to illustrate the question, which has broader applications than just creating View Models. For sure Resharper or VS could be extended to do what I'm looking for without resorting to keystroke macros. – Michael Apr 29 '16 at 18:47
  • They easily could be if you code a plugin, but I have never come across an existing ReSharper feature for that yet. I have always had to resort to custom live templates which it sounds like you are already doing. – TyCobb Apr 29 '16 at 18:50

0 Answers0