Skip to content
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

Add C# support #373

Open
eikster-dk opened this issue Mar 4, 2020 · 10 comments
Open

Add C# support #373

eikster-dk opened this issue Mar 4, 2020 · 10 comments
Labels
enhancement New feature or request new/codegen

Comments

@eikster-dk
Copy link

First of all, I'm a huge fan of sqlc and I use it every single personal go project where I need database support. Good work 🙏

I noticed that you have started adding support for other languages incl. typescript and kotlin. In my day to day work I write a lot of code in C# which use postgres database.

Would you consider adding support for C# based apps?

I might be able to help but I'm still learning Go so I might need some support in how to get started if you want help with this

@kyleconroy kyleconroy added enhancement New feature or request future In the year 3000... labels Mar 4, 2020
@kyleconroy
Copy link
Collaborator

Would you consider adding support for C# based apps?

I would. The main issue that I don't know what good C# support would look like. Kotlin support was added by a contributor, so I see no reason by C# couldn't be added the same way.

The first step would be to gather some more information. What does your current setup look like? What code are you writing by hand? What libraries are you using?

@eikster-dk
Copy link
Author

Currently the following libraries are used when I work with postgres and c#:

My setup is to hand write the C# classes and the sql, then dapper (micro orm) providing the mapping from sql to my C# POCO class with the help of reflection. The thing is that I need to manually keep track that my queries and the c# class needs to match - this can be a bit error prone at times.

Let me try and come up with an example of how the auto generated code could look like in the next couple of days.

@kamil-mrzyglod
Copy link

Hey,

Just recently found that library and it looks really cool, especially when its use is considered for projects already managing DB entities with rather low-level ORMs like aforementioned Dapper.

@kyleconroy - is C# support is still something you'd still actively consider? There're in fact three main approaches when building an app in C# with DB communication in mind:

  • pure ADO.NET
  • Dapper as lightweight overlay for ADO.NET
  • Entity Framework which is in fact much heavier overlay for ADO.NET allowing for querying a DB with C# code instead of SQL queries

Dapper is a DB-agnostic ORM meaning it supports many (if not all) .NET ADO providers being an interesting safe choice. Pure ADO.NET may be closer to your original approach for sqlc, but is not a first choice for most of the projects (in terms of popularity Entity Framework is probably something used for most of business projects with complex domains).

I'd say the best option would be to get started with Dapper (as it abstracts many lower-level concepts from ADO.NET) and give it a try as the simplest option.

@d47id
Copy link

d47id commented Sep 14, 2022

Personally I think pure ADO.NET with Npgsql is the right way to go. Dapper, IMO, adds an unnecessary layer of reflection-based object field mapping.

The point of sqlc is to generate simple, performant, type-safe code to access queries and query results. It doesn't really matter if that code ends up being verbose compared to what you can get with Dapper.

I also think there's a strong argument to be made for avoiding another dependency.

@Jlll1
Copy link

Jlll1 commented Sep 16, 2022

C# support would also mean the need for supporting SQL Server, since that's what the majority of C# projects are running.

@kyleconroy
Copy link
Collaborator

kyleconroy commented Nov 9, 2022

Codegen plugins are now stable enough. Both Python and Kotlin support has been migrated to plugins. This means that if someone would like to try their hand at C# support, you aren't blocked getting your changes into the sqlc codebase.

@kyleconroy kyleconroy added accepted and removed future In the year 3000... labels Nov 9, 2022
@nickpreston24
Copy link

Hi there.

The easy way to get C# support is this magic library. https://github.com/jonwagner/Insight.Database

I'm trialing it and it's super-easy to set up C# DTOs and interfaces and run existing stored procedures or inline SQL.

It's like Dapper, but on steriods. No EF Context 💩. No need to fill in the gaps for ADO.NET. Just pure bliss as advertised (at least, so far that's been my experience).

@SockworkOrange
Copy link
Contributor

@doron050 and myself are starting work on C# support in here :)

@doron050
Copy link

doron050 commented May 18, 2024

For anyone interested in SQLC for C#
@SockworkOrange and I have been developing a plugin for C# as said above.
Now we have a stable mature 1st version (v0.10.0) with support for MySQL and Postgres as well as all available .NET target frameworks, take a crack at it and we'd love feedback if you have any issues, PRs, just go for it 🎉

TLDR

version: "2"
plugins:
- name: csharp
  wasm:
    url: https://github.com/DionyOSS/sqlc-gen-csharp/releases/download/v0.10.0/sqlc-gen-csharp_0.10.0.wasm
    sha256: 613ae249a541ab95c97b362bd1b0b572970edcad5eb2a11806a52d3f95e0f65f
sql:
  # PostgreSQL Example
  - schema: "examples/authors/postgresql/schema.sql"
    queries: "examples/authors/postgresql/query.sql"
    engine: "postgresql"
    codegen:
      - plugin: csharp
        out: NpgsqlExample
        options:
          driver: Npgsql
          targetFramework: net8.0
          generateCsproj: true
          filePerQuery: false
  # MySQL Example
  - schema: "examples/authors/mysql/schema.sql"
    queries: "examples/authors/mysql/query.sql"
    engine: "mysql"
    codegen:
      - plugin: csharp
        out: MySqlConnectorExample
        options:
          driver: MySqlConnector

For further information refer to https://github.com/DaredevilOSS/sqlc-gen-csharp

@SockworkOrange
Copy link
Contributor

@kyleconroy can be closed then:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request new/codegen
Projects
None yet
Development

No branches or pull requests

8 participants