Example #1
0
 /// <summary>
 ///
 /// </summary>
 public static void Setup()
 {
     DbFactory = DatabaseFactory.Config(x =>
     {
         //x.UsingDatabase(() => new NPoco.Database(new ));
     });
 }
Example #2
0
        public DbFactory()
        {
            var fluentConfig = FluentMappingConfiguration.Scan(scanner =>
            {
                scanner.TheCallingAssembly();
                scanner.IncludeTypes(x => x.IsClass && !x.IsAbstract && x.Namespace.EndsWith("Models"));
                scanner.WithSmartConventions();
                scanner.PrimaryKeysNamed(m => "Id"); // override primary key name convention from "SmartConventions"
                scanner.Columns
                .IgnoreWhere(mi => ColumnInfo.FromMemberInfo(mi).IgnoreColumn)
                .ResultWhere(mi => ColumnInfo.FromMemberInfo(mi).ResultColumn)
                .ComputedWhere(mi => ColumnInfo.FromMemberInfo(mi).ComputedColumn)
                .VersionWhere(mi => ColumnInfo.FromMemberInfo(mi).VersionColumn)
                .Named(mi => mi.GetCustomAttributes <ColumnAttribute>().Any() ? mi.GetCustomAttribute <ColumnAttribute>().Name : mi.Name)
                .Aliased(mi => mi.GetCustomAttributes <AliasAttribute>().Any() ? mi.GetCustomAttribute <AliasAttribute>().Alias : null)
                .DbColumnTypeAs(mi => mi.GetCustomAttributes <ColumnTypeAttribute>().Any() ? mi.GetCustomAttribute <ColumnTypeAttribute>().Type : null);
            });

            var connString = ConfigurationManager.ConnectionStrings["test"].ConnectionString;

            factory = DatabaseFactory.Config(x =>
            {
                x.UsingDatabase(() => new Database(connString, databaseType));
                x.WithFluentConfig(fluentConfig);
            });
        }
Example #3
0
 static NPocoDatabase()
 {
     factory = DatabaseFactory.Config(x => {
         x.UsingDatabase(() => new NPocoDatabase("connectionStringName"));
         x.WithFluentConfig(FluentMappingConfiguration.Configure(new NPocoDatabaseMappings()));
     });
 }
Example #4
0
        private IDatabase CreateDatabaseInstance(IHostingEnvironment env)
        {
            Database database;

            if (env.IsDevelopment())
            {
                var s = new SqliteIntegration();
                s.EnsureSharedConnectionConfigured();

                database = s.CreateDatabase();

                s.RecreateSchema();
            }
            else
            {
                database = new Database("Server=localhost;Database=npoco;Trusted_Connection=True;",
                                        DatabaseType.SqlServer2012,
                                        SqlClientFactory.Instance);
            }

            var dbFactory = DatabaseFactory.Config(x =>
            {
                var fluentConfig = FluentMappingConfiguration.Configure(new NPocoLabMappings());
                x.UsingDatabase(() => database);
                x.WithFluentConfig(fluentConfig);
            });

            return(dbFactory.GetDatabase());
        }
Example #5
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var connection = Configuration.GetConnectionString("DefaultConnection");
            var dbFactory  = DatabaseFactory.Config(x =>
            {
                x.UsingDatabase(() => new Database(connection, DatabaseType.SqlServer2012, System.Data.SqlClient.SqlClientFactory.Instance));
                x.WithFluentConfig(FluentMappingConfiguration.Configure(new NpocoMappings()));
            });

            services.AddSingleton(dbFactory);

            services.AddControllers();
            services.AddScoped <IUserService, UserService>();
            services.AddScoped <IVehicleService, VehicleService>();

            services.AddCors(options =>
            {
                options.AddPolicy("AllowAllOrigins",
                                  builder =>
                {
                    builder.AllowAnyOrigin();
                    builder.AllowAnyHeader();
                    builder.AllowAnyMethod();
                });
            });
            //   services.AddSwaggerGen(c =>
            //   {
            //     c.SwaggerDoc("v1", new Info { Title = "API", Version = "v1" });
            //   });
        }
 public static void Init()
 {
     _DbFactory = DatabaseFactory.Config(x =>
     {
         x.UsingDatabase(() => new NPocoDataBase(_defaultConnectionName));
     });
 }
