User preferences/options framework

Input examples preferences are implemented with the Mvc Controls Toolkit Preferences/Options framework. You may change some of the preferences also in this page (see above). In this case preferences are taken from 3 sources:

  1. url/form inputs (highest priority)
  2. cookie (average priority)
  3. application configuration file (lowest priority)

First time user navigates to this web site preferences are taken from the application configuration file (lowest priority), and put in a request specific hierarchical dictionary. Then a cookie is created (average priority), since the cookie provider is writeable and has been configured to store the current settings.

From this moment on preferences will always be taken from this cookie, but when user provide new preferences in a Url/Form, since these preferences have higher priority. In fact, the preferences change logic works by putting in the Url new preferences. Once provided, also Url preferences are stored in the preferences cookie and become permanent.

Preferences can be extracted by the preferences hierarchical dictionary by matching the dictionary nested keys with possibly nested "option objects". These objects are created and matched as soon as they are injected somewhere. They have a per-request lifetime.

Preference may be taken/stored also in a database table or within the user "claims". All providers are configured in a purely declarative way, by simply specifyng a few properties of each provider.

See preliminary documentation for more informations.

@inject ExamplesOptions subType
@*This is the View of a ViewComponent 
  called by all Views where the user may change his/her preferences*@
<form asp-controller="InputExamples" asp-action="@Model.Type" method="get" class="form-horizontal marked">
    @if (Model.ShowFallback)
    {
        <label class="radio-inline"><input type="radio" @(!Model.FallBack ? "checked" : "") name="@("ExamplesOptions.ForcedFallbacks." + Model.Type)" value="False" />Try Html5 input</label>
        <label class="radio-inline"><input type="radio" @(Model.FallBack ? "checked" : "") name="@("ExamplesOptions.ForcedFallbacks." + Model.Type)" value="True" />Force widget</label>
    }
        <label class="radio-inline"><input @(subType.ValidationRangeType == ValidationRangeType.Fixed ? "checked" : "") type="radio" value="Fixed" name="ExamplesOptions.Basics.ValidationRangeType">Fixed range</label>
        <label class="radio-inline"><input @(subType.ValidationRangeType == ValidationRangeType.Dynamic ? "checked" : "") type="radio" value="Dynamic" name="ExamplesOptions.Basics.ValidationRangeType">Dyanmic range</label>
        <label class="radio-inline"><input @(subType.ValidationRangeType == ValidationRangeType.DynamicWithTolerance ? "checked" : "") type="radio" value="DynamicWithTolerance" name="ExamplesOptions.Basics.ValidationRangeType">Dynamic range+tolerance</label>
        <div style="margin-top10px">
            <button type="submit" class="btn btn-xs btn-default">Submit preferences changes</button>
        </div>
 
</form>
public InputExamplesController(ExamplesOptions eo)
        {
            this.eo = eo;
 
        }
 
        public IActionResult Number()
        {
            switch (eo.ValidationRangeType)
            {
                case ValidationRangeType.Fixed:
                    return View(new NumberExample { Value = 1.5f });
                case ValidationRangeType.Dynamic:
                    return View("NumberDynamic", 
                        new NumberExampleDynamic {
                            Value = 5f,
                            MinValue = 1.0f,
                            MaxValue = 10.0f
                        });
                default:
                    return View("NumberDynamicTolerance", 
                        new NumberExampleDynamicTolerance {
                            Value = 5f,
                            MinValue = 1.0f,
                            MaxValue = 10.0f });
            }
 
        }
services.AddPreferences()
    .AddPreferencesClass<ExamplesOptions>("Examples.Basics")
    .AddPreferencesClass<Html5ForcedFallbacks>("Examples.ForcedFallbacks")
    .AddPreferencesProvider(new ApplicationConfigurationProvider("Examples", Configuration)
    {
        SourcePrefix = "ExamplesCustomization"
    })
    .AddPreferencesProvider(new CookieProvider("Examples""_examples")
    {
        Priority = 1,
        AutoCreate = true,
        WhenEnabled = x => x.User == null || x.User.Identity == null || !x.User.Identity.IsAuthenticated
    })
    .AddPreferencesProvider(new RequestProvider("Examples")
    {
        Priority = 10,
        SourcePrefix = "ExamplesOptions",
        UseForm=true
    });

Fork me on GitHub