Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use SQLite.NET with immutable record types?

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?

like image 419
Display Name Avatar asked Jan 11 '15 17:01

Display Name


People also ask

Are records immutable?

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.

Can record have methods C#?

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):

What is the use of record type in C#?

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.

What is record type in C# 9?

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)


1 Answers

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.

like image 153
BradleyDotNET Avatar answered Sep 22 '22 20:09

BradleyDotNET