Example #7
0
 public static DatabaseFactory Get(string connectionString)
 {
     return(DatabaseFactory.Config(x =>
     {
         x.UsingDatabase(() => new Database(connectionString, ProviderName));
         x.WithFluentConfig(Config);
     }));
 }
Example #8
0
        public void SetUp()
        {
            var types     = new[] { typeof(User), typeof(ExtraUserInfo), typeof(Usersss), typeof(House), typeof(Supervisor) };
            var dbFactory = new DatabaseFactory();

            dbFactory.Config().WithFluentConfig(
                FluentMappingConfiguration.Scan(s =>
            {
                s.Assembly(typeof(User).Assembly);
                s.IncludeTypes(types.Contains);
                s.WithSmartConventions();
                s.Columns.ResultWhere(y => ColumnInfo.FromMemberInfo(y).IgnoreColumn);
                s.OverrideMappingsWith(new FluentMappingOverrides());
            })
                );



            var testDBType = Convert.ToInt32(ConfigurationManager.AppSettings["TestDBType"]);

            switch (testDBType)
            {
            case 1:     // SQLite In-Memory
                TestDatabase = new InMemoryDatabase();
                Database     = dbFactory.Build(new Database(TestDatabase.Connection));
                break;

            case 2:     // SQL Local DB
            case 3:     // SQL Server
                TestDatabase = new SQLLocalDatabase();
                Database     = dbFactory.Build(new Database(TestDatabase.Connection, new SqlServer2008DatabaseType()
                {
                    UseOutputClause = true
                }));
                break;

            case 4:     // SQL CE
            case 5:     // MySQL
            case 6:     // Oracle
            case 7:     // Postgres
                Assert.Fail("Database platform not supported for unit testing");
                return;

            case 8:     // Firebird
                TestDatabase = new FirebirdDatabase();
                var db = new Database(TestDatabase.Connection, new FirebirdDatabaseType());
                db.Mapper = new FirebirdDefaultMapper();
                Database  = dbFactory.Build(db);
                break;

            default:
                Assert.Fail("Unknown database platform specified");
                return;
            }

            InsertData();
        }
Example #9
0
        public void Setup()
        {
            var dbfactory = new DatabaseFactory();
            dbfactory
                .Config()
                .UsingDatabase(() => new Database(""))
                .WithFluentConfig(FluentMappingConfiguration.Configure(new MyMappings()));

            _database = dbfactory.GetDatabase();
        }
Example #10
0
        /// <summary>
        ///
        /// </summary>
        public static void Setup()
        {
            var fluentConfig = FluentMappingConfiguration.Configure(new SqliteMappings());

            DbFactory = DatabaseFactory.Config(x =>
            {
                x.UsingDatabase(() => new NPoco.Database("conndb", NPoco.DatabaseType.SQLite));
                x.WithFluentConfig(fluentConfig);
            });
        }
Example #11
0
        /// <summary>
        ///
        /// </summary>
        public static void Setup(string config)
        {
            var fluentConfig = FluentMappingConfiguration.Configure(new ReviewsServiceMappings());

            DbFactory = DatabaseFactory.Config(x =>
            {
                x.UsingDatabase(() => new NPoco.Database(config, DatabaseType.SqlServer2012, SqlClientFactory.Instance));
                x.WithFluentConfig(fluentConfig);
            });
        }
Example #12
0
        public void Setup()
        {
            var config = FluentMappingConfiguration.Configure(new ServerDomainDBMapping());

            DbFactory = DatabaseFactory.Config(x =>
            {
                x.UsingDatabase(() => new Database(this.connectionString, DatabaseType.SQLite));
                x.WithFluentConfig(config);
            });
        }
Example #13
0
        public static void Setup(IConfiguration configuration)
        {
            var fluentConfig = FluentMappingConfiguration.Configure(new LunchMapping(), new AttendeeMapping());

            DbFactory = DatabaseFactory.Config(x =>
            {
                x.UsingDatabase(() => new Database(configuration.GetConnectionString("LocsDatabase"), DatabaseType.SqlServer2012, SqlClientFactory.Instance));
                x.WithFluentConfig(fluentConfig);
            });
        }
Example #14
0
        public static DatabaseFactory InitializeFactory(string connectionString)
        {
            string connection = ConfigurationManager.ConnectionStrings["ConnectionStringDEBUG"].ConnectionString;
            string provider   = ConfigurationManager.ConnectionStrings["ConnectionStringDEBUG"].ProviderName;

            return(DatabaseFactory.Config(x =>
            {
                x.UsingDatabase(() => new Database(connection, provider));
            }));
        }
Example #15
0
        public void DatabaseShouldbeReturnedWhenInsertedIntoFactoryConfig()
        {
            var db = new Database(new SqlConnection());

            var factory = DatabaseFactory.Config(x =>
            {
                x.UsingDatabase(() => db);
            });

            Assert.AreEqual(db, factory.GetDatabase());
        }
Example #16
0
        public void Setup()
        {
            var dbfactory = new DatabaseFactory();

            dbfactory
            .Config()
            .UsingDatabase(() => new Database(""))
            .WithFluentConfig(FluentMappingConfiguration.Configure(new MyMappings()));

            _database = dbfactory.GetDatabase();
        }
Example #17
0
        public static void Setup(Mappings MappingClass)
        {
            var fluentConfig = FluentMappingConfiguration.Configure(MappingClass);

            DbFactory = DatabaseFactory.Config(x =>
            {
                x.UsingDatabase(() => new Database("CommandsDb"));
                x.WithFluentConfig(fluentConfig);
                //x.WithMapper(new Mapper());
            });
        }
Example #18
0
        /// <summary>
        ///
        /// </summary>
        public static void Setup(string config)
        {
            //NpgsqlConnection connection = new NpgsqlConnection(config);
            var fluentConfig = FluentMappingConfiguration.Configure(new OTPSolutionMappings());

            DbFactory = DatabaseFactory.Config(x =>
            {
                x.UsingDatabase(() => new NPoco.Database(config, DatabaseType.SqlServer2012, SqlClientFactory.Instance));
                x.WithFluentConfig(fluentConfig);
            });
        }
Example #19
0
        public static void Setup()
        {
            FluentConfig fluentConfig = FluentMappingConfiguration.Configure(new EmployeeMapping());

            DbFactory = DatabaseFactory.Config(x =>
            {
                x.UsingDatabase(() => new SqlServerDatabase(AppSettings.StringConnection));
                x.WithFluentConfig(fluentConfig);
                x.WithMapper(new Mapper());
            });
        }
Example #20
0
        public void Setup()
        {
            var dbfactory = new DatabaseFactory();

            dbfactory
            .Config()
            .UsingDatabase(() => new Database("", new SqlServer2012DatabaseType(), SqlClientFactory.Instance))
            .WithFluentConfig(FluentMappingConfiguration.Configure(new MyMappings()));

            _database = dbfactory.GetDatabase();
        }
Example #21
0
        public void MapperShouldBePlacedOnDatabaseWhenInsertedIntoFactoryConfig()
        {
            var db     = new Database(new SqlConnection());
            var mapper = new Mapper();

            var factory = DatabaseFactory.Config(x =>
            {
                x.WithMapper(mapper);
            });

            Assert.AreEqual(mapper, factory.Build(db).Mapper);
        }
Example #22
0
        public Database CreateDatabase()
        {
            var fluentConfig = FluentMappingConfiguration.Configure(new NPocoLabMappings());

            var dbFactory = DatabaseFactory.Config(x =>
            {
                x.UsingDatabase(() => new Database(Connection, DatabaseType.SQLite));
                x.WithFluentConfig(fluentConfig);
            });


            return(dbFactory.GetDatabase());
        }
