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

RiaTasks: Central Silverlight Business Rules Validation

, 13 Jul 2010 Ms-PL
Using Fluent Validation on the website to validate business rules in a Silverlight application
RIATasks2BusinessValidation.zip
RIATasks
RIATasks.Web
bin
de
FluentValidation.resources.dll
es
FluentValidation.resources.dll
FluentValidation.dll
FluentValidation.pdb
fr
FluentValidation.resources.dll
nl
FluentValidation.resources.dll
pt
FluentValidation.resources.dll
RIATasks.Web.dll
RIATasks.Web.pdb
sv
FluentValidation.resources.dll
BusinessValidation
ClientBin
RIATasks.xap
Properties
RIATasks.Web.csproj.user
RIATasksDB.dbml
RIATasksDB.dbml.layout
Assets
Bin
Debug
ar
System.Windows.Controls.resources.dll
bg
System.Windows.Controls.resources.dll
ca
System.Windows.Controls.resources.dll
cs
System.Windows.Controls.resources.dll
da
System.Windows.Controls.resources.dll
de
System.Windows.Controls.resources.dll
el
System.Windows.Controls.resources.dll
es
System.Windows.Controls.resources.dll
et
System.Windows.Controls.resources.dll
eu
System.Windows.Controls.resources.dll
fi
System.Windows.Controls.resources.dll
fr
System.Windows.Controls.resources.dll
he
System.Windows.Controls.resources.dll
hr
System.Windows.Controls.resources.dll
hu
System.Windows.Controls.resources.dll
id
System.Windows.Controls.resources.dll
it
System.Windows.Controls.resources.dll
ja
System.Windows.Controls.resources.dll
ko
System.Windows.Controls.resources.dll
lt
System.Windows.Controls.resources.dll
lv
System.Windows.Controls.resources.dll
ms
System.Windows.Controls.resources.dll
nl
System.Windows.Controls.resources.dll
no
System.Windows.Controls.resources.dll
pl
System.Windows.Controls.resources.dll
pt
pt-BR
System.Windows.Controls.resources.dll
System.Windows.Controls.resources.dll
RIATasks.dll
RIATasks.xap
ro
System.Windows.Controls.resources.dll
ru
System.Windows.Controls.resources.dll
sk
System.Windows.Controls.resources.dll
sl
System.Windows.Controls.resources.dll
sr-Cyrl-CS
System.Windows.Controls.resources.dll
sr-Latn-CS
System.Windows.Controls.resources.dll
sv
System.Windows.Controls.resources.dll
System.CoreEx.dll
System.Observable.dll
System.Reactive.dll
System.Windows.Controls.dll
System.Windows.Interactivity.dll
th
System.Windows.Controls.resources.dll
tr
System.Windows.Controls.resources.dll
uk
System.Windows.Controls.resources.dll
vi
System.Windows.Controls.resources.dll
zh-Hans
System.Windows.Controls.resources.dll
zh-Hant
System.Windows.Controls.resources.dll
Classes
Controls
ViewModels
Views
Models
Properties
Resources
RIATasks.csproj.user
SampleData
Service References
wsRIATasks
configuration.svcinfo
configuration91.svcinfo
Reference.svcmap
RIATasks.wsRIATasks.DeleteTaskResponse.datasource
RIATasks.wsRIATasks.GetTasksResponse.datasource
RIATasks.wsRIATasks.InsertTaskResponse.datasource
RIATasks.wsRIATasks.UpdateTaskResponse.datasource
WebService.disco
WebService.wsdl
ServiceReferences.ClientConfig
ViewModels
using System;
using System.Linq;
using System.ComponentModel;
using RIATasks.wsRIATasks;
using System.Collections.ObjectModel;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows;
using System.Collections.Generic;

namespace RIATasks
{
    public class TabControlModel : INotifyPropertyChanged
    {
        public TabControlModel()
        {
            AddNewTaskCommand = new DelegateCommand(AddNewTask, CanAddNewTask);
            DeleteTaskCommand = new DelegateCommand(DeleteTask, CanDeleteTask);
            UpdateTaskCommand = new DelegateCommand(UpdateTask, CanUpdateTask);

            // The following line prevents Expression Blend
            // from showing an error when in design mode
            if (!DesignerProperties.IsInDesignTool)
            {
                // Get the Tasks for the current user
                GetTasks();
            }
        }

        #region AddNewTaskCommand
        public ICommand AddNewTaskCommand { get; set; }
        public void AddNewTask(object param)
        {
            SetToNewTask();
        }

        private bool CanAddNewTask(object param)
        {
            // Only allow a New Task to be created
            // If there are no other [New] Tasks

            var colNewTasks = from Tasks in colTabItems
                              where (Tasks.Header as string).Contains("[New]")
                              select Tasks;

            return (colNewTasks.Count() == 0);
        }
        #endregion

