Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

A Sample Silverlight 4 Application Using MEF, MVVM, and WCF RIA Services - Part 1

, 7 Jul 2011
Part 1 of a series describing the creation of a Silverlight business application using MEF, MVVM Light, and WCF RIA Services.
IssueVisionforSilverlightSetup20110630.zip
IssueVision for Silverlight Setup
IssueVisionSetup.msi
setup.exe
IssueVision_for_Silverlight_setup.zip
IssueVisionSetup.msi
setup.exe
IssueVision_Silverlight20110630.zip
IssueVision.Silverlight
Assemblies
GalaSoft.MvvmLight.Extras.SL4.dll
GalaSoft.MvvmLight.SL4.dll
MenuControls.dll
System.Windows.Controls.DataVisualization.Toolkit.dll
System.Windows.Controls.Toolkit.dll
System.Windows.Controls.Toolkit.Internals.dll
System.Windows.Interactivity.dll
Database
IssueVision.Client
IssueVision.Client.csproj.user
Assets
Signin.png
Controls
Properties
Views
IssueVision.Common
IssueVision.Common.csproj.user
Controls
Helpers
Models
Properties
Resources
Themes
IssueVision.Data
IssueVision.Data.csproj.user
Properties
Web
Resources
IssueVision.Data.Web
IssueVision.edmx
Helpers
MetadataClasses
Properties
Resources
Services
IssueVision.Model
IssueVision.Model.csproj.user
Properties
IssueVision.Setup
IssueVisionSetup.vdproj
IssueVision.ViewModel
IssueVision.ViewModel.csproj.user
Properties
IssueVision.Web
IssueVision.Web.csproj.user
Properties
IssueVision_Silverlight_20110316.zip
GalaSoft.MvvmLight.Extras.SL4.dll
GalaSoft.MvvmLight.SL4.dll
MenuControls.dll
System.Windows.Controls.DataVisualization.Toolkit.dll
System.Windows.Controls.Toolkit.dll
System.Windows.Controls.Toolkit.Internals.dll
System.Windows.Interactivity.dll
IssueVision.Client.csproj.user
Signin.png
DataSources
IssueVision.Data.Web.IssueVisionContext.datasource
IssueVision.Data.Web.PasswordResetContext.datasource
IssueVision.Common.csproj.user
IssueVision.Data.csproj.user
IssueVision.edmx
DataTypes
IssueVision.Model.csproj.user
IssueVisionSetup.vdproj
IssueVision.ViewModel.csproj.user
IssueVision.Web.csproj.user
IssueVision_Silverlight_20110606.zip
GalaSoft.MvvmLight.Extras.SL4.dll
GalaSoft.MvvmLight.SL4.dll
MenuControls.dll
System.Windows.Controls.DataVisualization.Toolkit.dll
System.Windows.Controls.Toolkit.dll
System.Windows.Controls.Toolkit.Internals.dll
System.Windows.Interactivity.dll
IssueVision.Client.csproj.user
Signin.png
IssueVision.Common.csproj.user
IssueVision.Data.csproj.user
IssueVision.edmx
IssueVision.Model.csproj.user
IssueVisionSetup.vdproj
IssueVision.ViewModel.csproj.user
IssueVision.Web.csproj.user
IssueVision_Silverlight_Setup.zip
IssueVisionSetup.msi
setup.exe
using System;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Linq;
using System.ServiceModel.DomainServices.Client;
using IssueVision.Data.Web;
using IssueVision.Common;

namespace IssueVision.Model
{
    [Export(typeof(IIssueVisionModel))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class IssueVisionModel : IIssueVisionModel
    {
        #region "Private Data"
        private IssueVisionContext _ctx;
        #endregion "Private Data"

        #region "Protected Propertes"
        protected IssueVisionContext Context
        {
            get
            {
                if (_ctx == null)
                {
                    _ctx = new IssueVisionContext();
                    _ctx.PropertyChanged += _ctx_PropertyChanged;
                }

                return _ctx;
            }
        }
        #endregion "Protected Propertes"

        #region "IIssueVisionModel Interface implementation"

        public void GetIssueTypesAsync()
        {
            PerformQuery(Context.GetIssueTypesQuery(), GetIssueTypesComplete);
        }

        public void GetPlatformsAsync()
        {
            PerformQuery(Context.GetPlatformsQuery(), GetPlatformsComplete);
        }

        public void GetResolutionsAsync()
        {
            PerformQuery(Context.GetResolutionsQuery(), GetResolutionsComplete);
        }

        public void GetStatusesAsync()
        {
            PerformQuery(Context.GetStatusesQuery(), GetStatusesComplete);
        }

        public void GetSubStatusesAsync()
        {
            PerformQuery(Context.GetSubStatusesQuery(), GetSubStatusesComplete);
        }

        public void GetUsersAsync()
        {
            PerformQuery(Context.GetUsersQuery(), GetUsersComplete);
        }

        public void GetCurrentUserAsync()
        {
            PerformQuery(Context.GetCurrentUserQuery(), GetCurrentUserComplete);
        }

        public void GetSecurityQuestionsAsync()
        {
            PerformQuery(Context.GetSecurityQuestionsQuery(), GetSecurityQuestionsComplete);
        }

        public void GetMyIssuesAsync()
        {
            PerformQuery(Context.GetMyIssuesQuery(), GetMyIssuesComplete);
        }

        public void GetAllIssuesAsync()
        {
            PerformQuery(Context.GetIssuesQuery(), GetAllIssuesComplete);
        }

        public void GetAllUnresolvedIssuesAsync()
        {
            PerformQuery(Context.GetAllUnResolvedIssuesQuery(), GetAllUnresolvedIssuesComplete);
        }

        public void GetActiveBugCountByMonthAsync(int numberOfMonth)
        {
            Context.GetActiveBugCountByMonth(numberOfMonth, s =>
            {
                if (GetActiveBugCountByMonthComplete != null)
                {
                    try
                    {
                        Exception ex = null;
                        if (s.HasError)
                        {
                            ex = s.Error;
                            s.MarkErrorAsHandled();
                        }
                        GetActiveBugCountByMonthComplete(this, new InvokeOperationEventArgs(s, ex));
                    }
                    catch (Exception ex)
                    {
                        GetActiveBugCountByMonthComplete(this, new InvokeOperationEventArgs(ex));
                    }
                }
            }, null);
        }

        public void GetResolvedBugCountByMonthAsync(int numberOfMonth)
        {
            Context.GetResolvedBugCountByMonth(numberOfMonth, s =>
            {
                if (GetResolvedBugCountByMonthComplete != null)
                {
                    try
                    {
                        Exception ex = null;
                        if (s.HasError)
                        {
                            ex = s.Error;
                            s.MarkErrorAsHandled();
                        }
                        GetResolvedBugCountByMonthComplete(this, new InvokeOperationEventArgs(s, ex));
                    }
                    catch (Exception ex)
                    {
                        GetResolvedBugCountByMonthComplete(this, new InvokeOperationEventArgs(ex));
                    }
                }
            }, null);
        }

        public void GetActiveBugCountByPriorityAsync()
        {
            Context.GetActiveBugCountByPriority(s =>
            {
                if (GetActiveBugCountByPriorityComplete != null)
                {
                    try
                    {
                        Exception ex = null;
                        if (s.HasError)
                        {
                            ex = s.Error;
                            s.MarkErrorAsHandled();
                        }
                        GetActiveBugCountByPriorityComplete(this, new InvokeOperationEventArgs(s, ex));
                    }
                    catch (Exception ex)
                    {
                        GetActiveBugCountByPriorityComplete(this, new InvokeOperationEventArgs(ex));
                    }
                }
            }, null);
        }

        public Issue AddNewIssue()
        {
            var g = new Issue
            {
                IssueID = 0,
                OpenedDate = DateTime.Now,
                OpenedByID = WebContext.Current.User.Identity.Name,
                LastChange = DateTime.Now,
                ChangedByID = WebContext.Current.User.Identity.Name,
                AssignedToID = null,
                Priority = 2,
                Severity = 2,
                StatusID = 0,
                SubStatusID = null
            };
            Context.Issues.Add(g);
            return g;
        }

        public void RemoveAttribute(Data.Web.Attribute attribute)
        {
            if (Context.Attributes.Contains(attribute))
                Context.Attributes.Remove(attribute);
        }

        public void RemoveFile(File file)
        {
            if (Context.Files.Contains(file))
                Context.Files.Remove(file);
        }

        public User AddNewUser()
        {
            var g = new User
            {
                PasswordQuestion = "PasswordQuestion",
                UserType = "U"
            };
            Context.Users.Add(g);
            return g;
        }

        public void RemoveUser(User user)
        {
            if (Context.Users.Contains(user))
                Context.Users.Remove(user);
        }

        public void SaveChangesAsync()
        {
            Context.SubmitChanges(s =>
            {
                if (SaveChangesComplete != null)
                {
                    try
                    {
                        Exception ex = null;
                        if (s.HasError)
                        {
                            ex = s.Error;
                            s.MarkErrorAsHandled();
                        }
                        SaveChangesComplete(this, new SubmitOperationEventArgs(s, ex));
                    }
                    catch (Exception ex)
                    {
                        SaveChangesComplete(this, new SubmitOperationEventArgs(ex));
                    }
                }
            }, null);
        }

        /// <summary>
        /// Reject any pending changes
        /// </summary>
        public void RejectChanges()
        {
            Context.RejectChanges();
        }

        /// <summary>
        /// True if _ctx.HasChanges is true; otherwise, false
        /// </summary>
        public Boolean HasChanges
        {
            get
            {
                return _hasChanges;
            }

            private set
            {
                if (_hasChanges != value)
                {
                    _hasChanges = value;
                    OnPropertyChanged("HasChanges");
                }
            }
        }
        private Boolean _hasChanges;

        /// <summary>
        /// True if either "IsLoading" or "IsSubmitting" is
        /// in progress; otherwise, false
        /// </summary>
        public Boolean IsBusy
        {
            get
            {
                return _isBusy;
            }

            private set
            {
                if (_isBusy != value)
                {
                    _isBusy = value;
                    OnPropertyChanged("IsBusy");
                }
            }
        }
        private Boolean _isBusy;

        public event EventHandler<SubmitOperationEventArgs> SaveChangesComplete;
        public event EventHandler<EntityResultsArgs<IssueType>> GetIssueTypesComplete;
        public event EventHandler<EntityResultsArgs<Platform>> GetPlatformsComplete;
        public event EventHandler<EntityResultsArgs<Resolution>> GetResolutionsComplete;
        public event EventHandler<EntityResultsArgs<Status>> GetStatusesComplete;
        public event EventHandler<EntityResultsArgs<SubStatus>> GetSubStatusesComplete;
        public event EventHandler<EntityResultsArgs<User>> GetUsersComplete;
        public event EventHandler<EntityResultsArgs<User>> GetCurrentUserComplete;
        public event EventHandler<EntityResultsArgs<SecurityQuestion>> GetSecurityQuestionsComplete;
        public event EventHandler<EntityResultsArgs<Issue>> GetMyIssuesComplete;
        public event EventHandler<EntityResultsArgs<Issue>> GetAllIssuesComplete;
        public event EventHandler<EntityResultsArgs<Issue>> GetAllUnresolvedIssuesComplete;
        public event EventHandler<InvokeOperationEventArgs> GetActiveBugCountByMonthComplete;
        public event EventHandler<InvokeOperationEventArgs> GetResolvedBugCountByMonthComplete;
        public event EventHandler<InvokeOperationEventArgs> GetActiveBugCountByPriorityComplete;

        #endregion "IIssueVisionModel Interface implementation"

        #region "INotifyPropertyChanged Interface implementation"
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion "INotifyPropertyChanged Interface implementation"

        #region "Private Methods"
        private void _ctx_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "HasChanges":
                    HasChanges = _ctx.HasChanges;
                    break;
                case "IsLoading":
                    IsBusy = _ctx.IsLoading;
                    break;
                case "IsSubmitting":
                    IsBusy = _ctx.IsSubmitting;
                    break;
            }
        }

        private void PerformQuery<T>(EntityQuery<T> qry, EventHandler<EntityResultsArgs<T>> evt) where T : Entity
        {
            Context.Load(qry, LoadBehavior.RefreshCurrent, r =>
            {
                if (evt != null)
                {
                    try
                    {
                        if (r.HasError)
                        {
                            evt(this, new EntityResultsArgs<T>(r.Error));
                            r.MarkErrorAsHandled();
                        }
                        else
                        {
                            evt(this, new EntityResultsArgs<T>(r.Entities));
                        }
                    }
                    catch (Exception ex)
                    {
                        evt(this, new EntityResultsArgs<T>(ex));
                    }
                }
            }, null);
        }
        #endregion "Private Methods"                                     
    }
}

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)

Share

About the Author

Weidong Shen
Software Developer (Senior)
United States United States
Weidong has been an information system professional since 1990. He has a Master's degree in Computer Science, and is currently a MCSD .NET

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 7 Jul 2011
Article Copyright 2010 by Weidong Shen
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid