Can I use a List of String in a class intended for SQLite?
Asked Answered
A

3

16

What limitations are there on data types used in a class that will be used by SQLite-net to represent a table? Specifically, can I use this:

public List<string> CoconutWaterBrands { get; set; }

...or will I need this:

public string[] CoconutWaterBrands { get; set; }

...or something else altogether?

Arsenide answered 2/2, 2013 at 16:31 Comment(0)
B
11

ORMs (aka abstraction leak) will have this kind of problem. The fields in the class will correspond to columns in the table. If you use List or arrays then the ORM will either have to somehow combine those values in the list in a single column, for that it will have to choose some kind of separator and what if the separator appears inside your string value, ultimately you will slowly find your self in rabbit hole.

I don't think SQLite-net support arrays/list. You will need to use string (and split and join it as required) or create a new table to represent the one-to-many relationship.

Boson answered 2/2, 2013 at 16:45 Comment(0)
S
21

Similar to Sandy's Answer, in terms of serializing/deserializing the list, you could use Text blobbed properties from the SQLite-Net Extensions library. So for example in your Model class:

public class SomethingToDoWithCoconuts
{
    [TextBlob(nameof(CoconutWaterBrandsBlobbed))]
    public List<string> CoconutWaterBrands { get; set; }
    public string CoconutWaterBrandsBlobbed { get; set; } // serialized CoconutWaterBrands
}

from the documentation on Text blobbed properties:

Text-blobbed properties are serialized into a text property when saved and deserialized when loaded. This allows storing simple objects in the same table in a single column.

Text-blobbed properties have a small overhead of serializing and deserializing the objects and some limitations, but are the best way to store simple objects like List or Dictionary of basic types or simple relationships. Text-blobbed properties require a declared string property where the serialized object is stored.

Text-blobbed properties cannot have relationships to other objects nor inverse relationship to its parent.

A JSON-based serializer is used if no other serializer has been specified using TextBlobOperations.SetTextSerializer method. To use the JSON serializer, a reference to Newtonsoft Json.Net library must be included in the project, also available as a NuGet package.

Sherburn answered 12/3, 2016 at 21:37 Comment(1)
And since C# 6 you could also write [TextBlob(nameof(CoconutWaterBrandsBlobbed))] to make it less error-prone and more refactorable.Hilltop
B
11

ORMs (aka abstraction leak) will have this kind of problem. The fields in the class will correspond to columns in the table. If you use List or arrays then the ORM will either have to somehow combine those values in the list in a single column, for that it will have to choose some kind of separator and what if the separator appears inside your string value, ultimately you will slowly find your self in rabbit hole.

I don't think SQLite-net support arrays/list. You will need to use string (and split and join it as required) or create a new table to represent the one-to-many relationship.

Boson answered 2/2, 2013 at 16:45 Comment(0)
G
11

To provide a more concrete solution to the question, you could use Newtonsoft.Json to serialize to a JSON blob:

I.e.

private string coconutWaterBrands;

[Ignore]
public List<string> CoconutWaterBrands { 
    get
    {
        return JsonConvert.DeserializeObject<List<string>>(coconutWaterBrands);
    } 
    set
    {
        coconutWaterBrands = JsonConvert.SerializeObject(value);
    }
}
Gittern answered 8/7, 2015 at 12:39 Comment(1)
Maybe not with [Ignore] then, or you'll need another property to get it to (de)serialize.Tack

© 2022 - 2024 — McMap. All rights reserved.