Is this the best approach

Topics: Getting Started
Mar 25, 2012 at 9:00 PM

Hello,

I have an AccountViewModel, like the following:

    public class AddAccountViewModel : ScreenPageViewModelBase
    {
        /// <summary>
        /// Local variable for the userName
        /// </summary>
        private string userName;

        /// <summary>
        /// Local variale for the password
        /// </summary>
        private string password;

        /// <summary>
        /// Initializes a new instance of the AddAccountViewModel class
        /// </summary>
        /// <param name="viewModelWorker">The View Model Worker from common access properties</param>
        public AddAccountViewModel(ViewModelWorker viewModelWorker)
            : base(viewModelWorker)
        {
        }

        /// <summary>
        /// Gets or sets the username of the person logging into site
        /// </summary>
        public string Username
        {
            get
            {
                return this.userName;
            }

            set
            {
                this.userName = value;
                NotifyOfPropertyChange(() => this.Username);
                NotifyOfPropertyChange(() => this.CanSaveAndNavigateToProfileView);
            }
        }

        /// <summary>
        /// Gets or sets the password for the person logging into site
        /// </summary>
        public string Password
        {
            get
            {
                return this.password;
            }

            set
            {
                this.password = value;
                NotifyOfPropertyChange(() => this.Password);
                NotifyOfPropertyChange(() => this.CanSaveAndNavigateToProfileView);
            }
        }

        /// <summary>
        /// Gets a value indicating whether all details have been entered. Used by Caliburn.Micro
        /// </summary>
        public bool CanSaveAndNavigateToProfileView
        {
            get
            {
                if (string.IsNullOrEmpty(this.Username) == true ||
                    string.IsNullOrEmpty(this.Password))
                {
                    return false;
                }

                return true;
            }
        }
   }

Then, in another ViewModel, I want to be able to re-use the AddAccountViewModel, so I have the following in the constructor for the ProfileViewModel

        public ProfileViewModel(ViewModelWorker viewModelWorker, AddAccountViewModel addAccountViewModel)
            : base(viewModelWorker)
        {
        }

And this works really well in the sense that I can get access to the properties of the Account.  I use the Username and Password to log into a site and download information about the user, however, I would like to be able to edit other properties that I would like to add to the Account object.  For instance, I want to download information about the number of posts a user has made, and also there current reputation score, and add these properties once I download them from the site.

Is it "correct", to use the approach that I am using above?  Or should I do something else?

If I should be doing something else, can someone provide an example of the approach that I should be using?

Thanks in advance!

Gary