-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ refactor: Most of the content of SettingService has been moved to …
…the interface
- Loading branch information
Showing
7 changed files
with
151 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 10 additions & 9 deletions
19
src/SwashbucklerDiary.Maui/Services/SettingService/SettingService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 120 additions & 16 deletions
136
src/SwashbucklerDiary.Rcl/Services/SettingService/ISettingService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,142 @@ | ||
using SwashbucklerDiary.Rcl.Essentials; | ||
using Masa.Blazor; | ||
using SwashbucklerDiary.Rcl.Essentials; | ||
using System.Linq.Expressions; | ||
|
||
namespace SwashbucklerDiary.Rcl.Services | ||
{ | ||
public interface ISettingService : IPreferences | ||
{ | ||
Task InitializeAsync(); | ||
protected Dictionary<string, object> DefalutSettings { get; set; } | ||
|
||
T Get<T>(string key); | ||
protected Dictionary<string, object> TempSettings { get; set; } | ||
|
||
T Get<T>(string key, T defaultValue); | ||
public Task InitializeAsync() => DefalutInitializeAsync(DefalutSettings); | ||
|
||
T Get<T>(Expression<Func<Setting, T>> expr); | ||
protected static Task DefalutInitializeAsync(Dictionary<string, object> dictionary) | ||
{ | ||
var defaultSettings = new Setting().ToParameters().ToDictionary(); | ||
var defaultTempSettings = new TempSetting().ToParameters().ToDictionary(); | ||
|
||
T Get<T>(Expression<Func<Setting, T>> expr, T defaultValue); | ||
AddDefaultSettings(dictionary, defaultSettings); | ||
AddDefaultSettings(dictionary, defaultTempSettings); | ||
|
||
Task SetAsync<T>(Expression<Func<Setting, T>> expr, T value); | ||
return Task.CompletedTask; | ||
} | ||
|
||
Task RemoveAsync<T>(Expression<Func<Setting, T>> expr); | ||
private static void AddDefaultSettings(Dictionary<string, object> dictionary, Dictionary<string, object?> dictionary2) | ||
{ | ||
foreach (var item in dictionary2) | ||
{ | ||
if (item.Value is not null) | ||
{ | ||
dictionary.Add(item.Key, item.Value); | ||
} | ||
} | ||
} | ||
|
||
T GetTemp<T>(string key); | ||
public T Get<T>(string key); | ||
|
||
T GetTemp<T>(Expression<Func<TempSetting, T>> expr); | ||
public T Get<T>(string key, T defaultValue); | ||
|
||
T GetTemp<T>(string key, T defaultValue); | ||
public T GetTemp<T>(string key) | ||
{ | ||
if (TempSettings.TryGetValue(key, out var value)) | ||
{ | ||
return (T)value; | ||
} | ||
|
||
T GetTemp<T>(Expression<Func<TempSetting, T>> expr, T defaultValue); | ||
if (DefalutSettings.TryGetValue(key, out var defaulValue)) | ||
{ | ||
return (T)defaulValue; | ||
} | ||
|
||
void SetTemp<T>(string key, T value); | ||
return default!; | ||
} | ||
|
||
void SetTemp<T>(Expression<Func<TempSetting, T>> expr, T value); | ||
public T GetTemp<T>(string key, T defaultValue) | ||
{ | ||
if (TempSettings.TryGetValue(key, out var value)) | ||
{ | ||
return (T)value; | ||
} | ||
|
||
void RemoveTemp(string key); | ||
return defaultValue; | ||
} | ||
|
||
void RemoveTemp<T>(Expression<Func<TempSetting, T>> expr); | ||
public void SetTemp<T>(string key, T value) | ||
{ | ||
TempSettings[key] = value; | ||
} | ||
|
||
public void RemoveTemp(string key) | ||
{ | ||
TempSettings.Remove(key); | ||
} | ||
|
||
public T Get<T>(Expression<Func<Setting, T>> expr) | ||
{ | ||
if (expr.Body is MemberExpression memberExpr) | ||
{ | ||
return Get<T>(memberExpr.Member.Name); | ||
} | ||
|
||
throw new ArgumentException("Expression does not contain a member access."); | ||
} | ||
|
||
public Task SetAsync<T>(Expression<Func<Setting, T>> expr, T value) | ||
{ | ||
var key = GetSettingKey(expr); | ||
return SetAsync(key, value); | ||
} | ||
|
||
private static string GetSettingKey<T>(Expression<Func<Setting, T>> expr) => GetExpressionMemberName(expr); | ||
|
||
private static string GetTempSettingKey<T>(Expression<Func<TempSetting, T>> expr) => GetExpressionMemberName(expr); | ||
|
||
private static string GetExpressionMemberName<T, TResult>(Expression<Func<T, TResult>> expr) | ||
{ | ||
if (expr.Body is MemberExpression memberExpr) | ||
{ | ||
return memberExpr.Member.Name; | ||
} | ||
|
||
throw new ArgumentException("Expression does not contain a member access."); | ||
} | ||
|
||
public T GetTemp<T>(Expression<Func<TempSetting, T>> expr) | ||
{ | ||
var key = GetTempSettingKey(expr); | ||
return GetTemp<T>(key); | ||
} | ||
|
||
public T GetTemp<T>(Expression<Func<TempSetting, T>> expr, T defaultValue) | ||
{ | ||
var key = GetTempSettingKey(expr); | ||
return GetTemp<T>(key, defaultValue); | ||
} | ||
|
||
public void SetTemp<T>(Expression<Func<TempSetting, T>> expr, T value) | ||
{ | ||
var key = GetTempSettingKey(expr); | ||
SetTemp<T>(key, value); | ||
} | ||
|
||
public void RemoveTemp<T>(Expression<Func<TempSetting, T>> expr) | ||
{ | ||
var key = GetTempSettingKey(expr); | ||
RemoveTemp(key); | ||
} | ||
|
||
public T Get<T>(Expression<Func<Setting, T>> expr, T defaultValue) | ||
{ | ||
var key = GetSettingKey(expr); | ||
return GetTemp(key, defaultValue); | ||
} | ||
|
||
public Task RemoveAsync<T>(Expression<Func<Setting, T>> expr) | ||
{ | ||
var key = GetSettingKey(expr); | ||
return RemoveAsync(key); | ||
} | ||
} | ||
} |
165 changes: 0 additions & 165 deletions
165
src/SwashbucklerDiary.Rcl/Services/SettingService/SettingService.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.