Esempio n. 1
0
        public InMemoryDataStore(
            [NotNull] StateManager stateManager,
            [NotNull] DbContextService <IModel> model,
            [NotNull] EntityKeyFactorySource entityKeyFactorySource,
            [NotNull] EntityMaterializerSource entityMaterializerSource,
            [NotNull] ClrCollectionAccessorSource collectionAccessorSource,
            [NotNull] ClrPropertySetterSource propertySetterSource,
            [NotNull] InMemoryDatabase persistentDatabase,
            [NotNull] DbContextService <IDbContextOptions> options,
            [NotNull] ILoggerFactory loggerFactory)
            : base(stateManager, model, entityKeyFactorySource, entityMaterializerSource,
                   collectionAccessorSource, propertySetterSource, loggerFactory)
        {
            Check.NotNull(persistentDatabase, "persistentDatabase");

            var storeConfig = options.Service.Extensions
                              .OfType <InMemoryOptionsExtension>()
                              .FirstOrDefault();

            _persist = (storeConfig != null ? (bool?)storeConfig.Persist : null) ?? true;

            _database = new ThreadSafeLazyRef <InMemoryDatabase>(
                () => _persist
                    ? persistentDatabase
                    : new InMemoryDatabase(loggerFactory));
        }
        public InMemoryDataStore(
            [NotNull] IModel model,
            [NotNull] IEntityKeyFactorySource entityKeyFactorySource,
            [NotNull] IEntityMaterializerSource entityMaterializerSource,
            [NotNull] InMemoryDatabase persistentDatabase,
            [NotNull] IDbContextOptions options,
            [NotNull] ILoggerFactory loggerFactory)
            : base(
                Check.NotNull(model, nameof(model)),
                Check.NotNull(entityKeyFactorySource, nameof(entityKeyFactorySource)),
                Check.NotNull(entityMaterializerSource, nameof(entityMaterializerSource)),
                Check.NotNull(loggerFactory, nameof(loggerFactory)))
        {
            Check.NotNull(persistentDatabase, nameof(persistentDatabase));

            var storeConfig = options.Extensions
                              .OfType <InMemoryOptionsExtension>()
                              .FirstOrDefault();

            _persist = storeConfig?.Persist ?? true;

            _database = new ThreadSafeLazyRef <InMemoryDatabase>(
                () => _persist
                    ? persistentDatabase
                    : new InMemoryDatabase(loggerFactory));
        }
Esempio n. 3
0
        public InMemoryDatabase(
            [NotNull] IModel model,
            [NotNull] IEntityKeyFactorySource entityKeyFactorySource,
            [NotNull] IEntityMaterializerSource entityMaterializerSource,
            [NotNull] IClrAccessorSource <IClrPropertyGetter> clrPropertyGetterSource,
            [NotNull] IInMemoryStore persistentStore,
            [NotNull] IDbContextOptions options,
            [NotNull] ILoggerFactory loggerFactory)
            : base(model, loggerFactory)
        {
            Check.NotNull(entityKeyFactorySource, nameof(entityKeyFactorySource));
            Check.NotNull(entityMaterializerSource, nameof(entityMaterializerSource));
            Check.NotNull(clrPropertyGetterSource, nameof(clrPropertyGetterSource));
            Check.NotNull(persistentStore, nameof(persistentStore));
            Check.NotNull(options, nameof(options));
            Check.NotNull(loggerFactory, nameof(loggerFactory));

            _entityKeyFactorySource   = entityKeyFactorySource;
            _entityMaterializerSource = entityMaterializerSource;
            _clrPropertyGetterSource  = clrPropertyGetterSource;

            var storeConfig = options.Extensions
                              .OfType <InMemoryOptionsExtension>()
                              .FirstOrDefault();

            _persist = storeConfig?.Persist ?? true;

            _database = new ThreadSafeLazyRef <IInMemoryStore>(
                () => _persist
                    ? persistentStore
                    : new InMemoryStore(loggerFactory));
        }
        public void Has_value_is_false_until_value_accessed()
        {
            var safeLazy = new ThreadSafeLazyRef<string>(() => "s");

            Assert.False(safeLazy.HasValue);
            Assert.Equal("s", safeLazy.Value);
            Assert.True(safeLazy.HasValue);
        }
Esempio n. 5
0
        public void Has_value_is_false_until_value_accessed()
        {
            var safeLazy = new ThreadSafeLazyRef <string>(() => "s");

            Assert.False(safeLazy.HasValue);
            Assert.Equal("s", safeLazy.Value);
            Assert.True(safeLazy.HasValue);
        }
Esempio n. 6
0
        public NavigationAccessor(
            [NotNull] Func <IClrPropertyGetter> getter,
            [NotNull] Func <IClrPropertySetter> setter)
        {
            Check.NotNull(getter, "getter");
            Check.NotNull(setter, "setter");

            _getter = new ThreadSafeLazyRef <IClrPropertyGetter>(getter);
            _setter = new ThreadSafeLazyRef <IClrPropertySetter>(setter);
        }
        public CollectionNavigationAccessor(
            [NotNull] Func<IClrPropertyGetter> getter,
            [NotNull] Func<IClrPropertySetter> setter,
            [NotNull] Func<IClrCollectionAccessor> collectionAccessor)
            : base(getter, setter)
        {
            Check.NotNull(collectionAccessor, "collectionAccessor");

            _collectionAccessor = new ThreadSafeLazyRef<IClrCollectionAccessor>(collectionAccessor);
        }
Esempio n. 8
0
        public ValueGeneratorPool([NotNull] IValueGeneratorFactory factory, [NotNull] IProperty property, int poolSize)
        {
            Check.NotNull(factory, "factory");
            Check.NotNull(property, "property");

            _pool = new ThreadSafeLazyRef <IValueGenerator> [poolSize];
            for (var i = 0; i < poolSize; i++)
            {
                _pool[i] = new ThreadSafeLazyRef <IValueGenerator>(() => factory.Create(property));
            }
        }
        public ValueGeneratorPool([NotNull] IValueGeneratorFactory factory, [NotNull] IProperty property, int poolSize)
        {
            Check.NotNull(factory, "factory");
            Check.NotNull(property, "property");

            _pool = new ThreadSafeLazyRef<IValueGenerator>[poolSize];
            for (var i = 0; i < poolSize; i++)
            {
                _pool[i] = new ThreadSafeLazyRef<IValueGenerator>(() => factory.Create(property));
            }
        }
        public async Task Can_exchange_value()
        {
            var safeLazy = new ThreadSafeLazyRef<string>(() => "");
            var tasks = new List<Task>();

            for (var i = 0; i < 10; i++)
            {
                tasks.Add(Task.Run(() => safeLazy.ExchangeValue(s => s + "s")));
            }

            await Task.WhenAll(tasks);

            Assert.Equal("ssssssssss", safeLazy.Value);
        }
Esempio n. 11
0
        public async Task Can_exchange_value()
        {
            var safeLazy = new ThreadSafeLazyRef <string>(() => "");
            var tasks    = new List <Task>();

            for (var i = 0; i < 10; i++)
            {
                tasks.Add(Task.Run(() => safeLazy.ExchangeValue(s => s + "s")));
            }

            await Task.WhenAll(tasks);

            Assert.Equal("ssssssssss", safeLazy.Value);
        }
        public async Task Can_initialize_from_multiple_threads_and_initialization_happens_only_once()
        {
            var counter = 0;
            var safeLazy = new ThreadSafeLazyRef<string>(() => counter++.ToString());
            var tasks = new List<Task>();

            for (var i = 0; i < 10; i++)
            {
                tasks.Add(Task.Run(() => safeLazy.Value));
            }

            await Task.WhenAll(tasks);

            Assert.Equal(1, counter);
        }
Esempio n. 13
0
        public async Task Can_initialize_from_multiple_threads_and_initialization_happens_only_once()
        {
            var counter  = 0;
            var safeLazy = new ThreadSafeLazyRef <string>(() => counter++.ToString());
            var tasks    = new List <Task>();

            for (var i = 0; i < 10; i++)
            {
                tasks.Add(Task.Run(() => safeLazy.Value));
            }

            await Task.WhenAll(tasks);

            Assert.Equal(1, counter);
        }
Esempio n. 14
0
        public AtsConnection([NotNull] DbContextConfiguration configuration)
        {
            Check.NotNull(configuration, "configuration");

            var storeConfig = configuration
                              .ContextOptions
                              .Extensions
                              .OfType <AtsOptionsExtension>()
                              .Single();

            var connectionString = storeConfig.ConnectionString;

            Batching = storeConfig.UseBatching;

            _account     = new ThreadSafeLazyRef <CloudStorageAccount>(() => CloudStorageAccount.Parse(connectionString));
            _tableClient = new ThreadSafeLazyRef <CloudTableClient>(() => _account.Value.CreateCloudTableClient());
        }
        public InMemoryDataStore(
            [NotNull] DbContextConfiguration configuration,
            [NotNull] InMemoryDatabase persistentDatabase)
            : base(configuration)
        {
            Check.NotNull(configuration, "configuration");
            Check.NotNull(persistentDatabase, "persistentDatabase");

            var storeConfig = configuration.ContextOptions.Extensions
                              .OfType <InMemoryConfigurationExtension>()
                              .FirstOrDefault();

            _persist = (storeConfig != null ? (bool?)storeConfig.Persist : null) ?? true;

            _database = new ThreadSafeLazyRef <InMemoryDatabase>(
                () => _persist
                    ? persistentDatabase
                    : new InMemoryDatabase(configuration.LoggerFactory));
        }