question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Easier way configure conversion for all properties of readonly struct type

See original GitHub issue

I have a readonly struct type and I’m trying to configure my model in such a way that all properties of that type (nullable or not) get the same value converter:

public sealed class MyContext : DbContext
{
    public MyContext(DbContextOptions options)
        : base(options)
    {
    }

    protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
    {
        configurationBuilder
            .Properties<SimpleStruct>()
            .HaveConversion<Conv, Comp>();

        configurationBuilder
            .Properties<SimpleStruct?>()
            .HaveConversion<Conv, Comp2>();
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        // Model stuff
    }
}

public readonly struct SimpleStruct
{
    public readonly int X;
    
    public SimpleStruct(int x)
    {
        X = x;
    }
}

internal sealed class Conv : ValueConverter<SimpleStruct, int>
{
    public Conv()
        : base(v => v.X, x => new SimpleStruct(x), null)
    {
    }
}

internal sealed class Comp : ValueComparer<SimpleStruct>
{
    public Comp()
        : base(false)
    {
    }
}

internal sealed class Comp2 : ValueComparer<SimpleStruct?>
{
    public Comp2()
        : base(false)
    {
    }
}

This is a lot of code for something so simple. I tried to simplify this, for example by using one ValueComparer, but that results in runtime exceptions. It would be nice if this could be done in an easier way.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ajcvickerscommented, Sep 6, 2021

@RdJNL Also, you should not need a custom value comparer for a read-only struct like that. See Simple immutable structs.

1reaction
rojicommented, Sep 6, 2021

@RdJNL this should work automatically for nullable properties in the latest bits. Can you give the latest daily builds a try and report back on the results?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Structure types - C# reference
You use the readonly modifier to declare that a structure type is immutable. All data members of a readonly struct must be read-only...
Read more >
Does using public readonly fields for immutable structs work?
Making all the fields readonly is a great way to help (1) document that the struct is immutable, and (2) prevent accidental mutations....
Read more >
ReadOnly Structs in C# 8 with Examples
The readonly keyword is a C# modifier that is used to limit access to all the data members of a struct. If the...
Read more >
c# - Best Practices Returning Read-Only Object
I have "best practices" question about OOP in C# (but it sort-of applies to all languages). Consider having library class with object that...
Read more >
Allow init-only auto-properties in readonly structs · Issue ...
I propose that C# relax the restriction around the types of auto-properties supported on readonly structs to allow init-only auto-properties.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found