示例#1
0
        private void EditCountry(string countryName)
        {
            CountryEditViewModel viewModel = new CountryEditViewModel(countryName);
            CountryEditControl   control   = new CountryEditControl(viewModel);
            Window window = WindowFactory.CreateByContentsSize(control);

            viewModel.CountryEdited += (s, e) =>
            {
                CountryEditModel countryEditModel = e.Country;
                CountryEditDTO   countryEditDTO   = Mapper.Map <CountryEditModel, CountryEditDTO>(countryEditModel);

                using (ICountryService service = factory.CreateCountryService())
                {
                    ServiceMessage serviceMessage = service.Update(countryEditDTO);
                    RaiseReceivedMessageEvent(serviceMessage.IsSuccessful, serviceMessage.Message);

                    if (serviceMessage.IsSuccessful)
                    {
                        window.Close();
                        Notify();
                    }
                }
            };

            window.Show();
        }
        public ServiceMessage Update(CountryEditDTO countryEditDTO)
        {
            string message;
            bool   success = true;

            try
            {
                CountryEntity countryEntity = unitOfWork
                                              .Countries
                                              .GetAll()
                                              .SingleOrDefault(country => country.Name == countryEditDTO.OldCountryName);
                if (countryEntity != null)
                {
                    countryEntity.Name = countryEditDTO.NewCountryName;
                    unitOfWork.Commit();

                    message = "Country was renamed";
                }
                else
                {
                    message = "Country with such name doesn't exist";
                    success = false;
                }
            }
            catch (Exception ex)
            {
                message = ExceptionMessageBuilder.BuildMessage(ex);
                success = false;
            }

            return(new ServiceMessage(message, success));
        }