dotnet / SqlClient

Microsoft.Data.SqlClient provides database connectivity to SQL Server for .NET applications.
MIT License
839 stars 280 forks source link

Provide support for direct mapping to JSON string. #424

Open B-Esmaili opened 4 years ago

B-Esmaili commented 4 years ago

In some languages (like PHP for example) there is possibility to directly map query result into JSON string which makes a lot of sense when we don't need to manipulate the result returned from query execution which would result in better performance due to eliminating extra step of mapping to application models (and then mapping to JSON string later on).Is there any plan to add such a feature to SqlClient?

cheenamalhotra commented 4 years ago

Hi @B-Esmaili

We've never received this request before so as of now there are no plans to introduce this feature. Also the current conversion strategy for conversion is SqlDataReader >> DataTable >> JSON, and takes additional step, as you also mentioned.

Maybe something to consider in future, unless there's a heavy demand for it. Feel free to contribute a PR if you'd like to!

B-Esmaili commented 4 years ago

@cheenamalhotra thanks for your quick response , but i thinks we are already in JSON era and in evaluating features of a language JSON support is one of many important factors. Nevertheless I hope more developers would endorse mentioned requirement and move it to higher priority.

Wraith2 commented 4 years ago

This is a job normally done by several kinds of mappers and wrappers like EFCore or Drapper. While the same sort of functionality could be added into SqlClient there's nothing it can do that those projects (and others) don't already do. Why would it be a good idea to add the functionality at this level?

B-Esmaili commented 4 years ago

@Wraith2 could you please point me to a link containing sample code or doc for either Dapper or EFCore?Although i am not interested in EFCore but i am really curious about thing i've missed for so many years!

Wraith2 commented 4 years ago

Looking at the scenario you described you probably want something like this https://github.com/AutoMapper/AutoMapper.Data which isn't an orm just a mapper.

B-Esmaili commented 4 years ago

I couldn't find anything related to mapping to JSON in the provided link.

Wraith2 commented 4 years ago

No, that would be the job of a serialization library. Can you give a code example of what you're trying to do? because it sounds bonkers to me.

Georgy-Losenkov commented 4 years ago

@B-Esmaili, Why don't you want to perform conversion to JSON using T-SQL? FOR JSON clause is available starting SQL SERVER 2016 https://docs.microsoft.com/en-us/sql/relational-databases/json/format-query-results-as-json-with-for-json-sql-server?view=sql-server-ver15

Wraith2 commented 4 years ago

I'd still like an example use case or two to clarify exactly what functionality is desiredhere.

Json support in sql server returns a string and then to do anything with that string you're going to have to parse it. So you send a query to the engine which does a query then all the work of composing the results it into a string so you can receive it and then parse the json apart. I'd cut out the string format in the middle if possible and work with the data. It just seems very wasteful to me.

If you're going to do things like sending query results straight to or from apps into the sql server I don't think allowing you to bypass the word of validating the data is a good idea.

Georgy-Losenkov commented 4 years ago

@Wraith2, I completely agree with you. I am sorry that I have not addressed question to correct person.