Skip to content

If a record has multiple constructors without an explicit constructor chosen, default to the primary constructor #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

alex-zissis
Copy link

The following program will fail with Error DAP036 : Type 'RecordWithMultipleConstructors' has more than 1 constructor; mark one constructor with [ExplicitConstructor] or reduce constructors (https://aot.dapperlib.dev/rules/DAP036)

using Dapper;
using Microsoft.Data.Sqlite;
[module:DapperAot]

await using SqliteConnection connection = GetDbConnection();
IEnumerable<RecordWithMultipleConstructors> results = await connection.QueryAsync<RecordWithMultipleConstructors>("SELECT A, B FROM Test");


public record RecordWithMultipleConstructors(int A, int? B)
{
    public RecordWithMultipleConstructors(int A) : this(A, 22) {}
}

However you can't add an attribute targeting Constructor/Method on the primary constructor of a record.

[ExplicitConstructor]
public record RecordWithMultipleConstructors(int A, int? B)
{
    public RecordWithMultipleConstructors(int A) : this(A, 22) {}
}

This will cause an error at build time Error CS0592 : Attribute 'ExplicitConstructor' is not valid on this declaration type. It is only valid on 'constructor, method' declarations.

To fix this, if the type is a record and has multiple implicit constructors - default to the primary constructor.

@alex-zissis alex-zissis changed the title If a record has multiple constructors without an explicit constructor chosen, default to the primary construcor If a record has multiple constructors without an explicit constructor chosen, default to the primary constructor Apr 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant