Monday, December 03, 2012

More on my opinion about commands

Last week I wrote about  "A bad way to set up commands in your view model".
This prompted a few comments and strong opinions in both the comments and on twitter.

My main intention with the post was originally to rally against creating properties with a private setter when they will only ever be set once and the code that does the setting is kept apart from the property.

All things being equal I'd rather maintain code that looks like my preferred code than the first example.

I do, however,appreciate that this is not optimal in all situations. I've just spent lots of time working with code that does trivial things in the command or are executed occassionally and so the overhead of creating a new command each time it's needed is not an issue.

For the avoidance of doubt, I'm perfectly fine with any of the following when used at an appropriate time.
    public class MyViewModel : ViewModelBase
    {
        private RelayCommand myCommand;

        public RelayCommand MyCommand
        {
            get
            {
                if (myCommand == null)
                {
                    myCommand = new RelayCommand(() =>
                    {
                        // some functionality here
                    });
                }

                return myCommand;
            }
        }
    }
or
    public class MyViewModel : ViewModelBase
    {
        private RelayCommand myCommand;

        public RelayCommand SaveCommand
        {
            get
            {
                if (myCommand == null)
                {
                    myCommand = new RelayCommand(this.Save);
                }

                return myCommand;
            }
        }

        private void Save()
        {
            // do something
        }
    }
and I've used both in the past.


Anyway, moving on...


7 comments:

  1. Or better yet

    public class MyViewModel : ViewModelBase
    {
    private RelayCommand myCommand;

    public RelayCommand SaveCommand
    {
    get
    {
    return myCommand ?? (myCommand = new RelayCommand(this.Save));
    }
    }

    private void Save()
    {
    // do something
    }
    }

    ReplyDelete
  2. @Jamie yep that works fine too.

    As a personal preference, I'm not a fan of using null coalescing to set backing fields but maybe that's me.

    ReplyDelete
  3. I think your public property should be an ICommand not a RelayCommand - ICommand is part of your interface, RelayCommand just happens to be how you implement that interface.

    ReplyDelete
  4. Outstanding publish and amazing weblog, buy essays cheap I really like this kind of exciting content keep it up.

    ReplyDelete
  5. Quickly, the publish is really the biggest on this laudable subject best custom essay writing service. I recognize with your results and will thirstily look ahead to your upcoming up-dates.

    ReplyDelete
  6. oakley sunglasses cheap, http://www.oakley.in.net/
    ray ban sunglasses sale, http://www.raybansunglass.us.com/
    true religion jeans, http://www.truereligionjeanscanada.com/
    snow boots outlet, http://www.wintercoats.us.com/
    coach outlet store, http://www.coachoutletstores.com.co/
    stuart weitzman sale, http://www.stuartweitzmanoutlet.us/
    giuseppe zanotti sale, http://www.giuseppe-zanotti-outlet.org/
    ugg outlet, http://www.uggoutletstore.eu.com/
    asics, http://www.asicsisrael.com/
    parajumpers sale, http://www.woolrichoutletstore.us.com/
    los angeles clippers, http://www.clippersjerseystore.com/
    tiffany jewelry, http://www.tiffany-outlet.us.com/
    calvin klein,calvin klein outlet online,calvin klein wholesale,calvin klein shoes,calvin klein underwear,calvin klein coats,calvin klein handbags
    cheap ugg boots, http://www.cheapuggboots.net.co/
    michael kors uk, http://www.michaelkorsoutlet.org.uk/
    ray ban sunglasses,ray ban sunglasses outlet,ray ban,rayban,ray bans,ray ban outlet,ray-ban,raybans,ray ban wayfarer,ray-ban sunglasses,raybans.com,rayban sunglasses,cheap ray ban
    arizona cardinals, http://www.arizonacardinalsjersey.us/
    manchester united jersey, http://www.manchesterunitedonline.com/
    kkk10 13

    ReplyDelete