Answered by:
Trying to move away from datasets/tables - to objects

Question
-
User-1482891610 posted
Hi
I have been developing Web Forms for many years using DataSets/Tables. I am trying to move away from DataSets/Tables as they dont seem to work very well with WCF and Blazor (which im trying to learn). But i love the simplicity and flexability of DatatSets/Tables. Often I would just return my SQL tables structure as an the datatset and datatable. However it seems sending back a typed business object is the preffered method, however I have to create lots of classes and write lots of of additional code.
So my question is, is there an easy method, tool, nuget package etc... I can install thatr will return my DataSets and DataTables into objects?Many thanks in advance
Monday, June 22, 2020 9:42 AM
Answers
-
User475983607 posted
BigMeat
I have tried Strongly Typed datasets in the past. But it seems that typed Datasets do not work well with futire platforms, see below article:Services should pass simple types like strings and integers. This has always been a recommendation for building services in the .NET framework. It makes sense if you think about it. Services should return platform agnostic types so it can be used by an client.
What's actually happening is the WCF Web Service Reference provider is generating a List<XElement> inside of a custom ArrayOfXElement rather than a DataSet. I assume that's because the DataSet is not recommended and could be deprecated in the future. Anyway, the response is the schema and a diffset.
One option is opening the code generated reference.cs file and change the references from ArrayOfXElement to DataSet. This will work until the reference is refreshed.
Another option is converting the List<XElement> to a DataSet.
private async Task<DataSet> ConvertToDataSet() { ServiceClient wcfClient = new ServiceClient(); DataSetDemoResponse result = await wcfClient.DataSetDemoAsync(); ArrayOfXElement arrayOfXElement = result.DataSetDemoResult; StringBuilder sb = new StringBuilder(); sb.Append("<root>"); foreach(var x in arrayOfXElement.Nodes) { sb.Append(x.ToString()); } sb.Append("</root>"); byte[] bytes = Encoding.ASCII.GetBytes(sb.ToString()); MemoryStream ms = new MemoryStream(bytes); DataSet ds = new DataSet(); ds.ReadXml(ms); return ds; }
The best option is passing strong types.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, June 22, 2020 3:02 PM
All replies
-
User475983607 posted
So my question is, is there an easy method, tool, nuget package etc... I can install thatr will return my DataSets and DataTables into objects?Typed DataSet.
Monday, June 22, 2020 10:16 AM -
User-1482891610 posted
Hi
Thanks for the suggestion. I have tried Strongly Typed datasets in the past. But it seems that typed Datasets do not work well with futire platforms, see below article:https://github.com/dotnet/wcf/issues/3712
Does anybody have a routine that would convert my loosly typed datasset/table, based on the fields names and types. Or maybe a library, creating lots of gets and sets manually seems like a burden.
Many thanks in advanceMonday, June 22, 2020 11:23 AM -
User475983607 posted
BigMeat
I have tried Strongly Typed datasets in the past. But it seems that typed Datasets do not work well with futire platforms, see below article:Services should pass simple types like strings and integers. This has always been a recommendation for building services in the .NET framework. It makes sense if you think about it. Services should return platform agnostic types so it can be used by an client.
What's actually happening is the WCF Web Service Reference provider is generating a List<XElement> inside of a custom ArrayOfXElement rather than a DataSet. I assume that's because the DataSet is not recommended and could be deprecated in the future. Anyway, the response is the schema and a diffset.
One option is opening the code generated reference.cs file and change the references from ArrayOfXElement to DataSet. This will work until the reference is refreshed.
Another option is converting the List<XElement> to a DataSet.
private async Task<DataSet> ConvertToDataSet() { ServiceClient wcfClient = new ServiceClient(); DataSetDemoResponse result = await wcfClient.DataSetDemoAsync(); ArrayOfXElement arrayOfXElement = result.DataSetDemoResult; StringBuilder sb = new StringBuilder(); sb.Append("<root>"); foreach(var x in arrayOfXElement.Nodes) { sb.Append(x.ToString()); } sb.Append("</root>"); byte[] bytes = Encoding.ASCII.GetBytes(sb.ToString()); MemoryStream ms = new MemoryStream(bytes); DataSet ds = new DataSet(); ds.ReadXml(ms); return ds; }
The best option is passing strong types.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, June 22, 2020 3:02 PM -
User-1482891610 posted
Thanks ever so much for such a comprehensive response, really appreciate it
Your function worked a treat to convert the dataset. Will use this for the interim but aim to go down the typed route going forwardMany thanks again
Tuesday, June 23, 2020 1:57 PM -
User1120430333 posted
Your function worked a treat to convert the dataset. Will use this for the interim but aim to go down the typed route going forward
What is a DTO?
https://en.wikipedia.org/wiki/Data_transfer_object
https://www.codeproject.com/articles/1050468/data-transfer-object-design-pattern-in-csharp
Tuesday, June 23, 2020 3:12 PM