public virtual List <Column> GetColumns(Table table) { try { using (var ctx = DataContextScope.GetCurrent(ConnectionName).Begin()) { var columns = Provider.GetColumns(table); if (columns != null) { table.Columns = columns; var index = 0; foreach (var column in columns) { column.Table = table; column.Index = index++; } } return(columns); } } catch (Exception ex) { Trace.TraceError("Get Columns Exception:" + ex.ToString()); } return(new List <Column>()); }
/// <summary> /// 开启数据上下文,并异步执行后续 Http 管道处理请求。 /// </summary> /// <param name="request">一个 Http 请求的实例对象。</param> /// <param name="cancellationToken">和异步执行取消操作有关的通知对象。</param> /// <returns>异步执行的任务对象。</returns> protected override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { using (var ctx = new DataContextScope()) { return(base.SendAsync(request, cancellationToken)); } }
public virtual List <Table> GetTables(Database db) { try { using (var ctx = DataContextScope.GetCurrent(ConnectionName).Begin()) { var tables = Provider.GetTables(db); if (tables != null) { foreach (var table in tables) { table.Owner = db.Name; } } FixTables(tables); return(tables); } } catch (Exception ex) { Trace.TraceError("Get GetTables Exception:" + ex.ToString()); } return(new List <Table>()); }
/// <summary> /// Invokes a <paramref name="func"/> with a <typeparamref name="TResult"/> asynchronously. /// </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 async override Task <TResult> WrapInvokeAsync <TResult>(object caller, Func <Task <TResult> > func, BusinessInvokerArgs param = null, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int lineNumber = 0) { Check.NotNull(func, nameof(func)); BusinessInvokerArgs bia = Check.NotNull(param ?? BusinessInvokerArgs.Default, nameof(param)); TransactionScope txn = null; DataContextScope ctx = null; OperationType ot = ExecutionContext.Current.OperationType; try { if (bia.IncludeTransactionScope) { txn = new TransactionScope(bia.TransactionScopeOption); } ctx = DataContextScope.Begin(bia.DataContextScopeOption); var result = await func().ConfigureAwait(false); if (txn != null) { txn.Complete(); } return(result); } catch (Exception ex) { bia.ExceptionHandler?.Invoke(ex); throw; } finally { if (ctx != null) { ctx.Dispose(); } if (txn != null) { txn.Dispose(); } ExecutionContext.Current.OperationType = ot; } }
/// <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, BusinessInvokerArgs?param = null, [CallerMemberName] string?memberName = null, [CallerFilePath] string?filePath = null, [CallerLineNumber] int lineNumber = 0) { Check.NotNull(func, nameof(func)); BusinessInvokerArgs bia = param ?? BusinessInvokerArgs.Default; TransactionScope? txn = null; DataContextScope? ctx = null; OperationType ot = ExecutionContext.Current.OperationType; try { if (bia.IncludeTransactionScope) { txn = new TransactionScope(bia.TransactionScopeOption, TransactionScopeAsyncFlowOption.Enabled); } ctx = DataContextScope.Begin(bia.DataContextScopeOption); var result = func(); if (txn != null) { txn.Complete(); } return(result); } catch (Exception ex) { bia.ExceptionHandler?.Invoke(ex); throw; } finally { if (ctx != null) { ctx.Dispose(); } if (txn != null) { txn.Dispose(); } ExecutionContext.Current.OperationType = ot; } }
/// <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, BusinessInvokerArgs param = null, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int lineNumber = 0) { Check.NotNull(action, nameof(action)); BusinessInvokerArgs bia = param ?? BusinessInvokerArgs.Default; TransactionScope txn = null; DataContextScope ctx = null; OperationType ot = ExecutionContext.Current.OperationType; try { if (bia.IncludeTransactionScope) { txn = new TransactionScope(bia.TransactionScopeOption); } ctx = DataContextScope.Begin(bia.DataContextScopeOption); action(); if (txn != null) { txn.Complete(); } } catch (Exception ex) { bia.ExceptionHandler?.Invoke(ex); throw; } finally { if (ctx != null) { ctx.Dispose(); } if (txn != null) { txn.Dispose(); } ExecutionContext.Current.OperationType = ot; } }
public virtual List <Database> GetDatabases() { try { using (var ctx = DataContextScope.GetCurrent(ConnectionName).Begin()) { var dbs = Provider.GetDatabases(); return(dbs); } } catch (Exception ex) { Trace.TraceError("Get Databases Exception:" + ex.ToString()); } return(new List <Database>()); }
#pragma warning disable CA1000 // Do not declare static members on generic types; by-design, is ok. /// <summary> /// Registers (creates) the <see cref="Default"/> <see cref="DatabaseBase"/> instance; as well as registering with the <see cref="DataContextScope"/> for connection management /// and initiating the <see cref="OnConnectionOpen(DbConnection)"/>. /// </summary> /// <param name="create">Function to create the <see cref="Default"/> instance.</param> public static void Register(Func <TDefault> create) { lock (_lock) { if (_default != null) { throw new InvalidOperationException("The Register method can only be invoked once."); } _create = create ?? throw new ArgumentNullException(nameof(create)); DataContextScope.RegisterContext <TDefault, DbConnection>(() => { var conn = Default.CreateConnection(false); conn.Open(); Default.OnConnectionOpen(conn); return(conn); }); } }
public virtual List <Database> GetComplexDatabases() { try { using (var ctx = DataContextScope.GetCurrent(ConnectionName).Begin()) { var dbs = Provider.GetDatabases(); if (dbs != null) { foreach (var db in dbs) { db.Tables = Provider.GetTables(db); FixTables(db.Tables); if (db.Tables != null) { foreach (var table in db.Tables) { table.Owner = db.Name; } } } } return(dbs); } } catch (Exception ex) { Trace.TraceError("Get Complex Databases Exception:" + ex.ToString()); } return(new List <Database>()); }
public DataProviderBase(string connectionName) { _connectionName = connectionName; _dataContext = DataContextScope.GetCurrent(connectionName).DataContext; _db = _dataContext.DatabaseObject; }
public DbSchemaBase(String connName) { ConnectionName = connName; Context = DataContextScope.GetCurrent(ConnectionName).DataContext; }