Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#-6.0

dapper

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?

like image 811
Matt Mills Avatar asked Feb 25 '16 17:02

Matt Mills


People also ask

What databases does Dapper support?

Dapper has no DB specific implementation details, it works across all . NET ADO providers including SQLite, SQL CE, Firebird, Oracle, MySQL, PostgreSQL and SQL Server. Dapper was created by team at Stack Overflow. To utilize Dapper, we add the package reference to the project with the dotnet tool.

What does Dapper do C#?

Dapper is a micro ORM or it is a simple object mapper framework which helps to map the native query output to a domain class or a C# class. It is a high performance data access system built by StackOverflow team and released as open source.

Does Dapper support MySQL?

A dapper extension library. Support MySQL,SQL Server,PostgreSQL,SQLite and ODBC, Support cache. Learn more about Target Frameworks and . NET Standard.

Can I use Dapper in .NET core?

Dapper is a NuGet library, can be used with any . NET project. Quite lightweight, high performance. Drastically reduces the database access code.


2 Answers

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";
}
like image 127
Marc Gravell Avatar answered Sep 20 '22 08:09

Marc Gravell


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

like image 27
RiceRiceBaby Avatar answered Sep 22 '22 08:09

RiceRiceBaby