Click here to Skip to main content
15,886,258 members
Articles / DevOps / Testing

Composite Application Reloaded

Rate me:
Please Sign up or sign in to vote.
4.88/5 (38 votes)
11 May 2011CPOL12 min read 117.8K   1.5K   95  
A much simpler composite application library.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using DemoApp.DataAccess;
using DemoApp.Properties;
using System.ComponentModel.Composition;
using DemoApp.Model;
using System.Windows.Input;
using Galador.Applications;

namespace DemoApp.ViewModel
{
	/// <summary>
	/// Represents a container of CustomerViewModel objects
	/// that has support for staying synchronized with the
	/// CustomerRepository.  This class also provides information
	/// related to multiple selected customers.
	/// </summary>
	public class AllCustomersViewModel : WorkspaceViewModel
	{
		readonly ICustomerRepository customerRepository;

		public AllCustomersViewModel(ICustomerRepository customerRepository)
		{
			if (customerRepository == null)
				throw new ArgumentNullException("customerRepository");
			this.customerRepository = customerRepository;
			base.DisplayName = Strings.AllCustomersViewModel_DisplayName;

			AllCustomers = new ObservableCollection<CustomerViewModel>();
			customerRepository.CustomerAdded += (o, args) => AddCustomer(args.NewCustomer);
			foreach (var c in customerRepository.GetCustomers())
				AddCustomer(c);

			Composition.Compose(this);
		}

		[Import("EditCustomerCommand", typeof(ICommand))]
		public ICommand EditCommand { get; set; }
		public ObservableCollection<CustomerViewModel> AllCustomers { get; private set; }
		public decimal TotalSelectedSales { get { return this.AllCustomers.Sum(custVM => custVM.IsSelected ? custVM.Customer.TotalSales : 0.0M); } }

		void AddCustomer(Customer c)
		{
			var cvm = new CustomerViewModel(c, customerRepository);
			cvm.PropertyChanged += OnCustomerPropertyChanged;
			AllCustomers.Add(cvm);
		}
		void OnCustomerPropertyChanged(object sender, PropertyChangedEventArgs args)
		{
			// When a customer is selected or unselected, we must let the
			// world know that the TotalSelectedSales property has changed,
			// so that it will be queried again for a new value.
			if (args.PropertyName == "IsSelected")
				this.OnPropertyChanged(() => TotalSelectedSales);
		}

		protected override void OnDispose(bool disposing)
		{
			if (!disposing)
				return;
			foreach (CustomerViewModel custVM in this.AllCustomers)
			{
				custVM.Dispose();
				custVM.PropertyChanged -= OnCustomerPropertyChanged;
			}
			this.AllCustomers.Clear();
		}
	}
}

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) http://www.ansibleww.com.au
Australia Australia
The Australia born French man who went back to Australia later in life...
Finally got over life long (and mostly hopeless usually, yay!) chronic sicknesses.
Worked in Sydney, Brisbane, Darwin, Billinudgel, Darwin and Melbourne.

Comments and Discussions