Added fully virtual properties
This commit is contained in:
@@ -26,7 +26,7 @@ public class DbContextConfig {
|
||||
/// <summary>
|
||||
/// A helper class for editing the <see cref="DbContextConfig"/>
|
||||
/// </summary>
|
||||
public class DbContextConfigurator<TDbContext>(DbContextConfig config) {
|
||||
public sealed class DbContextConfigurator<TDbContext>(DbContextConfig config) {
|
||||
|
||||
/// <summary>
|
||||
/// The Internal DbContext configuration that's modified by the helper functions
|
||||
|
||||
@@ -15,7 +15,7 @@ public class HopFrameConfig {
|
||||
/// <summary>
|
||||
/// A helper class for editing the <see cref="HopFrameConfig"/>
|
||||
/// </summary>
|
||||
public class HopFrameConfigurator(HopFrameConfig config, IServiceCollection collection = null!) {
|
||||
public sealed class HopFrameConfigurator(HopFrameConfig config, IServiceCollection collection = null!) {
|
||||
|
||||
/// <summary>
|
||||
/// The Internal HopFrame configuration that's modified by the helper functions
|
||||
|
||||
@@ -23,9 +23,37 @@ public class PropertyConfig(PropertyInfo info, TableConfig table, int nthPropert
|
||||
public bool IsRelation { get; internal set; }
|
||||
public bool IsRequired { get; internal set; }
|
||||
public bool IsEnumerable { get; internal set; }
|
||||
public bool IsListingProperty { get; set; }
|
||||
public bool IsVirtualProperty { get; set; }
|
||||
public int Order { get; set; } = nthProperty;
|
||||
public int DisplayLength { get; set; } = 32;
|
||||
|
||||
public virtual object? GetValue(object? source, IServiceProvider provider) {
|
||||
return Info.GetValue(source);
|
||||
}
|
||||
|
||||
public virtual void SetValue(object? source, object? value, IServiceProvider provider) {
|
||||
Info.SetValue(source, value);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class VirtualPropertyConfig(TableConfig table, int nthProperty) : PropertyConfig(GetDummyProperty(), table, nthProperty) {
|
||||
public string? DummyProperty { get; set; } = null;
|
||||
|
||||
public Func<object, string, IServiceProvider, Task>? VirtualParser { get; set; }
|
||||
|
||||
public override object? GetValue(object? source, IServiceProvider provider) {
|
||||
return Formatter!.Invoke(source!, provider).Result;
|
||||
}
|
||||
|
||||
public override void SetValue(object? source, object? value, IServiceProvider provider) {
|
||||
VirtualParser?.Invoke(source!, (string)value!, provider).Wait();
|
||||
}
|
||||
|
||||
private static PropertyInfo GetDummyProperty() {
|
||||
return typeof(VirtualPropertyConfig)
|
||||
.GetProperties()
|
||||
.First(prop => prop.Name == nameof(DummyProperty));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -213,3 +241,28 @@ public class PropertyConfigurator<TProp>(PropertyConfig config) {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class VirtualPropertyConfigurator<TModel>(VirtualPropertyConfig config) : PropertyConfigurator<string>(config) {
|
||||
/// <summary>
|
||||
/// Determines the function used for parsing the value provided in the editor dialog to the actual model value
|
||||
/// </summary>
|
||||
public VirtualPropertyConfigurator<TModel> SetVirtualParser(Action<TModel, string, IServiceProvider> parser) {
|
||||
var cfg = InnerConfig as VirtualPropertyConfig;
|
||||
|
||||
cfg!.VirtualParser = (model, input, services) => {
|
||||
parser.Invoke((TModel)model, input, services);
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="SetVirtualParser{TModel}(System.Action{TModel,string,System.IServiceProvider})"/>
|
||||
public VirtualPropertyConfigurator<TModel> SetVirtualParser(Func<TModel, string, IServiceProvider, Task> parser) {
|
||||
var cfg = InnerConfig as VirtualPropertyConfig;
|
||||
|
||||
cfg!.VirtualParser = (model, input, services) => parser.Invoke((TModel)model, input, services);
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ public class TableConfig {
|
||||
/// <summary>
|
||||
/// A helper class for editing the <see cref="TableConfig"/>
|
||||
/// </summary>
|
||||
public class TableConfigurator<TModel>(TableConfig config) {
|
||||
public sealed class TableConfigurator<TModel>(TableConfig config) {
|
||||
|
||||
/// <summary>
|
||||
/// The Internal property configuration that's modified by the helper functions
|
||||
@@ -76,7 +76,7 @@ public class TableConfigurator<TModel>(TableConfig config) {
|
||||
public PropertyConfigurator<TProp> Property<TProp>(Expression<Func<TModel, TProp>> propertyExpression) {
|
||||
var info = GetPropertyInfo(propertyExpression);
|
||||
var prop = InnerConfig.Properties
|
||||
.Single(prop => prop.Info.Name == info.Name);
|
||||
.Single(prop => prop.Info == info);
|
||||
return new PropertyConfigurator<TProp>(prop);
|
||||
}
|
||||
|
||||
@@ -99,25 +99,25 @@ public class TableConfigurator<TModel>(TableConfig config) {
|
||||
/// <param name="template">The template used for generating the property value</param>
|
||||
/// <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) {
|
||||
public VirtualPropertyConfigurator<TModel> AddVirtualProperty(string name, Func<TModel, IServiceProvider, string> template) {
|
||||
var prop = new VirtualPropertyConfig(InnerConfig, InnerConfig.Properties.Count) {
|
||||
Name = name,
|
||||
IsListingProperty = true,
|
||||
IsVirtualProperty = true,
|
||||
Formatter = (obj, provider) => Task.FromResult(template.Invoke((TModel)obj, provider))
|
||||
};
|
||||
InnerConfig.Properties.Add(prop);
|
||||
return new PropertyConfigurator<string>(prop);
|
||||
return new VirtualPropertyConfigurator<TModel>(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) {
|
||||
public VirtualPropertyConfigurator<TModel> AddVirtualProperty(string name, Func<TModel, IServiceProvider, Task<string>> template) {
|
||||
var prop = new VirtualPropertyConfig(InnerConfig, InnerConfig.Properties.Count) {
|
||||
Name = name,
|
||||
IsListingProperty = true,
|
||||
IsVirtualProperty = true,
|
||||
Formatter = (obj, provider) => template.Invoke((TModel)obj, provider)
|
||||
};
|
||||
InnerConfig.Properties.Add(prop);
|
||||
return new PropertyConfigurator<string>(prop);
|
||||
return new VirtualPropertyConfigurator<TModel>(prop);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -229,7 +229,7 @@ public class TableConfigurator<TModel>(TableConfig config) {
|
||||
throw new ArgumentException($"Expression '{propertyLambda}' refers to a field, not a property.");
|
||||
}
|
||||
|
||||
Type type = typeof(TSource);
|
||||
var type = typeof(TSource);
|
||||
if (propInfo.ReflectedType != null && type != propInfo.ReflectedType &&
|
||||
!type.IsSubclassOf(propInfo.ReflectedType)) {
|
||||
throw new ArgumentException($"Expression '{propertyLambda}' refers to a property that is not from type {type}.");
|
||||
|
||||
@@ -61,7 +61,7 @@ internal sealed class ContextExplorer(HopFrameConfig config, IServiceProvider pr
|
||||
var entity = dbContext.Model.FindEntityType(table.TableType)!;
|
||||
|
||||
foreach (var propertyConfig in table.Properties) {
|
||||
if (propertyConfig.IsListingProperty) continue;
|
||||
if (propertyConfig.IsVirtualProperty) continue;
|
||||
if (propertyConfig.IsRelation) continue;
|
||||
|
||||
var prop = entity.FindProperty(propertyConfig.Info.Name);
|
||||
@@ -93,7 +93,7 @@ internal sealed class ContextExplorer(HopFrameConfig config, IServiceProvider pr
|
||||
|
||||
foreach (var property in entity.GetProperties()) {
|
||||
var propConfig = table.Properties
|
||||
.Where(prop => !prop.IsListingProperty)
|
||||
.Where(prop => !prop.IsVirtualProperty)
|
||||
.SingleOrDefault(prop => prop.Info == property.PropertyInfo);
|
||||
if (propConfig is null || propConfig.IsRequired) continue;
|
||||
propConfig.IsRequired = !property.IsNullable;
|
||||
|
||||
@@ -63,7 +63,7 @@ internal sealed class TableManager<TModel>(DbContext context, TableConfig config
|
||||
private bool ItemSearched(TModel item, string searchTerm) {
|
||||
foreach (var property in config.Properties) {
|
||||
if (!property.Searchable) continue;
|
||||
var value = property.Info.GetValue(item);
|
||||
var value = property.GetValue(item, provider);
|
||||
if (value is null) continue;
|
||||
|
||||
var strValue = value.ToString();
|
||||
@@ -77,10 +77,10 @@ internal sealed class TableManager<TModel>(DbContext context, TableConfig config
|
||||
public async Task<string> DisplayProperty(object? item, PropertyConfig prop, object? value = null, object? enumerableValue = null) {
|
||||
if (item is null) return string.Empty;
|
||||
|
||||
if (prop.IsListingProperty)
|
||||
if (prop.IsVirtualProperty)
|
||||
return await prop.Formatter!.Invoke(item, provider);
|
||||
|
||||
var propValue = value ?? prop.Info.GetValue(item);
|
||||
var propValue = value ?? prop.GetValue(item, provider);
|
||||
if (propValue is null)
|
||||
return string.Empty;
|
||||
|
||||
@@ -112,7 +112,7 @@ internal sealed class TableManager<TModel>(DbContext context, TableConfig config
|
||||
if (innerConfig is null) return propValue.ToString()!;
|
||||
|
||||
var innerProp = innerConfig.Properties
|
||||
.SingleOrDefault(p => p.Info == prop.DisplayedProperty && !p.IsListingProperty);
|
||||
.SingleOrDefault(p => p.Info == prop.DisplayedProperty && !p.IsVirtualProperty);
|
||||
|
||||
if (innerProp is null) return propValue.ToString() ?? string.Empty;
|
||||
return await DisplayProperty(propValue, innerProp);
|
||||
|
||||
Reference in New Issue
Block a user