internal static Option <bool> DeleteEntity <T, TDto, TDeleteInterceptor>(
            this Option <ServiceMapping <T, TDeleteInterceptor> > serviceMapping,
            Option <TDto> criteria)
            where T : class, IEntity
            where TDto : class, IDto
            where TDeleteInterceptor : IDeleteInterceptor
        {
            try
            {
                return(serviceMapping.GetDeleteParametersMapping(criteria)
                       .MapFlatten(
                           x =>
                {
                    var entityCriteria = x.Criteria.ToEntity <T>();
                    AsyncHelper.RunSync(() => x.Args.PreDelete?.Invoke(entityCriteria, x.Repository));
                    var result = x.Repository.Delete(entityCriteria);
                    AsyncHelper.RunSync(() => x.Args.PreSave?.Invoke(entityCriteria, x.Repository));
                    if (x.Args.PendingSave)
                    {
                        return result;
                    }
                    x.Uow.SaveChanges();
                    return result;
                }));
            }
            catch (ArgumentNullException ex)
            {
                return(Fail <bool> .Throw(ex));
            }
            catch (KeyNotFoundException ex)
            {
                return(Fail <bool> .Throw(ex));
            }
            catch (PreDeleteException ex)
            {
                return(Fail <bool> .Throw(ex));
            }
            catch (PreSaveException ex)
            {
                return(Fail <bool> .Throw(ex));
            }
#if NETSTANDARD2_0
            catch (EfCoreDbUpdateException ex)
            {
                return(Fail <bool> .Throw(ex));
            }
#endif
#if NET471
            catch (EfDbUpdateException ex)
            {
                return(Fail <bool> .Throw(ex));
            }
#endif
            catch (Exception ex)
            {
                return(Fail <bool> .Throw(ex));
            }
        }
 public Option <T> ToEntity <TDto, T>(Option <TDto> dto)
     where TDto : class, IDto where T : class, IEntity
 {
     try
     {
         var result = Mapper.Map <TDto, T>(dto.ReduceOrDefault());
         return(result);
     }
     catch (Exception ex)
     {
         return(Fail <T> .Throw(ex));
     }
 }
 internal static Option <ServiceMapping <T, TInterceptor2> > ChangeInterceptor <
     T, TInterceptor1, TInterceptor2>(
     this Option <ServiceMapping <T, TInterceptor1> > serviceMapping)
     where T : class, IEntity
     where TInterceptor1 : IInterceptor
     where TInterceptor2 : IInterceptor
 {
     try
     {
         return(serviceMapping.ReduceOrDefault().ChangeInterceptor <TInterceptor2>());
     }
     catch (Exception ex)
     {
         return(Fail <ServiceMapping <T, TInterceptor2> > .Throw(ex));
     }
 }
Beispiel #4
0
 public override Option <int> SaveChanges()
 {
     try
     {
         return(GetDbContext <EfDbContext>().Map(x => x.SaveChanges()));
     }
     catch (DbUpdateConcurrencyException ex)
     {
         return(Fail <int> .Throw(new EfDbUpdateConcurrencyException(ex.Message, ex)));
     }
     catch (DbUpdateException ex)
     {
         return(Fail <int> .Throw(new EfDbUpdateException(ex.Message, ex)));
     }
     catch (Exception ex)
     {
         return(Fail <int> .Throw(ex));
     }
 }
Beispiel #5
0
 public override Option <int> SaveChanges()
 {
     try
     {
         return(GetDbContext <EfCoreDbContext>().Map(x => x.SaveChanges()));
     }
     catch (DbUpdateConcurrencyException ex)
     {
         return(Fail <int> .Throw(
                    new EfCoreDbUpdateConcurrencyException(ex.Message, ex.Entries.Select(x => (IUpdateEntry)x).ToList())));
     }
     catch (DbUpdateException ex)
     {
         return(Fail <int> .Throw(
                    new EfCoreDbUpdateException(ex.Message, ex, ex.Entries.Select(x => (IUpdateEntry)x).ToList())));
     }
     catch (Exception ex)
     {
         return(Fail <int> .Throw(ex));
     }
 }
