Does Dapper support c# 6 read-only properties in P

2019-06-20 08:38发布

问题:

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?

回答1:

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";
}


回答2:

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



标签: dapper c#-6.0