Skip to content

Commit

Permalink
Rename generic type binding configurator
Browse files Browse the repository at this point in the history
  • Loading branch information
mingyaulee committed Sep 16, 2022
1 parent fff66ed commit 608cfb4
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 92 deletions.
70 changes: 3 additions & 67 deletions src/JsBind.Net/Configurations/BindingConfigurator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using JsBind.Net.BindingConfigurations;
using JsBind.Net.BindingConfigurations;

namespace JsBind.Net.Configurations
{
Expand All @@ -21,71 +19,9 @@ public BindingConfigurator(IBindingConfigurationProvider bindingConfigurationPro
}

/// <inheritdoc />
public IBindingConfigurator<T> Bind<T>()
public ITypedBindingConfigurator Bind<T>()
{
return new BindingConfigurator<T>(bindingConfigurationProvider);
}
}

/// <summary>
/// The configurator for binding of type <typeparamref name="T" />.
/// </summary>
/// <typeparam name="T">The type configured.</typeparam>
public class BindingConfigurator<T> : IBindingConfigurator<T>
{
private readonly IBindingConfigurationProvider bindingConfigurationProvider;
private BindingConfiguration? bindingConfiguration;
private BindingConfiguration BindingConfiguration
{
get
{
if (bindingConfiguration is null)
{
bindingConfiguration = new BindingConfiguration();
bindingConfigurationProvider.Add(typeof(T), bindingConfiguration);
}

return bindingConfiguration;
}
}

/// <summary>
/// Creates a new instance of <see cref="BindingConfigurator" />.
/// </summary>
/// <param name="bindingConfigurationProvider">The binding configuration provider.</param>
public BindingConfigurator(IBindingConfigurationProvider bindingConfigurationProvider)
{
this.bindingConfigurationProvider = bindingConfigurationProvider;
}

/// <inheritdoc />
public void IncludeDeclaredProperties()
{
AddIncludeProperties(bindingConfigurationProvider.GetDeclaredProperties(typeof(T)));
}

/// <inheritdoc />
public void IncludeProperties(params string[] properties)
{
AddIncludeProperties(properties);
}

/// <inheritdoc />
public void ExcludeProperties(params string[] properties)
{
BindingConfiguration.ExcludeProperties = properties;
}

private void AddIncludeProperties(IEnumerable<string> includeProperties)
{
if (BindingConfiguration.IncludeProperties is null)
{
BindingConfiguration.IncludeProperties = includeProperties;
}
else
{
BindingConfiguration.IncludeProperties = BindingConfiguration.IncludeProperties.Concat(includeProperties).ToList();
}
return new TypedBindingConfigurator<T>(bindingConfigurationProvider);
}
}
}
26 changes: 1 addition & 25 deletions src/JsBind.Net/Configurations/IBindingConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,6 @@ public interface IBindingConfigurator
/// </summary>
/// <typeparam name="T">The type to configure.</typeparam>
/// <returns>The configurator for binding of type <typeparamref name="T" />.</returns>
IBindingConfigurator<T> Bind<T>();
}

/// <summary>
/// The configurator for binding of type <typeparamref name="T" />.
/// </summary>
/// <typeparam name="T">The type configured.</typeparam>
public interface IBindingConfigurator<T>
{
/// <summary>
/// Configure the type to include declared properties.
/// </summary>
void IncludeDeclaredProperties();

/// <summary>
/// Configure the type to include specified properties.
/// </summary>
/// <param name="properties">The properties to include.</param>
void IncludeProperties(params string[] properties);

/// <summary>
/// Configure the type to exclude specified properties.
/// </summary>
/// <param name="properties">The properties to exclude.</param>
void ExcludeProperties(params string[] properties);
ITypedBindingConfigurator Bind<T>();
}
}
25 changes: 25 additions & 0 deletions src/JsBind.Net/Configurations/ITypedBindingConfigurator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace JsBind.Net.Configurations
{
/// <summary>
/// The configurator for binding of a defined type.
/// </summary>
public interface ITypedBindingConfigurator
{
/// <summary>
/// Configure the type to include declared properties.
/// </summary>
void IncludeDeclaredProperties();

/// <summary>
/// Configure the type to include specified properties.
/// </summary>
/// <param name="properties">The properties to include.</param>
void IncludeProperties(params string[] properties);

/// <summary>
/// Configure the type to exclude specified properties.
/// </summary>
/// <param name="properties">The properties to exclude.</param>
void ExcludeProperties(params string[] properties);
}
}
68 changes: 68 additions & 0 deletions src/JsBind.Net/Configurations/TypedBindingConfigurator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System.Collections.Generic;
using System.Linq;
using JsBind.Net.BindingConfigurations;

namespace JsBind.Net.Configurations
{
/// <summary>
/// The configurator for binding of type <typeparamref name="T" />.
/// </summary>
/// <typeparam name="T">The type configured.</typeparam>
public class TypedBindingConfigurator<T> : ITypedBindingConfigurator
{
private readonly IBindingConfigurationProvider bindingConfigurationProvider;
private BindingConfiguration? bindingConfiguration;
private BindingConfiguration BindingConfiguration
{
get
{
if (bindingConfiguration is null)
{
bindingConfiguration = new BindingConfiguration();
bindingConfigurationProvider.Add(typeof(T), bindingConfiguration);
}

return bindingConfiguration;
}
}

/// <summary>
/// Creates a new instance of <see cref="BindingConfigurator" />.
/// </summary>
/// <param name="bindingConfigurationProvider">The binding configuration provider.</param>
public TypedBindingConfigurator(IBindingConfigurationProvider bindingConfigurationProvider)
{
this.bindingConfigurationProvider = bindingConfigurationProvider;
}

/// <inheritdoc />
public void IncludeDeclaredProperties()
{
AddIncludeProperties(bindingConfigurationProvider.GetDeclaredProperties(typeof(T)));
}

/// <inheritdoc />
public void IncludeProperties(params string[] properties)
{
AddIncludeProperties(properties);
}

/// <inheritdoc />
public void ExcludeProperties(params string[] properties)
{
BindingConfiguration.ExcludeProperties = properties;
}

private void AddIncludeProperties(IEnumerable<string> includeProperties)
{
if (BindingConfiguration.IncludeProperties is null)
{
BindingConfiguration.IncludeProperties = includeProperties;
}
else
{
BindingConfiguration.IncludeProperties = BindingConfiguration.IncludeProperties.Concat(includeProperties).ToList();
}
}
}
}

0 comments on commit 608cfb4

Please sign in to comment.