henkmollema / Dapper-FluentMap

Provides a simple API to fluently map POCO properties to database columns when using Dapper.
MIT License
429 stars 88 forks source link

Combining Convention and EntityMap #77

Closed JonasHansen16 closed 1 year ago

JonasHansen16 commented 6 years ago

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.

henkmollema commented 6 years ago

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
JonasHansen16 commented 5 years ago

@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.

blai30 commented 3 years ago

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

henkmollema commented 1 year ago

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