![]() |
Sphyrnidae Common Library
2.0.1
Shared Utilities/Library
|
A Setting is a certain type of object that has the following properties:
The implementation of a setting requires you to implement the following:
Yes, this might seem like a lot of setup to essentially have a Dictionary you can query. However, this can be configured and extended to handle any scenario you can imagine, and uses the best available design pattern to do so without much custom coding.
Why this is preferable:
// Create your object type by inheriting from LookupSetting and extending for additional properties
public class Widget : LookupSetting { }
// Create a custom interface derived by ILookupSettings
public interface IWidgetSettings : ILookupSettings<Widget> { }
// Implement your custom interface derived from ILookupSettings and BaseLookupSetting
// Can optionally override virtual methods for additional behavior
public class WidgetSettings : BaseLookupSetting<Widget>, IWidgetSettings
{
public override string Key => "Widget";
public override int CachingSeconds => 600;
public override Task<IEnumerable<Widget>> GetAll()
{
var widgets = new List<Widget>
{
new Widget { Key = "widget1", Value = "foo1" },
new Widget { Key = "widget2", Value = "foo2" },
new Widget { Key = "widget3", Value = "foo3" }
};
return Task.FromResult(widgets.AsEnumerable());
}
}
// Create a custom interface derived by ILookupServices
public interface IWidgetServices : ILookupServices<IWidgetSettings, Widget> { }
// Implement your custom interface derived from IWidgetServices
public class WidgetServices : IWidgetServices
{
public ICache Cache { get; }
public IWidgetSettings Service { get; }
public WidgetServices(ICache cache, IWidgetSettings service)
{
Cache = cache;
Service = service;
}
}
// Static method implementation class that inherits from SettingsLookup
// Can place any additional methods here for different lookups
public class SettingsWidget : SettingsLookup<IWidgetSettings, Widget> { }
// Register your implementations in startup.cs
services.TryAddTransient<IWidgetSettings, WidgetSettings>();
services.TryAddTransient<IWidgetServices, WidgetServices>();
// Test everything out
IWidgetServices service; // Should be injected
var val = SettingsWidget.Get(service, "widget1", "default value"); // foo1
val = SettingsWidget.Get(service, "widget18", "default value"); // default value