U4-11513 - Add a "Select" extension method on "Sql" to avoid hardcoded field

Created by Sébastien Sougnez 13 Jul 2018, 08:41:20 Updated by Sebastiaan Janssen 15 Jul 2018, 05:53:26

Tags: Up For Grabs PR

Hi,

this is not really an issue but more a proposal (if it's not the right place to do so, please tell me where to do it).

PetaPoco is really nice to work with, however, I feel that having to specify the name of the columns to select as string is error prone and a bit of a shame when the rest of the API uses delegate. Therefore, I quickly created a method (for now, it's heavily using reflection as it was just a test) used to select fields based on delegates. Basically, this can be used like this:

var cmd = new Sql().Select<NewsletterSubscription>(dbc.SqlSyntax, x => x.Id, x => x.Email);

Of course, some override could be created to support multi table, such as:

var cmd = new Sql().Select<T1, T2>(dbc.SqlSyntax, x => x.Field1, x => x.Field2, y => y.Field1);

I'm going to use it for my project but I wondered if it was something that could be useful to more people than just me. If so, I could create a PR to add this to Umbraco.Core?

What do you guys think?

Comments

Sebastiaan Janssen 13 Jul 2018, 08:59:23

@ssougnez Sure, that's a great idea! We're using this in v8 already and maybe you can get some inspiration from the extension method there: https://github.com/umbraco/Umbraco-CMS/blob/temp8/src/Umbraco.Core/Persistence/NPocoSqlExtensions.cs#L666

We've switched to NPoco for v8, so it's not exactly the same but it might help.

Look forward to seeing a PR!


Sébastien Sougnez 13 Jul 2018, 09:04:51

@sebastiaan Cool, then I'll create a PR soon :-D


Sébastien Sougnez 13 Jul 2018, 13:23:08

You wanted it, you got it :-D

https://github.com/umbraco/Umbraco-CMS/pull/2770


Priority: Normal

Type: Bug

State: Fixed

Assignee:

Difficulty: Normal

Category:

Backwards Compatible: True

Fix Submitted:

Affected versions:

Due in version: 7.12.0

Sprint:

Story Points:

Cycle: