Code Monkey home page Code Monkey logo

Comments (4)

MarkPflug avatar MarkPflug commented on June 16, 2024 1

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.

MarkPflug avatar MarkPflug commented on June 16, 2024

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.

thestonehead avatar thestonehead commented on June 16, 2024

Fantastic! :)

from sylvan.

MarkPflug avatar MarkPflug commented on June 16, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.