Insert<TTEntity>(TEntity entity) should use GetType
Opened this issue · 2 comments
Hi, I answered this question today
https://stackoverflow.com/questions/55398454/how-to-instanciate-an-object-by-activator-createinstance-an-inserting-it-on-a-ta/55398570#55398570
He used object as generic argument. Which broke dapper. The reason why is becasue you do var type = typeof(T). The correct way would instead be var type = entity.GetType();
Sounds legit, although I wonder if another approach might be to add a non-generic overload that accepts object
, or only use .GetType()
if typeof(T)==typeof(object)
. My thought on using .GetType()
in the general case is that it might cause unforeseen side-effects, in particular when a sub-type is currently being passed in, but where it is currently working as the <T>
specified, not the concrete type. So from that perspective, using .GetType()
in the general case could introduce a break.
I cant see a legit use case when you would do
class B : A {}
Insert<A>(instaceOfB);
But sure a overload works too.