Does Dapper support c# 6 read-only properties in POCOs?

c#-6.0 dapper

Question

Given the following:

public class SomePoco {
    public int IntValue { get; }
}

and

CREATE TABLE SomePocoStorage (IntValue INT NOT NULL)

and

INSERT SomePocoStorage VALUES (1), (274)

If I call

connection.Query<SomePoco>("SELECT * FROM SomePocoStorage")

does Dapper handle populating the IntValue field on the returned SomePoco instances?

Accepted Answer

Good question! It isn't a scenario I've targeted, but I'd be more than happy to take a look at what would be involved. Since we already do a lot of nasty reflection, this could still be viable. Probably better as a github issue, but I'll have a look.

Update - it does now (at the current time, via repo only - not deployed):

[Fact] // passes
public void GetOnlyProperties()
{
    var obj = connection.QuerySingle<HazGetOnly>(
        "select 42 as [Id], 'def' as [Name];");
    obj.Id.IsEqualTo(42);
    obj.Name.IsEqualTo("def");
}
class HazGetOnly
{
    public int Id { get; }
    public string Name { get; } = "abc";
}

Popular Answer

No because there's no way for Dapper to set the value of the property if that property only has a getter.



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