Example #23
0
        public void MapperShouldBePlacedOnDatabaseWhenInsertedIntoFactoryConfigWhenCallingGetDatabase()
        {
            var db     = new Database(new SqlConnection());
            var mapper = new Mapper();

            var factory = DatabaseFactory.Config(x =>
            {
                x.UsingDatabase(() => db);
                x.WithMapper(mapper);
            });

            Assert.AreEqual(mapper, factory.GetDatabase().Mapper);
        }
Example #24
0
        public void MapperShouldBePlacedOnDatabaseWhenInsertedIntoFactoryConfigWhenCallingGetDatabase()
        {
            var db     = new Database(new SqlConnection());
            var mapper = new Mapper();

            var factory = DatabaseFactory.Config(x =>
            {
                x.UsingDatabase(() => db);
                var databaseFactoryConfig = x.WithMapper(mapper);
            });

            Assert.True(factory.Build(db).Mappers.Contains(mapper));
        }
Example #25
0
        public static void Init()
        {
            DbFact = new DatabaseFactory();
            var config = FluentMappingConfiguration.Scan(x =>
            {
                x.TheCallingAssembly();
                x.WithSmartConventions(true);
                x.Columns.IgnoreWhere(y => y.GetCustomAttributes(typeof(IgnoreAttribute), true).Any());
                x.OverrideMappingsWith(new OverrideNPoco());
            });

            DbFact.Config().WithFluentConfig(config);
        }
Example #26
0
        public void SetUp()
        {
            var types = new[] { typeof(User), typeof(ExtraUserInfo), typeof(Usersss), typeof(House), typeof(Supervisor) };
            var dbFactory = new DatabaseFactory();
            dbFactory.Config().WithFluentConfig(
                FluentMappingConfiguration.Scan(s =>
                {
                    s.Assembly(typeof (User).Assembly);
                    s.IncludeTypes(types.Contains);
                    s.WithSmartConventions();
                    s.Columns.ResultWhere(y => ColumnInfo.FromMemberInfo(y).IgnoreColumn);
                    s.OverrideMappingsWith(new FluentMappingOverrides());
                })
            );
            


            var testDBType = Convert.ToInt32(ConfigurationManager.AppSettings["TestDBType"]);
            switch (testDBType)
            {
                case 1: // SQLite In-Memory
                    TestDatabase = new InMemoryDatabase();
                    Database = dbFactory.Build(new Database(TestDatabase.Connection));
                    break;

                case 2: // SQL Local DB
                case 3: // SQL Server
                    TestDatabase = new SQLLocalDatabase();
                    Database = dbFactory.Build(new Database(TestDatabase.Connection, new SqlServer2008DatabaseType()));
                    break;

                case 4: // SQL CE
                case 5: // MySQL
                case 6: // Oracle
                case 7: // Postgres
                    Assert.Fail("Database platform not supported for unit testing");
                    return;
                case 8: // Firebird
                    TestDatabase = new FirebirdDatabase();
                    var db = new Database(TestDatabase.Connection, new FirebirdDatabaseType());
                    db.Mapper = new FirebirdDefaultMapper();
                    Database = dbFactory.Build(db);
                    break;

                default:
                    Assert.Fail("Unknown database platform specified");
                    return;
            }

            InsertData();
        }
Example #27
0
        public void FluentConfigShouldBePlacedOnDatabaseWhenInsertedIntoFactoryConfig()
        {
            var db           = new Database(new SqlConnection());
            var fluentConfig = new FluentConfig(x => y => new PocoData(y, new Mapper()));

            var factory = DatabaseFactory.Config(x =>
            {
                x.UsingDatabase(() => db);
                x.WithFluentConfig(fluentConfig);
            });

            var database = factory.GetDatabase();

            Assert.AreEqual(fluentConfig.Config(null), database.PocoDataFactory);
        }
