DZone
Mobile Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Mobile Zone > More on Bad Ways to Set Up Commands in Your View Model

More on Bad Ways to Set Up Commands in Your View Model

Matt Lacey user avatar by
Matt Lacey
·
Dec. 04, 12 · Mobile Zone · Interview
Like (0)
Save
Tweet
3.56K Views

Join the DZone community and get the full member experience.

Join For Free

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

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 occasionally, 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...

Command (computing) View model

Published at DZone with permission of Matt Lacey, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • API Security Weekly: Issue 165
  • How Low Code Demands More Creativity From Developers
  • How To Evaluate Software Quality Assurance Success: KPIs, SLAs, Release Cycles, and Costs
  • SQL vs. NoSQL: Pros and Cons

Comments

Mobile Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo