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

萝らか妹 提交于 2020-01-14 07:49:12

问题


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.



来源:https://stackoverflow.com/questions/35634434/does-dapper-support-c-sharp-6-read-only-properties-in-pocos

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!