Example #28
0
        /// <summary>
        /// Initializes the configuration and sets the <see cref="Factory"/> property.
        /// </summary>
        /// <remarks>Adapted from https://github.com/schotime/NPoco/wiki/Fluent-Mappings-including-Conventional
        /// </remarks>
        public static void Setup(IDatabaseConfigurationSettings databaseConfigurationSettings)
        {
            var fluentConfig = FluentMappingConfiguration.Configure(new IMap[]
            {
                new LocationInformationMapping(),
                new PropertyEnquiryMapping(),
                new PropertyInformationMapping(),
                new PropertyPhotoMapping()
            });

            Factory = DatabaseFactory.Config(x =>
            {
                x.UsingDatabase(() => new Database(databaseConfigurationSettings.ConnectionStringName));
                x.WithFluentConfig(fluentConfig);
            });
        }
        public void FluentConfigShouldBePlacedOnDatabaseWhenInsertedIntoFactoryConfig()
        {
            var db = new Database(new SqlConnection());
            var mapperCollection = new MapperCollection();
            var pocoDataFactory  = new FluentPocoDataFactory((y, f) => new PocoDataBuilder(y, mapperCollection).Init(), mapperCollection);
            var fluentConfig     = new FluentConfig(x => pocoDataFactory);

            var factory = DatabaseFactory.Config(x =>
            {
                x.UsingDatabase(() => db);
                x.WithFluentConfig(fluentConfig);
            });

            var database = factory.GetDatabase();

            Assert.AreEqual(fluentConfig.Config(null), database.PocoDataFactory);
        }
Example #30
0
        public void ValueObjectTestGetWithoutInterface()
        {
            var map = FluentMappingConfiguration.Scan(s =>
            {
                s.Assembly(typeof(User2).GetTypeInfo().Assembly);
                s.IncludeTypes(t => t == typeof(User2));
                s.Columns.ValueObjectColumnWhere(y => y.GetMemberInfoType() == typeof(MyNameObject2));
            });

            var factory = DatabaseFactory.Config(x =>
            {
                x.WithFluentConfig(map);
            });

            var s1 = factory.Build(Database).Single <User2>("select 'testtext' as Name /*poco_dual*/");

            Assert.AreEqual("testtext", s1.Name.MyAwesomeValue);
        }
Example #31
0
        public void SetUp()
        {
            var types     = new[] { typeof(User), typeof(ExtraUserInfo) };
            var dbFactory = new DatabaseFactory();

            dbFactory.Config().WithFluentConfig(
                FluentMappingConfiguration.Scan(s =>
            {
                s.Assembly(typeof(User).Assembly);
                s.IncludeTypes(types.Contains);
                s.WithSmartConventions();
                s.OverrideMappingsWith(new FluentMappingOverrides());
            })
                );

            var testDBType = Convert.ToInt32(ConfigurationManager.AppSettings["TestDBType"]);

            switch (testDBType)
            {
            case 1:     // SQLite In-Memory
                TestDatabase = new InMemoryDatabase();
                Database     = dbFactory.Build(new Database(TestDatabase.Connection));
                break;

            case 2:     // SQL Local DB
            case 3:     // SQL Server
                TestDatabase = new SQLLocalDatabase();
                Database     = dbFactory.Build(new Database(TestDatabase.Connection, new SqlServer2008DatabaseType()));
                break;

            case 4:
            case 5:
            case 6:
                Assert.Fail("Database platform not supported for unit testing");
                return;

            default:
                Assert.Fail("Unknown database platform specified");
                return;
            }

            InsertData();
        }
Example #32
0
        public void DifferentFactoriesWithDifferentMappingsGetDifferentPocoDataForSamePocoWithoutFactory()
        {
            // Assume these factories connect to different data sources
            var db1 = new Database(new SqlConnection())
            {
                PocoDataFactory = new PocoDataFactory(new MapperCollection())
            };
            var factory2 = DatabaseFactory.Config(x =>
            {
                x.UsingDatabase(() => new Database(new SqlConnection()));
                x.WithFluentConfig(FluentMappingConfiguration.Configure(new AnotherMapping()));
            });

            var pocoData1 = db1.PocoDataFactory.ForType(typeof(WanderingPoco));
            var pocoData2 = factory2.GetDatabase().PocoDataFactory.ForType(typeof(WanderingPoco));

            Assert.AreEqual("Table1", pocoData1.TableInfo.TableName);
            Assert.AreEqual("Table2", pocoData2.TableInfo.TableName);
        }
