public PgConnectionFactory() {
   Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;
   SimpleCRUD.SetDialect(SimpleCRUD.Dialect.PostgreSQL);
   var resolver = new CustomResolver();
   SimpleCRUD.SetTableNameResolver(resolver);
   SimpleCRUD.SetColumnNameResolver(resolver);
 }
        static RepositoryFactory()
        {
            var mappings = TypeColumnMappings.Create();

            foreach (var mapping in mappings)
            {
                Dapper.SqlMapper.SetTypeMap(
                    mapping.Type,
                    new CustomPropertyTypeMap(
                        mapping.Type,
                        (type, columnName) =>
                {
                    if (mapping.FieldNameByColumnName.ContainsKey(columnName))
                    {
                        string fieldName = mapping.FieldNameByColumnName[columnName];

                        var properties = type.GetProperties();

                        return(properties.FirstOrDefault(item => item.Name == fieldName));
                    }

                    return(null);
                }));
            }

            var resolver = new CustomSimpleCrudResolver(mappings);

            SimpleCRUD.SetTableNameResolver(resolver);
            SimpleCRUD.SetColumnNameResolver(resolver);
            SimpleCRUD.SetDialect(SimpleCRUD.Dialect.MySQL);
        }
Example #3
0
        public void TestLowerCaseNames()
        {
            var mapper = new SimpleCRUD.CachingNameResolver(new SimpleCRUD.LowercaseFormatter());

            SimpleCRUD.SetColumnNameResolver(mapper);
            SimpleCRUD.SetTableNameResolver(mapper);

            int userId;

            using (var c = GetOpenConnection())
            {
                var user = new User2 {
                    FirstName = "Jane", LastName = "Doe", ScheduledDayOff = DayOfWeek.Monday
                };
                c.Insert(user);
                userId = user.Id;
                var post = new Post2 {
                    Text = "My first post", Author = user
                };
                c.Insert(post);
            }

            using (var c2 = GetOpenConnection())
            {
                var user = c2.Get <User2>(userId);
                Assert.IsNotNull(user);
                user.FirstName.IsEqualTo("Jane");
                user.LastName.IsEqualTo("Doe");
                Assert.IsEqualTo((DayOfWeek)user.ScheduledDayOff, DayOfWeek.Monday);

                var posts = c2.MultiQuery <Post2, User2, Post2>("select p.* ||| u.* " +
                                                                "from post2 p, user2 u " +
                                                                "where p.author_id = u.id", (p, u) =>
                {
                    p.Author = u;
                    return(p);
                });
                Assert.IsTrue(posts.Any());
            }
        }