Last active
September 10, 2024 04:34
-
-
Save papinko/41f562566c2c902a29fda784415f36e7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public sealed class EditableProperty<T> : EditablePropertyBase<T> where T : IComparable | |
{ | |
protected override Dictionary<Func<bool>, T> ValidationRules { get; } = new Dictionary<Func<bool>, T>(); | |
public EditableProperty() | |
: base(default(T)) | |
{ | |
} | |
public EditableProperty(T value) | |
: base(value) | |
{ | |
} | |
public EditableProperty(T value, Action executeOnModifiedChanged) | |
: this(value) | |
{ | |
base.ExecuteOnModifiedChanged = executeOnModifiedChanged; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class EditablePropertyBase<T> where T : IComparable | |
{ | |
#region ---- private Properties ---- | |
private T oldValue; | |
private T newValue; | |
#endregion | |
#region ---- protected Constructors ---- | |
protected EditablePropertyBase(T value) { | |
this.oldValue = value; | |
this.newValue = value; | |
} | |
#endregion | |
#region ---- public Properties ---- | |
public T Value | |
{ | |
get => this.newValue; | |
set | |
{ | |
this.newValue = value; | |
if (this.IsModified) | |
ExecuteOnModifiedChanged(); | |
ExecuteOnPropertyChanged(); | |
} | |
} | |
protected T OldValue { get => this.oldValue; } | |
public virtual bool IsValid => ValidationRules != null && !ValidationRules.Any(rule => !rule.Key()); | |
public virtual bool IsModified => !oldValue.Equals(newValue); | |
public virtual bool IsModifiedAndValid { get => IsValid && IsModified; } | |
#endregion | |
#region ---- protected Properties ---- | |
protected abstract Dictionary<Func<bool>, T> ValidationRules { get; } | |
#endregion | |
#region ---- Validation ---- | |
public virtual void ValidationIsGreaterAs(T compareValue) | |
{ | |
this.ValidationRules.Add(() => this.Value.CompareTo(compareValue) == 1, compareValue); | |
} | |
public virtual void ValidationIsSmallerAs(T compareValue) | |
{ | |
this.ValidationRules.Add(() => this.Value.CompareTo(compareValue) == -1, compareValue); | |
} | |
public virtual void ValidationIsEquel(T compareValue) | |
{ | |
this.ValidationRules.Add(() => this.Value.CompareTo(compareValue) == 0, compareValue); | |
} | |
public virtual void ValidationIsNotEquel(T compareValue) | |
{ | |
this.ValidationRules.Add(() => this.Value.CompareTo(compareValue) != 0, compareValue); | |
} | |
#endregion | |
#region ---- public Actions/Methods ---- | |
public Action ExecuteOnPropertyChanged = () => {}; | |
public Action ExecuteOnModifiedChanged = () => {}; | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment