Beispiel #1
0
        public async Task RemoveAsync(TIdentity identity, CancellationToken cancellationToken = default)
        {
            try
            {
                var item = await this.FindAsync(identity, cancellationToken).ConfigureAwait(false);

                if (item == null)
                {
                    return;
                }

                item = await this.OnBeforeRemoveAsync(item, cancellationToken).ConfigureAwait(false);

                await Task.Run(() => this.Context.Remove(item)).ConfigureAwait(false);

                await this.Context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);

                await this.OnAfterRemoveAsync(identity, cancellationToken).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                var ve = new EntityFrameworkRepositoryException("Failure to RemoveAsync", ex);
                this.Logger?.LogError("LogInstance {LogInstance}, Key {Key}, Exception {Exception}", ve.LogInstance, identity, ex);
                throw ve;
            }
        }
        public async Task <TModel> FindOrAddAsync(TModel item, CancellationToken cancellationToken)
        {
            try
            {
                // TODO: use linq expression instead of AsEnumerable() for filtering for perf
                var existingItems = (await this.OnAfterRetrieveAsync(
                                         this.Items.AsEnumerable().Where(i => i.NaturalKey.Equals(item.NaturalKey)).AsQueryable(), cancellationToken).ConfigureAwait(false));
                if (existingItems.Any())
                {
                    var existingItem = existingItems.FirstOrDefault();
                    if (this.PopulateChildModelsOnGet)
                    {
                        await this.Context.EnsureChildModelsArePopulated(existingItem, this.Cache, cancellationToken).ConfigureAwait(false);
                    }
                    return(existingItem);
                }

                return(await this.AddAsync(item, cancellationToken).ConfigureAwait(false));
            }
            catch (Exception ex)
            {
                var ve = new EntityFrameworkRepositoryException("Failure to FindOrAddAsync", ex);
                this.Logger?.LogError("LogInstance {LogInstance}, Item {Item}, Exception {Exception}", ve.LogInstance, item.ToJson(), ex);
                throw ve;
            }
        }
Beispiel #3
0
        public async Task <IQueryable <TModel> > GetAllAsync(CancellationToken cancellationToken = default)
        {
            try
            {
                var items = await this.OnAfterRetrieveAsync(this.Items, cancellationToken).ConfigureAwait(false);

                if (this.PopulateChildModelsOnGet)
                {
                    await this.Context.EnsureChildModelsArePopulated(items, this.Cache, cancellationToken).ConfigureAwait(false);
                }
                return(items);
            }
            catch (Exception ex)
            {
                var ve = new EntityFrameworkRepositoryException("Failure to GetAllAsync", ex);
                this.Logger?.LogError("LogInstance {LogInstance}, Exception {Exception}", ve.LogInstance, ex);
                throw ve;
            }
        }
Beispiel #4
0
        public async Task <TModel> UpdateAsync(TModel item, CancellationToken cancellationToken = default)
        {
            try
            {
                this.Context.TrackUpdateWithEntityDetection(item, this.Cache, cancellationToken);

                item = await this.OnBeforeUpdateAsync(item, cancellationToken).ConfigureAwait(false);

                await this.Context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);

                return(await this.OnAfterUpdateAsync(await this.FindAsync(item.Identity, cancellationToken).ConfigureAwait(false), cancellationToken).ConfigureAwait(false));
            }
            catch (Exception ex)
            {
                var ve = new EntityFrameworkRepositoryException("Failure to UpdateAsync", ex);
                this.Logger?.LogError("LogInstance {LogInstance}, Item {Item}, Exception {Exception}", ve.LogInstance, item.ToJson(), ex);
                throw ve;
            }
        }
Beispiel #5
0
 public async Task <TModel> FindAsync(TIdentity identity, CancellationToken cancellationToken = default)
 {
     try
     {
         // TODO: use linq expression instead of AsEnumerable() for filtering for perf
         var foundItem = (await this.OnAfterRetrieveAsync(
                              this.Items.AsEnumerable().Where(i => identity.Equals(i.Identity)).AsQueryable(), cancellationToken).ConfigureAwait(false))
                         .First();
         if (this.PopulateChildModelsOnGet)
         {
             await this.Context.EnsureChildModelsArePopulated(foundItem, this.Cache, cancellationToken).ConfigureAwait(false);
         }
         return(foundItem);
     }
     catch (Exception ex)
     {
         var ve = new EntityFrameworkRepositoryException("Failure to FindAsync", ex);
         this.Logger?.LogError("LogInstance {LogInstance}, Key {Key}, Exception {Exception}", ve.LogInstance, identity, ex);
         throw ve;
     }
 }