/// <summary> /// Manages the DbContext and underlying query construction and lifetime. /// </summary> private TResult ExecuteQuery <TResult>(Func <IQueryable <TModel>, TResult> execute) { return(EfDbInvoker <TDbContext> .Default.Invoke(this, () => { using var db = new EfDbBase <TDbContext> .EfDbContextManager(QueryArgs); var dbSet = db.DbContext.Set <TModel>(); return execute((_query == null) ? dbSet : _query(dbSet)); }, _db)); }
/// <summary> /// Manages the DbContext and underlying query construction and lifetime. /// </summary> private void ExecuteQuery(Action <IQueryable <TModel> > execute) { EfDbInvoker <TDbContext> .Default.Invoke(this, () => { using var db = new EfDbBase <TDbContext> .EfDbContextManager(QueryArgs); var dbSet = db.DbContext.Set <TModel>(); execute((_query == null) ? dbSet : _query(dbSet)); }, _db); }
/// <summary> /// Initialize a new instance of the <see cref="EfDbContextManager"/> class. /// </summary> /// <param name="database">The database.</param> /// <param name="args">The <see cref="IEfDbArgs"/>.</param> public EfDbContextManager(EfDbBase <TDbContext> database, IEfDbArgs args) { if (args != null && args.DbContext != null) { DbContext = (TDbContext)args.DbContext; } else { _closeAndDispose = true; DbContext = new TDbContext(); } }
/// <summary> /// Invokes an <paramref name="action"/> synchronously. /// </summary> /// <param name="caller">The calling (invoking) object.</param> /// <param name="action">The function to invoke.</param> /// <param name="param">The optional parameter passed to the invoke.</param> /// <param name="memberName">The method or property name of the caller to the method.</param> /// <param name="filePath">The full path of the source file that contains the caller.</param> /// <param name="lineNumber">The line number in the source file at which the method is called.</param> protected override void WrapInvoke(object caller, Action action, EfDbBase <TDbContext> param = null, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int lineNumber = 0) { if (action == null) { throw new ArgumentNullException(nameof(action)); } try { action(); } catch (SqlException sex) { if (param != null) { param.ExceptionHandler?.Invoke(sex); } throw; } catch (Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException) { throw new ConcurrencyException(); } catch (Microsoft.EntityFrameworkCore.DbUpdateException deux) { if (deux.InnerException != null && deux.InnerException is SqlException sex) { if (param != null) { param.ExceptionHandler?.Invoke(sex); } } throw; } catch (TargetInvocationException tiex) { if (tiex?.InnerException is SqlException sex) { if (param != null) { param.ExceptionHandler?.Invoke(sex); } } throw; } }
/// <summary> /// Invokes a <paramref name="func"/> asynchronously. /// </summary> /// <param name="caller">The calling (invoking) object.</param> /// <param name="func">The function to invoke.</param> /// <param name="param">The optional parameter passed to the invoke.</param> /// <param name="memberName">The method or property name of the caller to the method.</param> /// <param name="filePath">The full path of the source file that contains the caller.</param> /// <param name="lineNumber">The line number in the source file at which the method is called.</param> protected async override Task WrapInvokeAsync(object caller, Func <Task> func, EfDbBase <TDbContext> param = null, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int lineNumber = 0) { if (func == null) { throw new ArgumentNullException(nameof(func)); } try { await func().ConfigureAwait(false); } catch (SqlException sex) { if (param != null) { param.ExceptionHandler?.Invoke(sex); } throw; } catch (Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException) { throw new ConcurrencyException(); } catch (Microsoft.EntityFrameworkCore.DbUpdateException deux) { if (deux.InnerException != null && deux.InnerException is SqlException sex) { if (param != null) { param.ExceptionHandler?.Invoke(sex); } } throw; } catch (TargetInvocationException tiex) { if (tiex?.InnerException is SqlException sex) { if (param != null) { param.ExceptionHandler?.Invoke(sex); } } throw; } }
/// <summary> /// Invokes a <paramref name="func"/> with a <typeparamref name="TResult"/> synchronously. /// </summary> /// <typeparam name="TResult">The result <see cref="Type"/>.</typeparam> /// <param name="caller">The calling (invoking) object.</param> /// <param name="func">The function to invoke.</param> /// <param name="param">The optional parameter passed to the invoke.</param> /// <param name="memberName">The method or property name of the caller to the method.</param> /// <param name="filePath">The full path of the source file that contains the caller.</param> /// <param name="lineNumber">The line number in the source file at which the method is called.</param> /// <returns>The result.</returns> protected override TResult WrapInvoke <TResult>(object caller, Func <TResult> func, EfDbBase <TDbContext>?param = null, [CallerMemberName] string?memberName = null, [CallerFilePath] string?filePath = null, [CallerLineNumber] int lineNumber = 0) { if (func == null) { throw new ArgumentNullException(nameof(func)); } try { return(func()); } catch (SqlException sex) { if (param != null) { param.ExceptionHandler?.Invoke(sex); } throw; } catch (Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException) { throw new ConcurrencyException(); } catch (Microsoft.EntityFrameworkCore.DbUpdateException deux) { if (deux.InnerException != null && deux.InnerException is SqlException sex) { if (param != null) { param.ExceptionHandler?.Invoke(sex); } } throw; } catch (TargetInvocationException tiex) { if (tiex?.InnerException is SqlException sex) { if (param != null) { param.ExceptionHandler?.Invoke(sex); } } throw; } }
/// <summary> /// Initializes a new instance of the <see cref="EfDbQuery{T, TModel, TDbContext}"/> class. /// </summary> /// <param name="db">The <see cref="DbSet{TModel}"/>.</param> /// <param name="queryArgs">The <see cref="EfDbArgs{T, TModel}"/>.</param> /// <param name="query">A function to modify the underlying <see cref="IQueryable{TModel}"/>.</param> internal EfDbQuery(EfDbBase <TDbContext> db, EfDbArgs <T, TModel> queryArgs, Func <IQueryable <TModel>, IQueryable <TModel> > query = null) { _db = db ?? throw new ArgumentNullException(nameof(db)); QueryArgs = queryArgs ?? throw new ArgumentNullException(nameof(queryArgs)); _query = query; }