        #region DeleteTaskCommand
        public ICommand DeleteTaskCommand { get; set; }
        public void DeleteTask(object param)
        {
            // Get The Task
            Task objTask = GetTaskFromTaskDetails((param as TaskDetails));

            if (objTask.TaskID != -1)
            {
                // Delete Task
                DeleteTask(objTask);
            }
            else
            {
                RemoveTask(objTask.TaskID);
            }
        }

        private bool CanDeleteTask(object param)
        {
            // Only allow this ICommand to fire 
            // if a TaskDetails was passed as a parameter
            return ((param as TaskDetails) != null);
        }
        #endregion

        #region UpdateTaskCommand
        public ICommand UpdateTaskCommand { get; set; }
        public void UpdateTask(object param)
        {
            // Get The Task
            Task objTask = GetTaskFromTaskDetails((param as TaskDetails));

            if (objTask.TaskID == -1)
            {
                // This is a new Task
                InsertTask(objTask);
            }
            else
            {
                // This is an Update
                UpdateTask(objTask);
            }
        }

        private bool CanUpdateTask(object param)
        {
            // Only allow this ICommand to fire 
            // if a TaskDetails was passed as a parameter
            return ((param as TaskDetails) != null);
        }
        #endregion

        // Operations

        #region DeleteTask
        private void DeleteTask(Task objTask)
        {
            // Call the Model to delete the Task
            TasksModel.DeleteTask(objTask.TaskID, (Param, EventArgs) =>
            {
                if (EventArgs.Error == null)
                {
                    // Show any errors
                    Message = EventArgs.Result.Errors.ToList();
                    // Set the visibility of the Message ListBox
                    MessageVisibility = (Message.Count > 0) ? Visibility.Visible : Visibility.Collapsed;

                    RemoveTask(objTask.TaskID);
                }
            });
        }
        #endregion

        #region UpdateTask
        private void UpdateTask(Task objTask)
        {
            // Call the Model to UpdateTask the Task
            TasksModel.UpdateTask(objTask, (Param, EventArgs) =>
            {
                if (EventArgs.Error == null)
                {
                    // Show any errors
                    Task objResultTask = EventArgs.Result;

                    Message = objResultTask.Errors.ToList();
                    // Set the visibility of the Message ListBox
                    MessageVisibility = (Message.Count > 0) ? Visibility.Visible : Visibility.Collapsed;
                }
            });
        }
        #endregion

        #region InsertTask
        private void InsertTask(Task objTask)
        {
            // Call the Model to Insert the Task
            TasksModel.InsertTask(objTask, (Param, EventArgs) =>
            {
                if (EventArgs.Error == null)
                {
                    // Set the CurrentTaskID Property
                    // So it can be selected when Tasks re-load
                    CurrentTaskID = EventArgs.Result.TaskID;

                    // Show any errors
                    Message = EventArgs.Result.Errors.ToList();
                    // Set the visibility of the Message ListBox
                    MessageVisibility = (Message.Count > 0) ? Visibility.Visible : Visibility.Collapsed;

                    // If there are no errors - refresh Task List
                    if (Message.Count == 0)
                    {
                        GetTasks();
                    }
                }
            });
        }
        #endregion

        #region GetTasks
        private void GetTasks()
        {
            // Clear the current Tasks
            colTabItems.Clear();

            // Call the Model to get the collection of Tasks
            TasksModel.GetTasks((Param, EventArgs) =>
            {
                if (EventArgs.Error == null)
                {
                    // loop thru each item
                    foreach (var objTask in EventArgs.Result)
                    {
                        // Create a TaskItem from the Task
                        TabItem objTabItem = CreateTaskItem(objTask);

                        // Add it to the Collection of TabItems
                        colTabItems.Add(objTabItem);
                    }

                    // Count the records returned
                    if (colTabItems.Count == 0)
                    {
                        // If there are no records, indicate that
                        Message.Clear();
                        Message.Add("No Records Found");
                        MessageVisibility = Visibility.Visible;
                    }
                    else
                    {
                        Message.Clear();
                        MessageVisibility = Visibility.Collapsed;
                    }

                    // If there is a CurrentTaskID set that 
                    // as the Current task
                    if (CurrentTaskID != -1)
                    {
                        // Locate the Task
                        var objTabItem = (from TaskItem in colTabItems
                                          let VM = (TaskItem.Content as TaskDetails).DataContext
                                          where (VM as TaskDetailsModel).CurrentTask.TaskID == CurrentTaskID
                                          select TaskItem).FirstOrDefault();

                        if (objTabItem != null)
                        {
                            // Set the CurrentTask as selected
                            objTabItem.IsSelected = true;
                        }
                    }
                }
            });
        }
        #endregion

