Beispiel #1
0
 private void ConfigurePocoDataFactoryAndMappers(IDatabase database, MapperCollection mappers)
 {
     database.Mappers = mappers;
     if (_options.PocoDataFactory != null)
     {
         database.PocoDataFactory = _cachedPocoDataFactory = (_cachedPocoDataFactory == null ? _options.PocoDataFactory.Config(mappers) : _cachedPocoDataFactory);
     }
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="SqlContext"/> class.
        /// </summary>
        /// <param name="sqlSyntax">The sql syntax provider.</param>
        /// <param name="pocoDataFactory">The Poco data factory.</param>
        /// <param name="databaseType">The database type.</param>
        /// <param name="mappers">The mappers.</param>
        public SqlContext(ISqlSyntaxProvider sqlSyntax, DatabaseType databaseType, IPocoDataFactory pocoDataFactory, IMapperCollection?mappers = null)
        {
            // for tests
            Mappers = mappers;

            SqlSyntax       = sqlSyntax ?? throw new ArgumentNullException(nameof(sqlSyntax));
            PocoDataFactory = pocoDataFactory ?? throw new ArgumentNullException(nameof(pocoDataFactory));
            DatabaseType    = databaseType ?? throw new ArgumentNullException(nameof(databaseType));
            Templates       = new SqlTemplates(this);
        }
Beispiel #3
0
        private SqlContext Initialize()
        {
            _logger.LogDebug("Initializing.");

            if (ConnectionString.IsNullOrWhiteSpace())
            {
                throw new InvalidOperationException("The factory has not been configured with a proper connection string.");
            }

            if (ProviderName.IsNullOrWhiteSpace())
            {
                throw new InvalidOperationException("The factory has not been configured with a proper provider name.");
            }

            if (DbProviderFactory == null)
            {
                throw new Exception($"Can't find a provider factory for provider name \"{ProviderName}\".");
            }

            _databaseType = DatabaseType.Resolve(DbProviderFactory.GetType().Name, ProviderName);
            if (_databaseType == null)
            {
                throw new Exception($"Can't find an NPoco database type for provider name \"{ProviderName}\".");
            }

            _sqlSyntax = _dbProviderFactoryCreator.GetSqlSyntaxProvider(ProviderName);
            if (_sqlSyntax == null)
            {
                throw new Exception($"Can't find a sql syntax provider for provider name \"{ProviderName}\".");
            }

            _bulkSqlInsertProvider = _dbProviderFactoryCreator.CreateBulkSqlInsertProvider(ProviderName);

            _databaseType = _sqlSyntax.GetUpdatedDatabaseType(_databaseType, ConnectionString);

            // ensure we have only 1 set of mappers, and 1 PocoDataFactory, for all database
            // so that everything NPoco is properly cached for the lifetime of the application
            _pocoMappers = new NPoco.MapperCollection();
            // add all registered mappers for NPoco
            _pocoMappers.AddRange(_npocoMappers);

            _pocoMappers.AddRange(_dbProviderFactoryCreator.ProviderSpecificMappers(ProviderName));

            var factory = new FluentPocoDataFactory(GetPocoDataFactoryResolver, _pocoMappers);

            _pocoDataFactory = factory;
            var config = new FluentConfig(xmappers => factory);

            // create the database factory
            _npocoDatabaseFactory = DatabaseFactory.Config(cfg =>
            {
                cfg.UsingDatabase(CreateDatabaseInstance) // creating UmbracoDatabase instances
                .WithFluentConfig(config);                // with proper configuration

                foreach (IProviderSpecificInterceptor interceptor in _dbProviderFactoryCreator.GetProviderSpecificInterceptors(ProviderName))
                {
                    cfg.WithInterceptor(interceptor);
                }
            });

            if (_npocoDatabaseFactory == null)
            {
                throw new NullReferenceException("The call to UmbracoDatabaseFactory.Config yielded a null UmbracoDatabaseFactory instance.");
            }

            _logger.LogDebug("Initialized.");

            return(new SqlContext(_sqlSyntax, _databaseType, _pocoDataFactory, _mappers));
        }
Beispiel #4
0
 // gets initialized poco data builders
 private InitializedPocoDataBuilder GetPocoDataFactoryResolver(Type type, IPocoDataFactory factory)
 => new UmbracoPocoDataBuilder(type, _pocoMappers, _upgrading).Init();
Beispiel #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SqlContext"/> class.
 /// </summary>
 /// <param name="sqlSyntax">The sql syntax provider.</param>
 /// <param name="pocoDataFactory">The Poco data factory.</param>
 /// <param name="databaseType">The database type.</param>
 /// <param name="mappers">The mappers.</param>
 public SqlContext(ISqlSyntaxProvider sqlSyntax, DatabaseType databaseType, IPocoDataFactory pocoDataFactory, IMapperCollection mappers = null)
     : this(sqlSyntax, databaseType, pocoDataFactory, new Lazy <IMapperCollection>(() => mappers ?? new Mappers.MapperCollection(Enumerable.Empty <BaseMapper>())))
 {
 }