Dapper-FluentMap icon indicating copy to clipboard operation
Dapper-FluentMap copied to clipboard

Combining Convention and EntityMap

Open JonasHansen16 opened this issue 6 years ago • 3 comments

First of all a quick thank you for all the work you've put into this project.

Currently i'm trying to combine conventions and an entitymap to map a poco to one of my db tables. The convention covers most of the database column names, however most of our primary keys have the same name. This obviously causes issues when working with joins. Is it possible to let my convention handle the mapping for certain columns and using an entitymap to handle the other columns my convention can't?

So far i haven't been able to make it work, it seems to either take one or the other.

JonasHansen16 avatar Oct 30 '18 16:10 JonasHansen16

Currently this does not work out of this box. You might try this:

Add this class:

public class CombinedTypeMap<TEntity> : MultiTypeMap
{
    public CombinedTypeMap() 
        : base(new FluentConventionTypeMap<TEntity>(), new FluentMapTypeMap<TEntity>())
    {
    }
}

Then register a Dapper type map for each of your entity (after you called FluentMapper.Initialize(...)):

SqlMapper.SetTypeMap(typeof(Foo), new CombinedTypeMap<Foo>());
// do this for all your entities

henkmollema avatar Nov 13 '18 22:11 henkmollema

@henkmollema Sorry for the late reply. unfortunately haven't been able to make it work.

this is the class i use

public class Test
    {
        //database field => n_id
        public int Id { get; set; }
        //database field => unconventional
        public string Text { get; set; }
        //database field => t_convention
        public string Convention { get; set; }
    }

My mappers

public class TypePrefixConvention : Convention
    {
        public TypePrefixConvention()
        {
            Properties<int>()
                .Configure(c => c.Transform(s => "N_" + Regex.Replace(input: s, pattern: @"(?ms-inx:((?<=.{1})(.{1})(?=[A-Z])))", replacement: @"$+_")).IsCaseInsensitive());
            Properties<string>()
                .Configure(c => c.Transform(s => "T_" + Regex.Replace(input: s, pattern: @"(?ms-inx:((?<=.{1})(.{1})(?=[A-Z])))", replacement: @"$+_")).IsCaseInsensitive());
        }
    }
public TestMap()
        {
            Map(x => x.Text)
                .ToColumn("unconventional", false);
        }

public class CombinedTypeMap<TEntity> : MultiTypeMap
    {
        public CombinedTypeMap()
            : base(new FluentConventionTypeMap<TEntity>(), new FluentMapTypeMap<TEntity>())
        {
        }
    }

Here is how i use it

FluentMapper.Initialize(config =>
            {
                config.AddConvention<TypePrefixConvention>()
                .ForEntity<Test>();
                config.AddMap(new TestMap());
            });
            SqlMapper.SetTypeMap(typeof(Test), new CombinedTypeMap<Test>());
            
            using (var connection = Factory.CreateDbConnection())
            {
                var query = "select * from perdb.dappertest";
                connection.Open();
                var test = connection.Query<Test>(query).ToList();
                foreach (var item in test)
                {
                    Console.WriteLine(item.Id);

                }
            }

it seems that only the convention is getting properly mapped and my explicit mapper doesn't seem to work.

JonasHansen16 avatar Dec 21 '18 13:12 JonasHansen16

Any update on this? I would love to define one Convention and zero entity mappers then have Dommel automatically map my PascalCase POCO properties to my snake_case MySQL columns/tables

blai30 avatar Feb 13 '21 22:02 blai30

I'm archiving this repository as I'm not using this library myself anymore and have no time maintaining it. Thanks for using it.

henkmollema avatar Apr 19 '23 13:04 henkmollema