        #region SetToNewTask
        private void SetToNewTask()
        {
            // Unset selected for all Items
            foreach (var item in colTabItems)
            {
                item.IsSelected = false;
            }

            // Create a empty Task
            // so form will be blank
            Task objTask = new Task();

            // Set TaskID = -1 so we know it's a new Task
            objTask.TaskID = -1;

            // Create a TaskItem from the Task
            TabItem objNewTabItem = CreateTaskItem(objTask);

            // Set it as selected
            objNewTabItem.IsSelected = true;

            // Add it to the Collection of TabItems
            this.colTabItems.Add(objNewTabItem);
        }
        #endregion

        #region RemoveTask
        private void RemoveTask(int TaskID)
        {
            // Locate the Task
            var objTabItem = (from TaskItem in colTabItems
                              let VM = (TaskItem.Content as TaskDetails).DataContext
                              where (VM as TaskDetailsModel).CurrentTask.TaskID == TaskID
                              select TaskItem).FirstOrDefault();

            if (objTabItem != null)
            {
                // Remove the Task
                colTabItems.Remove(objTabItem);
            }
        }
        #endregion

        #region CreateTaskItem
        private TabItem CreateTaskItem(Task Task)
        {
            // Create a Tasks Details
            TaskDetails objTaskDetails = new TaskDetails();
            // Get it's DataContext
            TaskDetailsModel objTaskDetailsModel = (TaskDetailsModel)objTaskDetails.DataContext;
            // Call a method to set the Current Task at it's DataContext
            objTaskDetailsModel.SetCurrentTask(Task);

            // Create a TabItem 
            TabItem objTabItem = new TabItem();
            // Give it a name so it can be programatically manipulated
            objTabItem.Name = string.Format("DynamicTab_{0}", Task.TaskID.ToString());
            // Set the Style to point to a Resource that te Designer
            // can later change
            objTabItem.Style = (Style)App.Current.Resources["TabItemStyle1"];
            // Set it's Header
            string strTaskID = (Task.TaskID == -1) ? "[New]" : Task.TaskID.ToString();
            objTabItem.Header = String.Format("Task {0}", strTaskID);
            // Set it's Content to the Tasks Details control
            objTabItem.Content = objTaskDetails;
            return objTabItem;
        }
        #endregion

        #region GetTaskFromTaskDetails
        private Task GetTaskFromTaskDetails(TaskDetails objTaskDetails)
        {
            // Get TaskDetailsModel from 
            TaskDetailsModel objTaskDetailsModel = (TaskDetailsModel)objTaskDetails.DataContext;

            // Get the Task from TaskDetails
            Task objTask = (Task)objTaskDetailsModel.CurrentTask;

            return objTask;
        }
        #endregion

        // Properties

        #region CurrentTaskID
        private int _CurrentTaskID = -1;
        public int CurrentTaskID
        {
            get { return _CurrentTaskID; }
            private set
            {
                if (CurrentTaskID == value)
                {
                    return;
                }
                _CurrentTaskID = value;
                this.NotifyPropertyChanged("CurrentTaskID");
            }
        }
        #endregion

        #region Message
        private List<string> _Message = new List<string>();
        public List<string> Message
        {
            get { return _Message; }
            private set
            {
                if (Message == value)
                {
                    return;
                }
                _Message = value;
                this.NotifyPropertyChanged("Message");
            }
        }
        #endregion

        #region MessageVisibility
        private Visibility _MessageVisibility = Visibility.Collapsed;
        public Visibility MessageVisibility
        {
            get { return _MessageVisibility; }
            private set
            {
                if (MessageVisibility == value)
                {
                    return;
                }
                _MessageVisibility = value;
                this.NotifyPropertyChanged("MessageVisibility");
            }
        }
        #endregion

        // Collections

        #region colTabItems
        private ObservableCollection<TabItem> _colTabItems
            = new ObservableCollection<TabItem>();
        public ObservableCollection<TabItem> colTabItems
        {
            get { return _colTabItems; }
            private set
            {
                if (colTabItems == value)
                {
                    return;
                }
                _colTabItems = value;
                this.NotifyPropertyChanged("colTabItems");
            }
        }
        #endregion

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion
    }
}

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 Microsoft Public License (Ms-PL)

Share

About the Author

defwebserver
Software Developer (Senior) http://ADefWebserver.com
United States United States
Michael Washington is a Microsoft MVP. He is a ASP.NET and
C# programmer.
He is the founder of
LightSwitchHelpWebsite.com

He has a son, Zachary and resides in Los Angeles with his wife Valerie.

He is the Author of:
Follow on   Twitter

| Advertise | Privacy | Mobile
Web01 | 2.8.141022.2 | Last Updated 13 Jul 2010
Article Copyright 2010 by defwebserver
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid