Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6964233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:53:55+00:00 2026-05-27T15:53:55+00:00

I have a simple MVVM architecture where I’m using a view model to bind

  • 0

I have a simple MVVM architecture where I’m using a view model to bind commands to a xaml view. My commands are fairly generic and I’ll be binding them to a couple of views so I’ve put them into their own classes implementing the ICommand interface. Inside of the view model I have public accessors like so:

    private ICommand _myCommand;
    public ICommand MyCommand
    {
        get
        {
            if (_myCommand == null)
            {
                _myCommand = new MyCommand(_injectedModel);
            }

            return _myCommand ;
        }
    }

This all works and I’m happy with the architecture but … I have tons of these functions. They all roughly do the same thing – expose the command as a public property, check if a private command already exists and if so use it otherwise create a new command. It’s a bit “boilerplate” for my taste.

I would like a nice way to abstract this. I could create a method that looks up commands based on an identifier from some sort of command map. I could just create all of my commands in the view models constructor (rather than doing so lazily).

What would you consider best practice? Should I avoid instantiating a new command inside each view model and have a central command lookup?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:53:56+00:00Added an answer on May 27, 2026 at 3:53 pm

    I could just create all of my commands in the view models constructor (rather than doing so lazily).

    I often do this. The cost of a command is fairly cheap, in most implementations. Depending on your implementation, it’s likely just a small class with a single delegate reference (or a pair of delegate references). This is unlikely to be significant enough overhead, IMO, to warrant the lazy construction.

    I often write this as:

    // Constructor
    public MyViewModel()
    {
        this._injectedModel = SetModel();
    
        this.MyCommand = new MyCommand(_injectedModel); 
    }
    
    ICommand MyCommand { get; private set; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Silverlight with the MVVM. I have a simple LoginControl bound to a
I'm writing a Silverlight app using the MVVM pattern. I have a main view
I have an app with simple MVVM implementation with ListView on my View. I
I'm just playing around with WPF and MVVM, and I have made a simple
I have created a simple MVVM, with only three classes CashFlowView, CashFlowViewModel, CashFlowModel. I
I am implementing a simple MVVM WPF application with simple databrowsing commands. There is
Trying to solve a very simple problem using mvvm-light, but after days of sifting
imagine the following simple Models (example for simplicity reasons; in fact, we have MVVM
I have a WPF application using MVVM. I have some user controls that show
I have an application all done in WPF, using MvvM/Prism/Unity and Remote as datasource.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.