.Net CoreDevelopment

Simple Settings service example in C#.

In .NET world storing data into xml configuration files is very common scenario. While, web apps (Asp.Net) use web.config, desktops use app.config (renamed to <FullExecutableFilename>.exe.config at runtime).

Every application sooner or later needs to collect, persist and restore some data (settings) from (to) end user. While web apps store these information almost always into database, desktops app store these settings frequently to XML based configuration files.

Problem.

For one of my prototypes (POC) I just wanted to create simple, persistence agnostic settings service. Having some difficulties playing together .NET Core and System.Configuration I decided to go my own way and create simple persisting data service. Let’s start!

Simple settings service.

My simple settings service definition.

And default implementation.

I almost always use Prism library to easily decouple my app into smaller more maintainable peaces. So, in my Prism registration section I just register my settings service, similar as:

And I could use my settings cross application, like:

or in any ViewModel by DI pattern, like:

Result, from GUI perspective, looks like:

GUI for my Settings service.

Settings (json file) storage looks like:

It makes sense to put this file into Environment.SpecialFolder.LocalApplicationData location, because this is end-user working location where he/she can read write files without problems.

Conclusion.

Application settings are very important part of any application.

Because of architectural specifics, Asp.Net web application almost always only read config settings from web.config and store any user settings into database.

In contrary, desktop apps very often use read/write settings files to persist user settings. Very often, .settings files were used to store application of user settings.

In this blog post I wanted to create simple but generic settings service to store end-user settings and persist it in json file.

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.