Creating mechanism to save dynamic settings locally with ASP.NET Core, Entity Framework Core and SQLite

Creating mechanism to save dynamic settings locally with ASP.NET Core, Entity Framework Core and SQLite

Next step in TeamScreen development is settings screen. In time I plan to abandon solution with providing credentials to 3rd party services using configuration files and moved them to more user-friendly UI. In today’s post, I’ll start from saving only one setting – an interval between plugin change. Saving other settings will come after the creation of plugin architecture.

The first thing we need to do is to remove existing reference to Microsoft.EntityFrameworkCore.SqlServer – it’s added by default when creating new ASPNET.Core project. After that, I add a reference to Microsoft.EntityFrameworkCore.Sqlite. Next, I’ll add entity – it’s very simple:

Different plugins will have a different set of settings. To handle it I plan to serialize those settings to JSON and then save them in Value property. Next, we need to have DbContext containing PluginSetting’s entity set:

DBContextOptions are passed to DbContext using dependency injection, so of course, we need to register that, which is done in Startup class:

Having entity and DbContext set up, we need now to create initial DB migration describing how DB is created. I still have identity context which I hope I’ll use in future for managing users. With newly added DbContext, we have two, so when creating migrations, we need to set which context we set:

Ok, DB should be created when we will access it for the first time. Now, how exactly will look settings, that’ll be saved there? All of them implement interface ISettings:

ISettings contains only one method used to set up initial settings values. In future, all plugins will have settings created for them. For now, I have only CoreSettings for general TeamScreen settings containing only Interval property:

 Now – SettingsService which role is to save and load settings:

We have two methods – Get and Set. Get returns first settings from DB for given plugin, when nothing is found we return default settings. When something is returned from DB I deserialize it to correct settings type. Set serializes settings and saves them in DB, depending if it existed before uses insert or update. Both these methods are generic so I can save/load type-safe settings. To do that I needed to provide additional restrictions on generic type:

Ok, with that we have a mechanism to dynamically save and load settings. Next thing we need is actual UI for this. For now, it’ll be simple, as we need to only display one setting. We have a main page with a dynamically loaded partial view for “Core” plugin:

We also need a controller for that:

All of that creates this page:

It’s – once again – very simple, but also generic. In future it’ll easy to use it for all sort of settings for another plugins. I hope you liked the solution. Thanks for reading and see you next time 🙂

One thought on “Creating mechanism to save dynamic settings locally with ASP.NET Core, Entity Framework Core and SQLite

  1. Pingback: dotnetomaniak.pl

Leave a Reply

Your email address will not be published. Required fields are marked *