Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let's make the type mapper more pluggable. #385

Open
NickCraver opened this issue Nov 6, 2015 · 7 comments
Open

Let's make the type mapper more pluggable. #385

NickCraver opened this issue Nov 6, 2015 · 7 comments

Comments

@NickCraver
Copy link
Member

I'm seeing a big picture of a lot of issues solved in a single way here. People want to slightly adjust how the type mapping works in various ways - and I think we can do a better job of allowing this.

Here are some examples:

This one is a more questionable example (as in questionable if this would help):

There may be a few I missed, but you get the idea. You can replace the DefaultTypeMap since #110 was merged a few weeks ago. However, we can do better here by allowing DefaultTypeMap to be plugged a bit rather than clipboard inherited all over. We'd also need to look at what functionality could move down into it to be pluggable (such as column names).

I'm not arguing for this to all be in core - I honestly think it'd be a bad idea if all of it was. I think making it pluggable makes some of the odder cases, if common, appear as mappers you could easily plug in from contrib. For example, what if #376 could be fixed by:

SqlMapper.TypeMapProvider = t => Dapper.Contrib.DataMemberMapper;

and #350 by:

SqlMapper.TypeMapProvider = t => MyCustomMultiMapper;

Posting this here for thoughts.

@henkmollema
Copy link
Contributor

I created a utility to map POCO properties to database column names using a fluent API. It uses the CustomPropertyTypeMap: https://github.com/henkmollema/Dapper-FluentMap (and specifically FluentTypeMap).

To accomplish this I have to add each type map using SqlMapper.SetTypeMap(), which is a bit clumsy. Perhaps creating and configuring custom type maps could more 'pluggable' as you call it.

@megakid
Copy link

megakid commented Nov 13, 2015

Agree with this. I wanted to mark all DateTime coming via Dapper as DateTimeKind.Utc as the MySql.Data ADO Connector marks them as DateTimeKind.Unspecified. This proved virtually impossible and had to do it as a post step by reflecting over all properties and marking DateTime properties as UTC.

@mperrenoud
Copy link

@NickCraver I have seen a lot of discussion surrounding the idea of bringing in the Column attribute. Did this happen by chance? Right now I have a table I'm trying to work with named BillingLocation, but the creator of the table added a column named BillingLocation as well.

Ideally I can just change the name of the property to Name and then use the Column attribute to set its field behind being BillingLocation.

@mitchcapper
Copy link

@mperrenoud no this is not yet in code this issue is tracking a more dynamic way to do column name mapping. I did find this feature useful however so you can find the latest dapper with the [Column] support that was created by @mjashanks here: https://github.com/mitchcapper/dapper-dot-net/tree/column_attribute_support .

In addition, when this does finally get implemented I would recommend adding some helper methods for making non-standard crud operations easier. I wrote this code on top of @mjashanks updated code:
mitchcapper@477e58a?w=1

which then allows for things like:
db.ExecuteScalarAsync<string>("SELECT COUNT(*) FROM " + db.GetTableName<MyTable>() + " WHERE test=1");
or:
await db.QueryFirstOrDefaultAsync<Message>(db.GetSelectSql<MyTable>() + " WHERE test=1 ORDER BY sortable LIMIT 1");

@phillijw
Copy link

BINARY(16) to Guid mapping would be helpful for mysql/mariadb (unless I'm missing some other way of doing this currently)

@cdonnellytx
Copy link
Contributor

@phillijw A similar mapping for Oracle (RAW(16) to Guid) was tried in #637, but in the end it was backed out:

Reverted; sorry, closing this down since we can't support it natively in the lib due to the incompatibility between backends

Unfortunately I didn't find any further details on why this was backed out. (I speculate it would break compatibility with drivers that support Guids natively, like SQL Server, but I don't fully understand the reasons.)

@christophano
Copy link

I would love to see support for #350, but I'm not sure it's possible via the type map, without a lot of changes.
I know your previous objections to including this in the base library are related to the "magical" nature of automatic conventions, particularly where they could affect previously working queries, but is it something you would consider if it was disabled by default and, of course, didn't have a significant impact on performance?

@NickCraver NickCraver added the area:mapping [Column] and friends label May 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants