简体   繁体   中英

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; } { get; set; } { 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM