Skip to content

Commit

Permalink
Refactoring of SelectorHelper into SelectorDiscoveryService
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasherceg committed Mar 26, 2023
1 parent cf7304f commit 5e1a806
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/AutoUI/Core/AutoUIExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using DotVVM.AutoUI.Configuration;
using DotVVM.AutoUI.Controls;
using DotVVM.AutoUI.Metadata;
using DotVVM.AutoUI.PropertyHandlers;
using DotVVM.Framework.Configuration;
using DotVVM.Framework.Controls;
using DotVVM.Framework.ViewModel.Validation;
Expand All @@ -27,6 +28,7 @@ public static IDotvvmServiceCollection AddAutoUI(this IDotvvmServiceCollection s
RegisterResourceFileProviders(services.Services, autoUiConfiguration);

services.Services.Configure<DotvvmConfiguration>(AddAutoUIConfiguration);
services.Services.AddSingleton<ISelectorDiscoveryService, SelectorDiscoveryService>();

return services;
}
Expand Down
7 changes: 6 additions & 1 deletion src/AutoUI/Core/Controls/AutoFormBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ namespace DotVVM.AutoUI.Controls
public abstract class AutoFormBase : CompositeControl
{
protected readonly IServiceProvider services;

private readonly ISelectorDiscoveryService selectorDiscoveryService;

public AutoFormBase(IServiceProvider services)
{
this.services = services;
this.selectorDiscoveryService = services.GetRequiredService<ISelectorDiscoveryService>();
}


Expand Down Expand Up @@ -50,6 +54,7 @@ public AutoFormBase(IServiceProvider services)
public static readonly DotvvmCapabilityProperty FieldPropsProperty =
DotvvmCapabilityProperty.RegisterCapability<FieldProps, AutoFormBase>();


protected AutoUIContext CreateAutoUiContext()
{
return new AutoUIContext(this.GetDataContextType().NotNull(), services) {
Expand Down Expand Up @@ -200,7 +205,7 @@ protected virtual void SetFieldVisibility(HtmlGenericControl field, PropertyDisp
{
try
{
var dataSource = SelectorHelper.DiscoverSelectorDataSourceBinding(context, selector.SelectionType);
var dataSource = selectorDiscoveryService.DiscoverSelectorDataSourceBinding(context, selector.SelectionType);
var nonEmptyBinding =
dataSource.GetProperty<DataSourceLengthBinding>().Binding.GetProperty<IsMoreThanZeroBindingProperty>().Binding;
field.SetValueRaw(HtmlGenericControl.VisibleProperty, nonEmptyBinding);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using DotVVM.Framework.Compilation.ControlTree;
using DotVVM.Framework.Controls;
using DotVVM.Framework.Utils;
using Microsoft.Extensions.DependencyInjection;

namespace DotVVM.AutoUI.PropertyHandlers.FormEditors;

Expand All @@ -17,7 +18,8 @@ public override bool CanHandleProperty(PropertyDisplayMetadata property, AutoUIC
public override DotvvmControl CreateControl(PropertyDisplayMetadata property, AutoEditor.Props props, AutoUIContext context)
{
var selectorConfiguration = property.SelectionConfiguration!;
var selectorDataSourceBinding = SelectorHelper.DiscoverSelectorDataSourceBinding(context, selectorConfiguration.SelectionType);
var selectorDiscoveryService = context.Services.GetRequiredService<ISelectorDiscoveryService>();
var selectorDataSourceBinding = selectorDiscoveryService.DiscoverSelectorDataSourceBinding(context, selectorConfiguration.SelectionType);
var nestedDataContext = context.CreateChildDataContextStack(selectorConfiguration.SelectionType);

return new Repeater()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using DotVVM.AutoUI.Metadata;
using DotVVM.Framework.Controls;
using DotVVM.Framework.Utils;
using Microsoft.Extensions.DependencyInjection;

namespace DotVVM.AutoUI.PropertyHandlers.FormEditors
{
Expand All @@ -16,7 +17,8 @@ public override bool CanHandleProperty(PropertyDisplayMetadata property, AutoUIC
public override DotvvmControl CreateControl(PropertyDisplayMetadata property, AutoEditor.Props props, AutoUIContext context)
{
var selectorConfiguration = property.SelectionConfiguration!;
var selectorDataSourceBinding = SelectorHelper.DiscoverSelectorDataSourceBinding(context, selectorConfiguration.SelectionType);
var selectorDiscoveryService = context.Services.GetRequiredService<ISelectorDiscoveryService>();
var selectorDataSourceBinding = selectorDiscoveryService.DiscoverSelectorDataSourceBinding(context, selectorConfiguration.SelectionType);
var nestedDataContext = context.CreateChildDataContextStack(selectorConfiguration.SelectionType);

return new ComboBox()
Expand Down
9 changes: 9 additions & 0 deletions src/AutoUI/Core/PropertyHandlers/ISelectorDiscoveryService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;
using DotVVM.Framework.Binding.Expressions;

namespace DotVVM.AutoUI.PropertyHandlers;

public interface ISelectorDiscoveryService
{
IValueBinding DiscoverSelectorDataSourceBinding(AutoUIContext autoUiContext, Type propertyType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

namespace DotVVM.AutoUI.PropertyHandlers
{
public class SelectorHelper
public class SelectorDiscoveryService : ISelectorDiscoveryService
{

public static IValueBinding DiscoverSelectorDataSourceBinding(AutoUIContext autoUiContext, Type propertyType)
public IValueBinding DiscoverSelectorDataSourceBinding(AutoUIContext autoUiContext, Type propertyType)
{
var viewModelType = typeof(ISelectorViewModel<>).MakeGenericType(propertyType);

Expand Down Expand Up @@ -49,7 +49,7 @@ public static IValueBinding DiscoverSelectorDataSourceBinding(AutoUIContext auto
throw new DotvvmControlException($"No property of type {viewModelType.FullName} was found in the viewmodel {autoUiContext.DataContextStack}!");
}

private static Expression[] FindSelectorProperties(Expression parent, Type selectorViewModelType)
protected virtual Expression[] FindSelectorProperties(Expression parent, Type selectorViewModelType)
{
var directProperties = parent.Type
.GetProperties(BindingFlags.Instance | BindingFlags.Public)
Expand Down

0 comments on commit 5e1a806

Please sign in to comment.