public TDbContext Get <TDbContext>() where TDbContext : DbContext
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("DbContextCollection");
            }

            var requestedType = typeof(TDbContext);

            if (!_initializedDbContexts.ContainsKey(requestedType))
            {
                // First time we've been asked for this particular DbContext type.
                // Create one, cache it and start its database transaction if needed.
                var dbContext = _dbContextFactory != null
                    ? _dbContextFactory.Create <TDbContext>()
                    : Activator.CreateInstance <TDbContext>();

                _initializedDbContexts.Add(requestedType, dbContext);

                if (_readOnly)
                {
                    dbContext.Configuration.AutoDetectChangesEnabled = false;
                }

                if (_isolationLevel.HasValue)
                {
                    var tran = dbContext.Database.BeginTransaction(_isolationLevel.Value);
                    _transactions.Add(dbContext, tran);
                }
            }

            return(_initializedDbContexts[requestedType]  as TDbContext);
        }
示例#2
0
 private Task <T> UsingContext <T>(Func <Task <T> > dbQuery)
 {
     // Join to parent context if it exists
     using (var context = _ambientDbContextFactory.Create(join: true, suppress: true))
     {
         return(dbQuery());
     }
 }
示例#3
0
        /// <summary>
        /// the basic ctor
        /// </summary>
        /// <param name="factory">the ambient context factory</param>
        protected BaseService(
            IAmbientDbContextFactory factory
            )
        {
            AmbientDbContextStorageProvider.SetStorage(new AsyncLocalContextStorage());
            ContextFactory = factory;

            ContextFactory.Create();
        }
        public async Task RunAsync()
        {
            // This will not actually work because there is no database to connect to.
            // Rather, this is a way to showcase how the library is supposed to be used.
            using (var ambientContext = _ambientContextFactory.Create())
            {
                await _anUpdateQuery.ExecuteAsync();

                await _anInsertQuery.ExecuteAsync();

                ambientContext.Commit();
            }
        }
        public async Task <IActionResult> Put(int id, [FromBody] string value)
        {
            // This will not actually work because there is no database to connect to.
            // Rather, this is a way to showcase how the library is supposed to be used.
            using (var ambientContext = _ambientContextFactory.Create())
            {
                await _anUpdateQuery.ExecuteAsync();

                await _anInsertQuery.ExecuteAsync();

                ambientContext.Commit();
            }

            return(NoContent());
        }
示例#6
0
        public static void AssemblyInit(TestContext testContext)
        {

            Configuration = GetIConfigurationRoot(testContext.TestRunDirectory);

            AmbientDbContextStorageProvider.SetStorage(new AsyncLocalContextStorage());

            ContextFactory = new AmbientDbContextFactory(new DbConnectionFactory(Configuration));

            ContextFactory.Create();

            ContextLocator = new AmbientDbContextLocator();

            DbContext = ContextLocator.Get();

        }
 public void Handle(TNotification command)
 {
     using (var ambientContext = _ambientDbContextFactory.Create())
     {
         try
         {
             _decorated.Handle(command);
             ambientContext.Commit();
         }
         catch
         {
             ambientContext.Rollback();
             throw;
         }
     }
 }
示例#8
0
 public virtual Task Start()
 {
     _ambientDbContext = _ambientDbContextFactory.Create();
     return(Task.CompletedTask);
 }