The title says it. (to be clear, SQLite.NET is hosted here)
All the examples work with mutable record types, that means they have { get; set; }
in each property definition. I want to get rid of mutable types where possible in my project, but SQLite.NET looks like a possible obstacle.
If I try to get a query result with 1 column of type System.String
, it fails, for example.
So is it possible to make SQLite.NET use constructors instead of property setters, or I have to use mutable types for retrieving and storing data in SQLite.NET?
Because a record type is immutable, it is thread-safe and cannot mutate or change after it has been created. You can initialize a record type only inside a constructor. You can declare a record using the record keyword as shown in the code snippet below.
A record is a reference type and follows value-based equality semantics. You can define a record struct to create a record that is a value type. To enforce value semantics, the compiler generates several methods for your record type (both for record class types and record struct types):
Beginning with C# 9, you use the record keyword to define a reference type that provides built-in functionality for encapsulating data. C# 10 allows the record class syntax as a synonym to clarify a reference type, and record struct to define a value type with similar functionality.
Record type or record is a very interesting feature introduced in C# 9.0. As we know in F#, everything is considered as immutable, and similarly in C# record types help us to work with immutable types. Prerequisites. .NET 5.0. Visual Studio 2019 (V 16.8, Preview 3)
I can't say for sure if you can make SQLLite use constructors instead of properties. That said, I very much doubt it.
Using properties is much easier, as you can use reflection to find the appropriate member that matches the column name. Doing that with a constructor would be extremely painful. On that note, pretty much every other ORM works the exact same way.
If you really want to use immutable types, you'll need to put a wrapper around the ORM that takes the populated objects and returns a different object that has only immutable properties. If you really want to make sure no one ever uses the mutable types, make them internal
and put them in a separate assembly.
Granted, this is a lot of extra work just for immutable types, but given the nature of ORMs, its pretty much your only option.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With