Beispiel #1
0
        public async Task <SaveResponse <IClient> > SaveAsync(IClient client)
        {
            var response = new SaveResponse <IClient>();

            try
            {
                // Validate Information before saving
                response.FromValidationResult(await ValidateAsync(client));

                // 4. Begin transaction
                using (var scope = new TransactionScope(TransactionScopeOption.Required))
                {
                    // 5. Save Client
                    response = _clientRepository.SaveAsync(client).Result;

                    if (response.IsSuccessful)
                    {
                        // 6. Save Addresses
                        var addressResponse = _addressApplicationService.SaveAsync(client.Address).Result;

                        // 7. Save Contacts
                        var contactResponse = _contactApplicationService
                                              .SaveAllAsync(client.Contacts as IReadOnlyList <IContact>).Result;

                        response.Join <SaveResponse>(addressResponse)
                        .Join <SaveResponse>(contactResponse);
                    }

                    if (response.IsSuccessful)
                    {
                        scope.Complete();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                response.AddError(ex);
                _logManager.LogError(ex, "Error saving client");
            }

            return(response);
        }