Example #1
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);
            });
        }
        private static void MergeAttributeOverrides(Dictionary <Type, TypeDefinition> config)
        {
            foreach (KeyValuePair <Type, TypeDefinition> typeDefinition in config)
            {
                TableInfo tableInfo = TableInfo.FromPoco(typeDefinition.Key);
                typeDefinition.Value.TableName       = tableInfo.TableName;
                typeDefinition.Value.PrimaryKey      = tableInfo.PrimaryKey;
                typeDefinition.Value.SequenceName    = tableInfo.SequenceName;
                typeDefinition.Value.AutoIncrement   = tableInfo.AutoIncrement;
                typeDefinition.Value.UseOutputClause = tableInfo.UseOutputClause;

                foreach (KeyValuePair <string, ColumnDefinition> columnDefinition in typeDefinition.Value.ColumnConfiguration)
                {
                    ColumnInfo columnInfo = ColumnInfo.FromMemberInfo(columnDefinition.Value.MemberInfo);
                    columnDefinition.Value.DbColumnName       = columnInfo.ColumnName;
                    columnDefinition.Value.DbColumnAlias      = columnInfo.ColumnAlias;
                    columnDefinition.Value.DbColumnType       = columnInfo.ColumnType;
                    columnDefinition.Value.IgnoreColumn       = columnInfo.IgnoreColumn;
                    columnDefinition.Value.ResultColumn       = columnInfo.ResultColumn;
                    columnDefinition.Value.ComputedColumn     = columnInfo.ComputedColumn;
                    columnDefinition.Value.ComputedColumnType = columnInfo.ComputedColumnType;
                    columnDefinition.Value.VersionColumn      = columnInfo.VersionColumn;
                    columnDefinition.Value.VersionColumnType  = columnInfo.VersionColumnType;
                    columnDefinition.Value.ForceUtc           = columnInfo.ForceToUtc;
                    columnDefinition.Value.Serialized         = columnInfo.SerializedColumn;
                }
            }
        }
Example #3
0
        public void ColumnInfoShouldAggregateForceToUtcColumnValues()
        {
            var memberInfo = ColumnInfo.FromMemberInfo(typeof(Test1).GetMember("ForceToUtc")[0]);

            Assert.AreEqual("ForceToUtc", memberInfo.ColumnName);
            Assert.AreEqual(false, memberInfo.ForceToUtc);
        }
Example #4
0
        public void ColumnInfoShouldGetNameFromFirstAttributeWhereNotNull()
        {
            var memberInfo = ColumnInfo.FromMemberInfo(typeof(Test1).GetMember("Result")[0]);

            Assert.AreEqual("MyResult", memberInfo.ColumnName);
            Assert.AreEqual(true, memberInfo.ForceToUtc);
        }
Example #5
0
        public bool MapMemberToColumn(MemberInfo mi, ref string columnName, ref bool resultColumn)
        {
            var ci = ColumnInfo.FromMemberInfo(mi);

            //Column Name
            columnName = Util.GetAttributeValue <ColumnNameAttribute, string>(mi, "ColumnName", ci.ColumnName);

            return(true);
        }
Example #6
0
        public void ColumnInfoShouldPreferComputednColumnFirst()
        {
            var memberInfo = ColumnInfo.FromMemberInfo(typeof(Test1).GetMember("ComputedWins")[0]);

            Assert.AreEqual("ComputedWins", memberInfo.ColumnName);
            Assert.AreEqual(false, memberInfo.ResultColumn);
            Assert.AreEqual(true, memberInfo.ComputedColumn);
            Assert.AreEqual(false, memberInfo.VersionColumn);
        }
Example #7
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 #8
0
        public void FluentMappingOverridesShouldOverrideComplexMappingPrefix()
        {
            var map = FluentMappingConfiguration.Scan(s =>
            {
                s.Assembly(typeof(User).GetTypeInfo().Assembly);
                s.IncludeTypes(t => t == typeof(User));
                s.Columns.ComplexPropertiesWhere(y => ColumnInfo.FromMemberInfo(y).ComplexMapping);
                s.OverrideMappingsWith(new FluentMappingOverrides());
            });

            var pd = map.Config(new MapperCollection()).Resolver(typeof(User), new PocoDataFactory(new MapperCollection())).Build();

            Assert.AreEqual(true, pd.Columns.ContainsKey("CM__Street"));
        }
Example #9
0
        public static ColumnMigratorInfo FromMemberInfo(MemberInfo member)
        {
            ColumnInfo columnInfo = ColumnInfo.FromMemberInfo(member);

            if (columnInfo.IgnoreColumn || columnInfo.ResultColumn)
            {
                return(null);
            }

            var type       = DbTypeAttribute.FromMember(member);
            var isNullable = NullableAttribute.FromMember(member);

            return(new ColumnMigratorInfo(columnInfo.ColumnName, type.Type)
            {
                DbTypeParameter = type.TypeParameter,
                AllowNull = isNullable
            });
        }
Example #10
0
        public void SetUp()
        {
            var types     = new[] { typeof(User), typeof(ExtraUserInfo), typeof(UserWithExtraInfo), typeof(Usersss), typeof(House), typeof(Supervisor) };
            var dbFactory = new DatabaseFactory();
            //DatabaseFactory.ColumnSerializer = new JsonNetColumnSerializer();
            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();
        }
Example #11
0
        public void ColumnInfoShouldInheritTheBaseClassPropertyAttributes()
        {
            var memberInfo = ColumnInfo.FromMemberInfo(typeof(OverrideTest1).GetMember("Id")[0]);

            Assert.AreEqual("TestId", memberInfo.ColumnName);
        }