Beispiel #6
0
 public override async Task <Option <int> > SaveChangesAsync(Option <CancellationToken> ctok)
 {
     try
     {
         return(await GetDbContext <EfDbContext>()
                .Combine(ctok)
                .MapAsync(x => x.Item1.SaveChangesAsync(x.Item2)));
     }
     catch (DbUpdateConcurrencyException ex)
     {
         return(Fail <int> .Throw(new EfDbUpdateConcurrencyException(ex.Message, ex)));
     }
     catch (DbUpdateException ex)
     {
         return(Fail <int> .Throw(new EfDbUpdateException(ex.Message, ex)));
     }
     catch (Exception ex)
     {
         return(Fail <int> .Throw(ex));
     }
 }
 public Fail <T> ToFail <T>()
 {
     return(Fail <T> .Throw(new ValidationException(ErrorMessages)));
 }
        internal static Option <Dictionary <string, object> > CreateEntity <T, TDto, TCreateInterceptor>(
            this Option <ServiceMapping <T, TCreateInterceptor> > serviceMapping,
            Option <TDto> dto)
            where T : class, IEntity
            where TDto : class, IDto
            where TCreateInterceptor : ICreateInterceptor
        {
            try
            {
                return(serviceMapping.GetCreateParametersMapping(dto)
                       .MapFlatten(
                           x =>
                {
                    x.Args.PreValidation?.Invoke(x.Dto);
                    x.Dto.Validate();
                    x.Args.PostValidation?.Invoke(x.Dto);
                    var entity = x.Dto.ToEntity <T>();
                    AsyncHelper.RunSync(() => x.Args.PostMap?.Invoke(entity, x.Repository));
                    var result = x.Repository.Create(entity, x.Args.MessageIfExistFunc);
                    AsyncHelper.RunSync(() => x.Args.PreSave?.Invoke(entity, x.Repository));
                    if (x.Args.PendingSave)
                    {
                        return result;
                    }
                    x.Uow.SaveChanges();
                    return result;
                }));
            }
            catch (ArgumentNullException ex)
            {
                return(Fail <Dictionary <string, object> > .Throw(ex));
            }
            catch (ValidationException ex)
            {
                return(Fail <Dictionary <string, object> > .Throw(ex));
            }
            catch (PreValidationException ex)
            {
                return(Fail <Dictionary <string, object> > .Throw(ex));
            }
            catch (PostValidationException ex)
            {
                return(Fail <Dictionary <string, object> > .Throw(ex));
            }
            catch (PostMapException ex)
            {
                return(Fail <Dictionary <string, object> > .Throw(ex));
            }
            catch (PreSaveException ex)
            {
                return(Fail <Dictionary <string, object> > .Throw(ex));
            }
            catch (ExistingEntityException <T> ex)
            {
                return(Fail <Dictionary <string, object> > .Throw(ex));
            }
#if NETSTANDARD2_0
            catch (EfCoreDbUpdateException ex)
            {
                var message = ex.HumanizeKeyError();
                return(Fail <Dictionary <string, object> > .Throw(new EfCoreDbUpdateException(message, ex)));
            }
#endif
#if NET471
            catch (EfDbUpdateException ex)
            {
                var message = ex.HumanizeKeyError();
                return(Fail <Dictionary <string, object> > .Throw(new EfDbUpdateException(message, ex)));
            }
#endif
            catch (Exception ex)
            {
                return(Fail <Dictionary <string, object> > .Throw(ex));
            }
        }
        internal static async Task <Option <bool> > DeleteEntityAsync <T, TDto, TDeleteInterceptor>(
            this Option <ServiceMapping <T, TDeleteInterceptor> > serviceMapping,
            Option <TDto> criteria,
            Option <CancellationToken> ctok)
            where T : class, IEntity
            where TDto : class, IDto
            where TDeleteInterceptor : IDeleteInterceptor
        {
            try
            {
                return(await serviceMapping.GetAsyncDeleteParametersMapping(criteria, ctok)
                       .MapFlattenAsync(
                           async x =>
                {
                    var entityCriteria = x.Criteria.ToEntity <T>();
                    if (x.Args.PreDelete != null)
                    {
                        await x.Args.PreDelete.Invoke(entityCriteria, x.Repository);
                    }
                    var result = await x.Repository.DeleteAsync(entityCriteria, x.Ctok);
                    if (x.Args.PreSave != null)
                    {
                        await x.Args.PreSave.Invoke(entityCriteria, x.Repository);
                    }
                    if (x.Args.PendingSave)
                    {
                        return result;
                    }
                    await x.Uow.SaveChangesAsync(x.Ctok);
                    return result;
                }));
            }
            catch (ArgumentNullException ex)
            {
                return(Fail <bool> .Throw(ex));
            }
            catch (KeyNotFoundException ex)
            {
                return(Fail <bool> .Throw(ex));
            }
            catch (PreDeleteException ex)
            {
                return(Fail <bool> .Throw(ex));
            }
            catch (PreSaveException ex)
            {
                return(Fail <bool> .Throw(ex));
            }
#if NETSTANDARD2_0
            catch (EfCoreDbUpdateException ex)
            {
                return(Fail <bool> .Throw(ex));
            }
#endif
#if NET471
            catch (EfDbUpdateException ex)
            {
                return(Fail <bool> .Throw(ex));
            }
#endif
            catch (Exception ex)
            {
                return(Fail <bool> .Throw(ex));
            }
        }
        internal static async Task <Option <Dictionary <string, object> > > CreateEntityAsync <T, TDto, TCreateInterceptor>(
            this Option <ServiceMapping <T, TCreateInterceptor> > serviceMapping,
            Option <TDto> dto,
            Option <CancellationToken> ctok)
            where T : class, IEntity
            where TDto : class, IDto
            where TCreateInterceptor : ICreateInterceptor
        {
            try
            {
                return(await serviceMapping.GetAsyncCreateParametersMapping(dto, ctok)
                       .MapFlattenAsync(
                           async x =>
                {
                    x.Args.PreValidation?.Invoke(x.Dto);
                    await x.Dto.ValidateAsync();
                    x.Args.PostValidation?.Invoke(x.Dto);
                    var entity = x.Dto.ToEntity <T>();
                    if (x.Args.PostMap != null)
                    {
                        await x.Args.PostMap.Invoke(entity, x.Repository);
                    }
                    var result = await x.Repository.CreateAsync(entity, x.Args.MessageIfExistFunc, x.Ctok);
                    if (x.Args.PreSave != null)
                    {
                        await x.Args.PreSave.Invoke(entity, x.Repository);
                    }
                    if (x.Args.PendingSave)
                    {
                        return result;
                    }
                    await x.Uow.SaveChangesAsync(ctok);
                    return result;
                }));
            }
            catch (ArgumentNullException ex)
            {
                return(Fail <Dictionary <string, object> > .Throw(ex));
            }
            catch (ValidationException ex)
            {
                return(Fail <Dictionary <string, object> > .Throw(ex));
            }
            catch (PreValidationException ex)
            {
                return(Fail <Dictionary <string, object> > .Throw(ex));
            }
            catch (PostValidationException ex)
            {
                return(Fail <Dictionary <string, object> > .Throw(ex));
            }
            catch (PostMapException ex)
            {
                return(Fail <Dictionary <string, object> > .Throw(ex));
            }
            catch (PreSaveException ex)
            {
                return(Fail <Dictionary <string, object> > .Throw(ex));
            }
            catch (ExistingEntityException <T> ex)
            {
                return(Fail <Dictionary <string, object> > .Throw(ex));
            }
#if NETSTANDARD2_0
            catch (EfCoreDbUpdateException ex)
            {
                var message = ex.HumanizeKeyError();
                return(Fail <Dictionary <string, object> > .Throw(new EfCoreDbUpdateException(message, ex)));
            }
#endif
#if NET471
            catch (EfDbUpdateException ex)
            {
                var message = ex.HumanizeKeyError();
                return(Fail <Dictionary <string, object> > .Throw(new EfDbUpdateException(message, ex)));
            }
#endif
            catch (Exception ex)
            {
                return(Fail <Dictionary <string, object> > .Throw(ex));
            }
        }