Comments (4)
Thanks for bringing this issue to my attention. I just pushed https://www.nuget.org/packages/Sylvan.Data/0.2.3 to fix both the record class
binder issue, and added the class constraint.
from sylvan.
Interesting. I've yet to use the record class/struct feature of C#. It appears the issue is that there is a compiler generated property Type EqualityContract
on the record class that the binder wants to bind a value to. The property doesn't have a setter though, so that is definitely a bug that needs to be fixed.
You can work around this pretty easily. The GetRecords<T>
is essentially just a helper extension method that makes the binder easy to use providing the default settings. You can implement your own extension (or use the binder directly) to have more control over the binder behavior. Specifically, there is a property on the binder named BindingMode
that controls whether all columns/properties must be bound. By default, it uses AllProperties
mode, meaning that all properties on the target type need to have a value bound to them from the source data. You can change this to use AllColumn
to require that all columns are bound to a property, or Any
which is the most lenient, and doesn't really validate the columns/properties.
Here is what such an extension would look like:
static class DataExtensions
{
public static IEnumerable<T> GetRecords<T>(this DbDataReader reader)
where T : class, new()
{
var opts = new DataBinderOptions { BindingMode = DataBindingMode.AllColumns };
var binder = DataBinder.Create<T>(reader, opts);
while (reader.Read())
{
var item = new T();
binder.Bind(reader, item);
yield return item;
}
}
}
You would use it exactly like the GetRecords
in Sylvan.Data.
You mentioned record struct
working correctly. Unfortunatley, that is another bug I need to fix. Binding to structs (or record structs) doesn't work, and the values on the struct will all end up with their default value. This was meant to be an unsupported scenario, but apparently I forgot to add the constraints to prevent it. The class
constraint in the code above is missing in the Sylvan.Data code.
from sylvan.
Fantastic! :)
from sylvan.
If you've found the library to be useful, please give the repository a star. Helps other people discover it.
from sylvan.
Related Issues (20)
- As is. The Object to CSV Writer example doesn't compile HOT 1
- Clone a CsvDataReader HOT 1
- CsvDataWriter to append to a file HOT 1
- Configuration adjustments for CSV parsing HOT 1
- Records Count and Max Length of each Column HOT 1
- A 19kb record throws CsvRecordTooLargeException HOT 3
- Fields ending in escaped character return the wrong character HOT 1
- Minor unescape issue? HOT 2
- CompiledDataBinder: "Body of catch must have the same type as body of try" exception HOT 7
- Bulk Load CSV data into Postgres? HOT 1
- DbDataReader reads all rows as empty from IEnumerable<object> HOT 5
- The ObjectDataReader should use DataMember attribute to allow column name mapping HOT 1
- Encoding problem HOT 3
- DataTable Columns and Rows HOT 3
- Reading many csv files, checking the length of fields and merging them all into one file HOT 3
- CsvWriter to output to a .gz file HOT 1
- Invalid Examples for CSV Data Binding HOT 2
- CSV: RowNumber and line breaks inside fields HOT 1
- nullables issue, how to get the property names, and type info HOT 1
- Query: Nullable boolean CsvDataWriterOptions FalseString HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sylvan.