Example #33
0
        public void SetUp()
        {
            var types = new[] { typeof(User), typeof(ExtraUserInfo) };
            var dbFactory = new DatabaseFactory();
            dbFactory.Config().WithFluentConfig(
                FluentMappingConfiguration.Scan(s =>
                {
                    s.Assembly(typeof (User).Assembly);
                    s.IncludeTypes(types.Contains);
                    s.WithSmartConventions();
                    s.OverrideMappingsWith(new FluentMappingOverrides());
                })
            );

            var testDBType = Convert.ToInt32(ConfigurationManager.AppSettings["TestDBType"]);
            switch (testDBType)
            {
                case 1: // SQLite In-Memory
                    TestDatabase = new InMemoryDatabase();
                    Database = dbFactory.Build(new Database(TestDatabase.Connection));
                    break;

                case 2: // SQL Local DB
                case 3: // SQL Server
                    TestDatabase = new SQLLocalDatabase();
                    Database = dbFactory.Build(new Database(TestDatabase.Connection, new SqlServer2012DatabaseType()));
                    break;

                case 4:
                case 5:
                case 6:
                    Assert.Fail("Database platform not supported for unit testing");
                    return;

                default:
                    Assert.Fail("Unknown database platform specified");
                    return;
            }

            InsertData();
        }
Example #34
0
        public void SetUp()
        {
            var types = new[] { typeof(User), typeof(ExtraUserInfo), typeof(UserWithExtraInfo), typeof(Usersss), typeof(House), typeof(Supervisor) };
            var dbFactory = new DatabaseFactory();
            var config = FluentMappingConfiguration.Scan(s =>
            {
                s.Assembly(typeof(User).GetTypeInfo().Assembly);
                s.IncludeTypes(types.Contains);
                s.PrimaryKeysNamed(y => ToLowerIf(y.Name + "Id", false));
                s.TablesNamed(y => ToLowerIf(Inflector.MakePlural(y.Name), false));
                s.Columns.Named(x => ToLowerIf(x.Name, false));
                s.Columns.ForceDateTimesToUtcWhere(x => x.GetMemberInfoType() == typeof(DateTime) || x.GetMemberInfoType() == typeof(DateTime?));
                s.Columns.ResultWhere(y => ColumnInfo.FromMemberInfo(y).ResultColumn);
                s.OverrideMappingsWith(new FluentMappingOverrides());
                s.OverrideMappingsWith(new OneToManyMappings());
            });
            dbFactory.Config().WithFluentConfig(config);

            var configuration = new ConfigurationBuilder()
                .AddJsonFile("config.json")
                .Build();

            var testDBType = Convert.ToInt32(configuration.GetSection("TestDBType").Value);
            switch (testDBType)
            {
                case 1: // SQLite In-Memory
                    TestDatabase = new InMemoryDatabase();
                    Database = dbFactory.Build(new Database(TestDatabase.Connection));
                    break;

                case 2: // SQL Local DB
                case 3: // SQL Server
                    var dataSource = configuration.GetSection("TestDbDataSource").Value;
                    TestDatabase = new SQLLocalDatabase(dataSource);
                    Database = dbFactory.Build(new Database(TestDatabase.Connection, new SqlServer2008DatabaseType()));
                    break;

                case 4: // SQL CE
                case 5: // MySQL
                case 6: // Oracle
                case 7: // Postgres
                    Assert.Fail("Database platform not supported for unit testing");
                    return;
            #if !DNXCORE50
                case 8: // Firebird
                    TestDatabase = new FirebirdDatabase();
                    var db = new Database(TestDatabase.Connection, new FirebirdDatabaseType());
                    db.Mappers.Insert(0, new FirebirdDefaultMapper());
                    Database = dbFactory.Build(db);
                    break;
            #endif

                default:
                    Assert.Fail("Unknown database platform specified");
                    return;
            }

            InsertData();
        }