private ReservationViewModel ConvertToViewModel(ReservationInfo reservation) { var reservationViewModel = new ReservationViewModel { Start = reservation.Start, End = reservation.End, CustomerName = string.Empty, }; if (reservation.CustomerId != null) { IEnumerable<CustomerInfo> customers = this.customerService.GetCustomers(); CustomerInfo customer = customers.FirstOrDefault(c => c.Id == reservation.CustomerId); if (customer == null) { throw new ArgumentException("Unknown customer Id"); } reservationViewModel.CustomerName = customer.Name; } return reservationViewModel; }
private ReservationInfo ConvertToDataObject(ReservationViewModel reservationViewModel) { var reservation = new ReservationInfo { Start = reservationViewModel.Start, End = reservationViewModel.End }; if (!String.IsNullOrEmpty(reservationViewModel.CustomerName)) { IEnumerable<CustomerInfo> customers = this.customerService.GetCustomers(); CustomerInfo customer = customers.FirstOrDefault( c => String.Compare(c.Name, reservationViewModel.CustomerName, StringComparison.InvariantCultureIgnoreCase) == 0); if (customer == null) { throw new ArgumentException("Invalid customer name" + reservationViewModel.CustomerName); } reservation.CustomerId = customer.Id; } return reservation; }