Easier way configure conversion for all properties of readonly struct type
See original GitHub issueI 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:
- Created 2 years ago
- Comments:9 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@RdJNL Also, you should not need a custom value comparer for a read-only struct like that. See Simple immutable structs.
@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?