Dapper Column number rather than column name?

.net dapper

Question

I have a very similar question to Dapper-dot-net "no column name", but the answer there is not getting me where I need.

I'm writing a web interface and using dapper to get data from Stored Procedures from my client's ERP system. The SP returns 4 columns of data without column names. That being said the SP's are locked and I can't change them. I've tried to work around this by using a temp table in my query as Sam suggested.

var grid = QueryMultiple(@"set nocount on 
declare @t table(Id int, Name nvarchar(max), AnotherId int)

insert @t
exec proc

set nocount off 
select Id, Name from @t
select Id, AnotherId from @t
");

However, I've now discovered the original SP also contains an insert for logging and therefore SQL will not allow me to insert my sp into a temp table because of this.

There is mention of adding support for:

class Foo { [ColumnNumber(1)] public string Name {get;set;} }

How can I do this? Can someone point me in the right direction to modify Dapper source to not require column names and allow me to map by column number?

Accepted Answer

The issue of non-trivial bindings keeps coming up; it came up most recently here: http://code.google.com/p/dapper-dot-net/issues/detail?id=108

My suggestion there stands, although I haven't had time to code it yet. I propose a static event that you can choose to handle, and apply whatever esoteric mappings you want, i.e.

SqlMapper.ColumnBind += (sender, args) {
    int colIndex = args.ColumnIndex;
    Type type = args.TargetType;
    MemberInfo member = // ... Entirely up to you to define logic
    args.Member = member;
};

The above is entirely theoretical, but would it meet your needs?

Note: if ColumnBind was not subscribed, it would just do normal basic name mapping as normal. Thoughts?

(note: it could also be a single event invoke per type, rather than per-column; that might be more efficient, as the subscriber will call GetProperties etc less often)



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why