Click here to Skip to main content
15,886,362 members
Articles / Desktop Programming / WPF

From Russia with Love – Retrieving ViewModel Objects from a Model Assembly

Rate me:
Please Sign up or sign in to vote.
4.98/5 (31 votes)
7 Sep 2009CPOL6 min read 114.9K   365   68  
Reviews the “From Russia with Love” technique of simplifying the creation of ViewModel objects from other libraries, without compromising your MVVM architecture
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;
using ModelLib;
using MvvmFoundation.Wpf;

namespace TransformingModelToVM
{
    public class CommunityViewModel : ObservableObject
    {
        #region Methods

        public CommunityViewModel()
        {
            _memberCommandsInternal = new ObservableCollection<CommandViewModel>();
            this.MemberCommands = new ReadOnlyObservableCollection<CommandViewModel>(_memberCommandsInternal);

            // Create the command that is used by each CommandViewModel in MemberCommands.
            _selectMemberCommand = new RelayCommand<Person>(this.SelectMember);

            // Create the data source and begin a call to fetch and transform Person objects.
            var dataSource = new PersonDataSource();
            dataSource.RetrieveAndTransformAsync(
                _memberCommandsInternal,         // output
                this.CreateCommandForPerson,     // transform
                () => this.IsLoadingData = false // onCompleted
                ); 

            this.IsLoadingData = true;
        }

        void SelectMember(Person person)
        {
            // In a real app this method would do something with the selected Person.
            string msg = String.Format("You selected '{0}'.", FormatName(person));
            MessageBox.Show(msg, "Congratulations!");
        }

        CommandViewModel CreateCommandForPerson(Person person)
        {
            return new CommandViewModel(_selectMemberCommand, FormatName(person), person);
        }

        static string FormatName(Person person)
        {
            return String.Format("{0}, {1}", person.LastName, person.FirstName);
        }

        #endregion // Methods

        #region Properties

        /// <summary>
        /// Returns true if the community member data is
        /// currently being loaded from a data source.
        /// </summary>
        public bool IsLoadingData
        {
            get { return _isLoadingData; }
            private set
            {
                if (value.Equals(_isLoadingData))
                    return;

                _isLoadingData = value;

                base.RaisePropertyChanged("IsLoadingData");
            }
        }

        /// <summary>
        /// Returns a read-only collection of command objects, each of which represents a 
        /// member of the community.  When a command executes, it 'selects' the associated member.
        /// </summary>
        public ReadOnlyObservableCollection<CommandViewModel> MemberCommands { get; private set; }

        #endregion // Properties

        #region Fields

        bool _isLoadingData;
        readonly ObservableCollection<CommandViewModel> _memberCommandsInternal;
        readonly ICommand _selectMemberCommand;

        #endregion // Fields
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
Josh creates software, for iOS and Windows.

He works at Black Pixel as a Senior Developer.

Read his iOS Programming for .NET Developers[^] book to learn how to write iPhone and iPad apps by leveraging your existing .NET skills.

Use his Master WPF[^] app on your iPhone to sharpen your WPF skills on the go.

Check out his Advanced MVVM[^] book.

Visit his WPF blog[^] or stop by his iOS blog[^].

See his website Josh Smith Digital[^].

Comments and Discussions