Search code examples
c#xmlsqliterelationshipsuwp

Datasource for app menu stored in local database or file?


I have questions about my the structure of my UWP app (MVVM). Basically I have these three Models.

public class Country
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<City> Cities { get; set; }
    ...
}

public class City
{
    public int Id { get; set; }
    public string Name { get; set; }
    ...
}

public class Item
{
    public int Id { get; set; }
    public Country Co { get; set; }
    public City Ci { get; set; }
    ...
}

I use the classes Country and City to build a hierarchical menu (ListView) on the apps first start, therefore I parse a XML file with predefined values. My ItemsSource for the menu control is an ObservableCollection

public ObservableCollection<Country> Countries { get; set; }

The app should store instances of the class Item in a local SQLite database, and this leads to my questions. Is it a good idea to store the datasource for the apps main menu in a local database? I would prefer the database over a file, because it's easier to add new entries (Country).

I use the SQLiteNetExtensions package from NuGet so I can map relationships in the database. Which relationships (1:1, 1:m,...) fit the datastructure of the models above? Maybe you can give me a hint, I'm not an expert on data modeling.

Thank you!


Solution

  • Where do the changes originate from? Based on that decide, where to store the data.

    I would assume the user is adding instances of Item and you are storing them to the database, which you will have in the local storage.

    For Country and City items:

    • If changes are also done by the user, storing them in a database is a good idea.
    • If changes are distributed with the app, an XML file is a better choice. Or at least a different database, otherwise you will overwrite the users data or have to add the records to existing database at application startup.

    As far as relationships go:

    [OneToMany]
    public List<City> Cities { get; set; }
    
    [ManyToOne]
    public Country Co { get; set; }
    [ManyToOne]
    public City Ci { get; set; }