Add project files.
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace TheXamlGuy.Framework.Core
|
||||
{
|
||||
public interface IPropertyBinderCollection : IReadOnlyCollection<PropertyBinder>
|
||||
{
|
||||
void Add(string key, PropertyBinder binder);
|
||||
|
||||
bool TryGet(string key, [MaybeNull] out PropertyBinder? value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace TheXamlGuy.Framework.Core
|
||||
{
|
||||
public interface IPropertyBuilder
|
||||
{
|
||||
IPropertyBinderCollection Binders { get; }
|
||||
|
||||
void Add<TProperty>(Expression<Func<TProperty>> property, Action propertyChanged);
|
||||
|
||||
void Add<TProperty>(Expression<Func<TProperty>> property, Action propertyChanged,
|
||||
PropertyValidation propertyChangedValidation);
|
||||
|
||||
void Add<TProperty>(Expression<Func<TProperty>> property, PropertyValidation propertyChangedValidation);
|
||||
|
||||
void Add<TProperty>(Expression<Func<TProperty>> property, PropertyValidation propertyChangedValidation,
|
||||
PropertyChangedMode mode);
|
||||
|
||||
void Add<TProperty>(Expression<Func<TProperty>> property, Action propertyChanged,
|
||||
PropertyValidation propertyChangedValidation, PropertyChangedMode mode);
|
||||
|
||||
void Add<TProperty>(Expression<Func<TProperty>> property, Action propertyChanged, PropertyChangedMode mode);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace TheXamlGuy.Framework.Core
|
||||
{
|
||||
public class PropertyBinder
|
||||
{
|
||||
private readonly Action? propertyChanged;
|
||||
private readonly PropertyValidation? propertyValidation;
|
||||
|
||||
internal PropertyBinder(string propertyName, Action propertyChanged)
|
||||
{
|
||||
PropertyName = propertyName;
|
||||
this.propertyChanged = propertyChanged;
|
||||
}
|
||||
|
||||
internal PropertyBinder(string propertyName,
|
||||
Action propertyChanged,
|
||||
PropertyChangedMode mode)
|
||||
{
|
||||
PropertyName = propertyName;
|
||||
Mode = mode;
|
||||
|
||||
this.propertyChanged = propertyChanged;
|
||||
}
|
||||
|
||||
internal PropertyBinder(string propertyName,
|
||||
Action propertyChanged,
|
||||
PropertyValidation validation,
|
||||
PropertyChangedMode mode)
|
||||
{
|
||||
PropertyName = propertyName;
|
||||
Mode = mode;
|
||||
|
||||
this.propertyChanged = propertyChanged;
|
||||
propertyValidation = validation;
|
||||
}
|
||||
|
||||
internal PropertyBinder(string propertyName,
|
||||
Action propertyChanged,
|
||||
PropertyValidation validation)
|
||||
{
|
||||
PropertyName = propertyName;
|
||||
|
||||
this.propertyChanged = propertyChanged;
|
||||
propertyValidation = validation;
|
||||
}
|
||||
|
||||
internal PropertyBinder(string propertyName, PropertyValidation validation)
|
||||
{
|
||||
PropertyName = propertyName;
|
||||
propertyValidation = validation;
|
||||
}
|
||||
|
||||
internal PropertyBinder(string propertyName, PropertyValidation validation, PropertyChangedMode mode)
|
||||
{
|
||||
PropertyName = propertyName;
|
||||
Mode = mode;
|
||||
|
||||
propertyValidation = validation;
|
||||
}
|
||||
|
||||
public PropertyChangedMode Mode { get; }
|
||||
|
||||
public string? PropertyName { get; }
|
||||
|
||||
public void Set()
|
||||
{
|
||||
propertyChanged?.Invoke();
|
||||
}
|
||||
|
||||
public bool TryValidate([MaybeNull] out string message)
|
||||
{
|
||||
message = "";
|
||||
|
||||
if (propertyValidation is not null && propertyValidation.Validation?.Invoke() == false)
|
||||
{
|
||||
message = propertyValidation.Message;
|
||||
return false;
|
||||
}
|
||||
|
||||
propertyChanged?.Invoke();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
|
||||
namespace TheXamlGuy.Framework.Core
|
||||
{
|
||||
public class PropertyBinderCollection : IPropertyBinderCollection
|
||||
{
|
||||
private readonly Dictionary<string, PropertyBinder> binders = new();
|
||||
|
||||
public int Count => binders.Count;
|
||||
|
||||
public void Add(string key, PropertyBinder binder)
|
||||
{
|
||||
binders.Add(key, binder);
|
||||
}
|
||||
|
||||
public IEnumerator<PropertyBinder> GetEnumerator()
|
||||
{
|
||||
return binders.Select(x => x.Value).GetEnumerator();
|
||||
}
|
||||
|
||||
public bool TryGet(string key, [MaybeNull] out PropertyBinder? value)
|
||||
{
|
||||
return binders.TryGetValue(key, out value);
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return binders.Select(x => x.Value).GetEnumerator();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace TheXamlGuy.Framework.Core
|
||||
{
|
||||
public class PropertyBuilder : IPropertyBuilder
|
||||
{
|
||||
public PropertyBuilder(IPropertyBinderCollection binders)
|
||||
{
|
||||
Binders = binders;
|
||||
}
|
||||
|
||||
public IPropertyBinderCollection Binders { get; }
|
||||
|
||||
public void Add<TProperty>(Expression<Func<TProperty>> property, PropertyValidation propertyChangedValidation)
|
||||
{
|
||||
string? name = GetPropertyName(property);
|
||||
Binders.Add(name, new PropertyBinder(name, propertyChangedValidation));
|
||||
}
|
||||
|
||||
public void Add<TProperty>(Expression<Func<TProperty>> property, Action propertyChanged,
|
||||
PropertyValidation propertyChangedValidation)
|
||||
{
|
||||
string? name = GetPropertyName(property);
|
||||
Binders.Add(name, new PropertyBinder(name, propertyChanged, propertyChangedValidation));
|
||||
}
|
||||
|
||||
public void Add<TProperty>(Expression<Func<TProperty>> property, Action propertyChanged)
|
||||
{
|
||||
string? name = GetPropertyName(property);
|
||||
Binders.Add(name, new PropertyBinder(name, propertyChanged));
|
||||
}
|
||||
|
||||
public void Add<TProperty>(Expression<Func<TProperty>> property, PropertyValidation propertyChangedValidation, PropertyChangedMode mode)
|
||||
{
|
||||
string? name = GetPropertyName(property);
|
||||
Binders.Add(name, new PropertyBinder(name, propertyChangedValidation, mode));
|
||||
}
|
||||
|
||||
public void Add<TProperty>(Expression<Func<TProperty>> property, Action propertyChanged, PropertyValidation propertyChangedValidation, PropertyChangedMode mode)
|
||||
{
|
||||
string? name = GetPropertyName(property);
|
||||
Binders.Add(name, new PropertyBinder(name, propertyChanged, propertyChangedValidation, mode));
|
||||
}
|
||||
|
||||
public void Add<TProperty>(Expression<Func<TProperty>> property, Action propertyChanged, PropertyChangedMode mode)
|
||||
{
|
||||
string? name = GetPropertyName(property);
|
||||
Binders.Add(name, new PropertyBinder(name, propertyChanged, mode));
|
||||
}
|
||||
|
||||
private string GetPropertyName<T>(Expression<Func<T>> predicate)
|
||||
{
|
||||
if (predicate is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(predicate));
|
||||
}
|
||||
|
||||
Expression? body = predicate.Body;
|
||||
MemberExpression? memberExpression = body as MemberExpression ?? (MemberExpression)((UnaryExpression)body).Operand;
|
||||
return memberExpression.Member.Name;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace TheXamlGuy.Framework.Core
|
||||
{
|
||||
public enum PropertyChangedMode
|
||||
{
|
||||
Default,
|
||||
Explicit
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
|
||||
namespace TheXamlGuy.Framework.Core
|
||||
{
|
||||
public class PropertyValidation
|
||||
{
|
||||
public PropertyValidation(Func<bool> validation)
|
||||
{
|
||||
Validation = validation;
|
||||
}
|
||||
|
||||
public PropertyValidation(Func<bool> validation, string message)
|
||||
{
|
||||
Validation = validation;
|
||||
Message = message;
|
||||
}
|
||||
|
||||
public string? Message { get; }
|
||||
|
||||
public Func<bool>? Validation { get; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace TheXamlGuy.Framework.Core
|
||||
{
|
||||
public class PropertyValidationError<TKey, TValue> where TKey : class where TValue : class
|
||||
{
|
||||
private readonly Dictionary<TKey, TValue?> dictionary = new();
|
||||
|
||||
public int Count => dictionary.Count;
|
||||
|
||||
public TValue? this[[MaybeNull]TKey key]
|
||||
{
|
||||
get => dictionary.ContainsKey(key) ? dictionary[key] : null;
|
||||
set
|
||||
{
|
||||
if (Contains(key))
|
||||
{
|
||||
dictionary.Remove(key);
|
||||
}
|
||||
|
||||
dictionary.Add(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Contains(TKey key)
|
||||
{
|
||||
return dictionary.ContainsKey(key);
|
||||
}
|
||||
|
||||
public void Remove(TKey key)
|
||||
{
|
||||
if (Contains(key))
|
||||
{
|
||||
dictionary.Remove(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user