public void SaveCommandDoesNotExecuteIfCustomerIsInvalid()
        {
            Customer customer = TestHelpers.GetSampleCustomer();

            AddCustomerViewModel viewModel = new AddCustomerViewModel(customer);
            Assert.IsTrue(customer.IsValid);

            // Invalid address should disable save command.
            Assert.IsTrue(viewModel.SaveCommand.CanExecute(null));
            customer.AddressLineOne = TestHelpers.STRING_LONGER_THAN_255_CHARS;
            Assert.IsFalse(customer.IsValid);
            Assert.IsFalse(viewModel.SaveCommand.CanExecute(null));
            customer.AddressLineOne = "Sample Address";
            Assert.IsTrue(customer.IsValid);

            // Invalid date should disable save command.
            Assert.IsTrue(viewModel.SaveCommand.CanExecute(null));
            customer.DateOfBirth = new DateTime(1410, 07, 21);
            Assert.IsFalse(customer.IsValid);
            Assert.IsFalse(viewModel.SaveCommand.CanExecute(null));
            customer.DateOfBirth = new DateTime(1985, 7, 8);
            Assert.IsTrue(customer.IsValid);

            // Invalid name should disable save command.
            Assert.IsTrue(viewModel.SaveCommand.CanExecute(null));
            customer.Name = TestHelpers.STRING_LONGER_THAN_50_CHARS;
            Assert.IsFalse(customer.IsValid);
            Assert.IsFalse(viewModel.SaveCommand.CanExecute(null));
            customer.Name = "Test Name";
            Assert.IsTrue(customer.IsValid);
        }
 /// <summary>
 /// Creates an instance of the SaveCommand.
 /// </summary>
 /// <param name="addCustomerViewModel">A view model on which command's method will be invoked.</param>
 public SaveCommand(AddCustomerViewModel addCustomerViewModel)
 {
     this.addCustomerViewModel = addCustomerViewModel;
 }
        /// <summary>
        /// Invokes the update customer window.
        /// </summary>
        internal void UpdateCustomer()
        {
            try
            {
                // Make a copy of the current customer to avoid directly editing the customer in the data grid.
                // The customer will be modified in the cache and in the database
                // and the grid view will be updated from the cache or the database.
                Customer c = new Customer();
                c.Copy(this.SelectedCustomer);

                AddCustomerView addCustomerView = new AddCustomerView();
                AddCustomerViewModel addCustomerViewModel = new AddCustomerViewModel(c);
                addCustomerViewModel.SaveCompleted += (s, e) =>
                {
                    // Get latest data after save.
                    this.SelectedCustomer = null;
                    addCustomerView.Close();
                    this.GetData();
                };
                addCustomerViewModel.Cancelled += (s, e) =>
                {
                    this.SelectedCustomer = null;
                    addCustomerView.Close();
                };
                addCustomerView.DataContext = addCustomerViewModel;
                addCustomerView.ShowDialog();
                this.SelectedCustomer = null;
            }
            catch (Exception ex)
            {
                InfoDialogViewModel.ShowDialog(ex.Message, "Unhandled Exception");
                Log.LogException(ex, "MainViewModel.cs");
            }
        }
 /// <summary>
 /// Invokes the add new customer window .
 /// </summary>
 internal void AddCustomer()
 {
     try
     {
         AddCustomerView addCustomerView = new AddCustomerView();
         // Set the date of birth to today, so it's not set to default 01/01/0001
         Customer customer = new Customer() { DateOfBirth = DateTime.Now };
         AddCustomerViewModel addCustomerViewModel = new AddCustomerViewModel(customer);
         addCustomerViewModel.SaveCompleted += (s, e) =>
             {
                 // Get latest data after save.
                 this.SelectedCustomer = null;
                 addCustomerView.Close();
                 this.GetData();
             };
         addCustomerViewModel.Cancelled += (s, e) =>
             {
                 this.SelectedCustomer = null;
                 addCustomerView.Close();
             };
         addCustomerView.DataContext = addCustomerViewModel;
         addCustomerView.ShowDialog();
     }
     catch (Exception ex)
     {
         InfoDialogViewModel.ShowDialog(ex.Message, "Unhandled Exception");
         Log.LogException(ex, "MainViewModel.cs");
     }
 }
 public void SettingCustomerToAddCustomerViewModelTriggersPropertyChanged()
 {
     Customer customer = new Customer();
     AddCustomerViewModel viewModel = new AddCustomerViewModel(customer);
     viewModel.PropertyChanged += (s, e) =>
     {
         Assert.AreEqual("SelectedCustomer", e.PropertyName);
         Assert.AreEqual(customer, viewModel.SelectedCustomer);
     };
     viewModel.SelectedCustomer = customer;
 }