Implemented async delegates

This commit is contained in:
2025-01-27 17:58:40 +01:00
parent d42f024175
commit b6a7c508db
8 changed files with 97 additions and 57 deletions

View File

@@ -11,9 +11,9 @@ public class PropertyConfig(PropertyInfo info, TableConfig table, int nthPropert
public bool Sortable { get; set; } = true;
public bool Searchable { get; set; } = true;
public PropertyInfo? DisplayedProperty { get; set; }
public Func<object, IServiceProvider, string>? Formatter { get; set; }
public Func<object, IServiceProvider, string>? EnumerableFormatter { get; set; }
public Func<string, IServiceProvider, object>? Parser { get; set; }
public Func<object, IServiceProvider, Task<string>>? Formatter { get; set; }
public Func<object, IServiceProvider, Task<string>>? EnumerableFormatter { get; set; }
public Func<string, IServiceProvider, Task<object>>? Parser { get; set; }
public Func<object?, IServiceProvider, Task<IEnumerable<string>>>? Validator { get; set; }
public bool Editable { get; set; } = true;
public bool Creatable { get; set; } = true;
@@ -74,7 +74,6 @@ public class PropertyConfigurator<TProp>(PropertyConfig config) {
/// <summary>
/// Determines if the value that should be displayed instead of the string representation of the type
/// </summary>
/// <seealso cref="Format"/>
public PropertyConfigurator<TProp> SetDisplayedProperty<TInnerProp>(Expression<Func<TProp, TInnerProp>> propertyExpression) {
InnerConfig.DisplayedProperty = TableConfigurator<TProp>.GetPropertyInfo(propertyExpression);
return this;
@@ -83,9 +82,14 @@ public class PropertyConfigurator<TProp>(PropertyConfig config) {
/// <summary>
/// Determines the value that's displayed in the admin ui
/// </summary>
/// <seealso cref="FormatEach{TInnerProp}"/>
/// <seealso cref="SetDisplayedProperty{TInnerProp}"/>
public PropertyConfigurator<TProp> Format(Func<TProp, IServiceProvider, string> formatter) {
InnerConfig.Formatter = (obj, provider) => Task.FromResult(formatter.Invoke((TProp)obj, provider));
return this;
}
/// <inheritdoc cref="Format(System.Func{TProp,System.IServiceProvider,string})"/>
public PropertyConfigurator<TProp> Format(Func<TProp, IServiceProvider, Task<string>> formatter) {
InnerConfig.Formatter = (obj, provider) => formatter.Invoke((TProp)obj, provider);
return this;
}
@@ -94,6 +98,12 @@ public class PropertyConfigurator<TProp>(PropertyConfig config) {
/// Determines the value that's displayed for each entry in the list
/// </summary>
public PropertyConfigurator<TProp> FormatEach<TInnerProp>(Func<TInnerProp, IServiceProvider, string> formatter) {
InnerConfig.EnumerableFormatter = (obj, provider) => Task.FromResult(formatter.Invoke((TInnerProp)obj, provider));
return this;
}
/// <inheritdoc cref="FormatEach{TInnerProp}(System.Func{TInnerProp,System.IServiceProvider,string})"/>
public PropertyConfigurator<TProp> FormatEach<TInnerProp>(Func<TInnerProp, IServiceProvider, Task<string>> formatter) {
InnerConfig.EnumerableFormatter = (obj, provider) => formatter.Invoke((TInnerProp)obj, provider);
return this;
}
@@ -102,7 +112,13 @@ public class PropertyConfigurator<TProp>(PropertyConfig config) {
/// Determines the function used for parsing the value provided in the editor dialog to the actual property value
/// </summary>
public PropertyConfigurator<TProp> SetParser(Func<string, IServiceProvider, TProp> parser) {
InnerConfig.Parser = (str, provider) => parser.Invoke(str, provider)!;
InnerConfig.Parser = (str, provider) => Task.FromResult<object>(parser.Invoke(str, provider)!);
return this;
}
/// <inheritdoc cref="SetParser(System.Func{string,System.IServiceProvider,TProp})"/>
public PropertyConfigurator<TProp> SetParser(Func<string, IServiceProvider, Task<TProp>> parser) {
InnerConfig.Parser = async (str, provider) => (await parser.Invoke(str, provider))!;
return this;
}

View File

@@ -99,6 +99,17 @@ public class TableConfigurator<TModel>(TableConfig config) {
/// <returns>The configurator for the virtual property</returns>
/// <seealso cref="PropertyConfigurator{TProp}"/>
public PropertyConfigurator<string> AddVirtualProperty(string name, Func<TModel, IServiceProvider, string> template) {
var prop = new PropertyConfig(InnerConfig.Properties.First().Info, InnerConfig, InnerConfig.Properties.Count) {
Name = name,
IsListingProperty = true,
Formatter = (obj, provider) => Task.FromResult(template.Invoke((TModel)obj, provider))
};
InnerConfig.Properties.Add(prop);
return new PropertyConfigurator<string>(prop);
}
/// <inheritdoc cref="AddVirtualProperty(string,System.Func{TModel,System.IServiceProvider,string})"/>
public PropertyConfigurator<string> AddVirtualProperty(string name, Func<TModel, IServiceProvider, Task<string>> template) {
var prop = new PropertyConfig(InnerConfig.Properties.First().Info, InnerConfig, InnerConfig.Properties.Count) {
Name = name,
IsListingProperty = true,

View File

@@ -12,5 +12,5 @@ public interface ITableManager {
public Task AddItem(object item);
public Task RevertChanges(object item);
public string DisplayProperty(object? item, PropertyConfig prop, object? value = null);
public Task<string> DisplayProperty(object? item, PropertyConfig prop, object? value = null);
}

View File

@@ -66,24 +66,24 @@ internal sealed class TableManager<TModel>(DbContext context, TableConfig config
return false;
}
public string DisplayProperty(object? item, PropertyConfig prop, object? value = null) {
public async Task<string> DisplayProperty(object? item, PropertyConfig prop, object? value = null) {
if (item is null) return string.Empty;
if (prop.IsListingProperty)
return prop.Formatter!.Invoke(item, provider);
return await prop.Formatter!.Invoke(item, provider);
var propValue = value ?? prop.Info.GetValue(item);
if (propValue is null)
return string.Empty;
if (prop.Formatter is not null) {
return prop.Formatter.Invoke(propValue, provider);
return await prop.Formatter.Invoke(propValue, provider);
}
if (prop.IsEnumerable) {
if (value is not null) {
if (prop.EnumerableFormatter is not null) {
return prop.EnumerableFormatter.Invoke(value, provider);
return await prop.EnumerableFormatter.Invoke(value, provider);
}
return value.ToString() ?? string.Empty;
@@ -103,11 +103,11 @@ internal sealed class TableManager<TModel>(DbContext context, TableConfig config
var innerConfig = explorer.GetTable(propValue.GetType());
if (innerConfig is null) return propValue.ToString()!;
var innerProp = innerConfig!.Properties
var innerProp = innerConfig.Properties
.SingleOrDefault(p => p.Info == prop.DisplayedProperty && !p.IsListingProperty);
if (innerProp is null) return propValue.ToString() ?? string.Empty;
return DisplayProperty(propValue, innerProp);
return await DisplayProperty(propValue, innerProp);
}
private IQueryable<TModel> IncludeForeignKeys(IQueryable<TModel> query) {