Skip to content

Commit a041827

Browse files
authored
Merge pull request #5 from luismts/version-1.2
Version 1.2
2 parents 703439e + 3af272e commit a041827

32 files changed

+1110
-38
lines changed
Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
using Plugin.ValidationRules;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Text;
2+
using Plugin.ValidationRules.Extensions;
3+
using Plugin.ValidationRules.Interfaces;
54
using ValidationRulesTest.Validations;
65

76
namespace ValidationRulesTest.Models
87
{
9-
public class UserValidator
8+
public class UserValidator : IMapperValidator<User>
109
{
1110
ValidationUnit _unit1;
1211

1312
public UserValidator()
1413
{
15-
LastName = new ValidatableObject<string>();
16-
Name = new ValidatableObject<string>();
17-
Email = new ValidatableObject<string>();
14+
LastName = new Validatable<string>();
15+
Name = new Validatable<string>();
16+
Email = new Validatable<string>();
1817

1918
_unit1 = new ValidationUnit(Name, LastName, Email);
2019

@@ -29,24 +28,47 @@ public UserValidator()
2928
Email.Validations.Add(new EmailRule());
3029
}
3130

32-
public ValidatableObject<string> LastName { get; set; }
33-
public ValidatableObject<string> Name { get; set; }
34-
public ValidatableObject<string> Email { get; set; }
31+
public Validatable<string> LastName { get; set; }
32+
public Validatable<string> Name { get; set; }
33+
public Validatable<string> Email { get; set; }
3534

3635
public bool Validate()
3736
{
3837
// Your logic goes here
3938
return _unit1.Validate();
4039
}
4140

42-
public User Cast()
41+
public User Map()
4342
{
44-
return new User
43+
var stopper = new System.Diagnostics.Stopwatch();
44+
var testRuns = 1000; // 1 second
45+
46+
stopper.Start();
47+
48+
// Simple Manual Mapper
49+
var manualMapperUser = new User
4550
{
4651
Name = this.Name.Value,
4752
LastName = this.LastName.Value,
4853
Email = this.Email.Value
4954
};
55+
56+
stopper.Stop();
57+
58+
var time1 = stopper.Elapsed.TotalMilliseconds / (double)testRuns;
59+
System.Console.WriteLine("ManualMapper: " + time1); // Elapsed time: 0.002
60+
61+
stopper.Restart();
62+
63+
// Extension Mapper with simple Model
64+
var extMapperUser = this.MapValidator<User, UserValidator>();
65+
66+
stopper.Stop();
67+
68+
var time2 = stopper.Elapsed.TotalMilliseconds / (double)testRuns;
69+
System.Console.WriteLine("ExtensionMapper: " + time2); // Elapsed time: 0.013
70+
71+
return manualMapperUser;
5072
}
5173
}
5274
}

samples/ValidationRulesTest/ValidationRulesTest/ViewModels/Example1ViewModel.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ public class Example1ViewModel
99

1010
public Example1ViewModel()
1111
{
12-
Name = new ValidatableObject<string>();
13-
LastName = new ValidatableObject<string>();
14-
Email = new ValidatableObject<string>();
12+
Name = new Validatable<string>();
13+
LastName = new Validatable<string>();
14+
Email = new Validatable<string>();
1515

1616
_unit1 = new ValidationUnit(Name, LastName, Email);
1717

1818
AddValidations();
1919
}
2020

21-
public ValidatableObject<string> LastName { get; set; }
22-
public ValidatableObject<string> Name { get; set; }
23-
public ValidatableObject<string> Email { get; set; }
21+
public Validatable<string> LastName { get; set; }
22+
public Validatable<string> Name { get; set; }
23+
public Validatable<string> Email { get; set; }
2424

2525

2626
private void AddValidations()

samples/ValidationRulesTest/ValidationRulesTest/ViewModels/Example2ViewModel.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ public class Example2ViewModel : ExtendedPropertyChanged
99
{
1010
public Example2ViewModel()
1111
{
12-
_user = new ValidatableObject<User>();
12+
_user = new Validatable<User>();
1313
_user.Value = new User();
1414

1515
AddValidations();
1616
}
1717

18-
private ValidatableObject<User> _user;
19-
public ValidatableObject<User> User
18+
private Validatable<User> _user;
19+
public Validatable<User> User
2020
{
2121
get => _user;
2222
set => SetProperty(ref _user, value);

samples/ValidationRulesTest/ValidationRulesTest/ViewModels/Example3ViewModel.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public UserValidator User
2222

2323
public bool Validate()
2424
{
25+
User modelUser = User.Map();
26+
2527
// Your logic goes here
2628
return User.Validate();
2729
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System;
2+
using System.Reflection;
3+
4+
namespace Plugin.ValidationRules.Extensions
5+
{
6+
public static class Extensions
7+
{
8+
/// <summary>
9+
/// Capitalize the first character and add a space before each capitalized letter (except the first character).
10+
/// </summary>
11+
/// <param name="the_string"></param>
12+
/// <returns></returns>
13+
public static string ToCapitalizeCase(this string the_string)
14+
{
15+
// If there are 0 or 1 characters, just return the string.
16+
if (the_string == null) return the_string;
17+
if (the_string.Length < 2) return the_string.ToUpper();
18+
19+
// Start with the first character.
20+
string result = the_string.Substring(0, 1).ToUpper();
21+
22+
// Add the remaining characters.
23+
for (int i = 1; i < the_string.Length; i++)
24+
{
25+
if (char.IsUpper(the_string[i])) result += " ";
26+
result += the_string[i];
27+
}
28+
29+
return result;
30+
}
31+
32+
public static Model MapValidator<Model, Validator>(this Validator validator) where Model : new()
33+
{
34+
if (validator == null)
35+
return default(Model);
36+
37+
Model newModel = new Model();
38+
39+
Type modelObjectType = newModel.GetType();
40+
PropertyInfo[] modelPropList = modelObjectType.GetProperties();
41+
42+
Type validatorType = validator.GetType();
43+
PropertyInfo[] validatorPropList = validatorType.GetProperties();
44+
45+
foreach (PropertyInfo validatorPropInfo in validatorPropList)
46+
{
47+
foreach (PropertyInfo modelPropInfo in modelPropList)
48+
{
49+
if (modelPropInfo.Name == validatorPropInfo.Name)
50+
{
51+
try
52+
{
53+
PropertyInfo validatorProp = validatorPropInfo.PropertyType.GetProperty(nameof(Validatable<string>.Value));
54+
55+
if (validatorProp == null)
56+
break;
57+
58+
var validatorPropValue = validatorPropInfo.GetValue(validator); // Not working directly
59+
var propValue = validatorProp.GetValue(validatorPropValue, null);
60+
61+
modelPropInfo.SetValue(newModel, propValue, null);
62+
}
63+
catch (Exception) { }
64+
65+
break;
66+
}
67+
}
68+
}
69+
70+
return newModel;
71+
}
72+
}
73+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.Windows.Input;
3+
4+
namespace Plugin.ValidationRules.Extensions
5+
{
6+
public class RelayCommand : ICommand
7+
{
8+
readonly Func<object, bool> _canExecute;
9+
readonly Action<object> _execute;
10+
11+
public RelayCommand(Action<object> execute)
12+
{
13+
if (execute == null)
14+
throw new ArgumentNullException(nameof(execute));
15+
16+
_execute = execute;
17+
}
18+
19+
public RelayCommand(Action execute) : this(o => execute())
20+
{
21+
if (execute == null)
22+
throw new ArgumentNullException(nameof(execute));
23+
}
24+
25+
public RelayCommand(Action<object> execute, Func<object, bool> canExecute) : this(execute)
26+
{
27+
if (canExecute == null)
28+
throw new ArgumentNullException(nameof(canExecute));
29+
30+
_canExecute = canExecute;
31+
}
32+
33+
public RelayCommand(Action execute, Func<bool> canExecute) : this(o => execute(), o => canExecute())
34+
{
35+
if (execute == null)
36+
throw new ArgumentNullException(nameof(execute));
37+
if (canExecute == null)
38+
throw new ArgumentNullException(nameof(canExecute));
39+
}
40+
41+
public bool CanExecute(object parameter)
42+
{
43+
if (_canExecute != null)
44+
return _canExecute(parameter);
45+
46+
return true;
47+
}
48+
49+
public void Execute(object parameter)
50+
{
51+
_execute(parameter);
52+
}
53+
54+
public event EventHandler CanExecuteChanged;
55+
56+
public void ChangeCanExecute() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);
57+
}
58+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
3+
namespace Plugin.ValidationRules.Extensions
4+
{
5+
public class ValueChangedEventArgs<T> : EventArgs
6+
{
7+
public T OldValue { get; set; }
8+
public T NewValue { get; set; }
9+
}
10+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Plugin.ValidationRules.Interfaces;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace Plugin.ValidationRules.Formatters
7+
{
8+
public class BoolNegationFormatter : IValueFormatter<bool>
9+
{
10+
public bool TrueValue { get; set; }
11+
public bool FalseValue { get; set; }
12+
public bool IsInverted { get; set; }
13+
14+
public bool Format(bool value)
15+
{
16+
var returnValue = this.FalseValue;
17+
18+
if (value is bool boolValue)
19+
{
20+
if (this.IsInverted)
21+
{
22+
returnValue = boolValue ? this.FalseValue : this.TrueValue;
23+
}
24+
else
25+
{
26+
returnValue = boolValue ? this.TrueValue : this.FalseValue;
27+
}
28+
}
29+
30+
return returnValue;
31+
}
32+
}
33+
34+
public class InverseBoolFormatter : BoolNegationFormatter
35+
{
36+
}
37+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using Plugin.ValidationRules.Interfaces;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace Plugin.ValidationRules.Formatters
7+
{
8+
public class MaskFormatter : IValueFormatter<string>
9+
{
10+
IDictionary<int, char> _positions;
11+
string _oldValue;
12+
13+
public MaskFormatter(string mask)
14+
{
15+
Mask = mask;
16+
SetPositions();
17+
}
18+
19+
public string Mask { get; private set; }
20+
21+
public string Format(string text)
22+
{
23+
if (text == _oldValue)
24+
return text;
25+
26+
_oldValue = text;
27+
28+
if (string.IsNullOrWhiteSpace(text) || _positions == null)
29+
return text;
30+
31+
if (text.Length > Mask.Length)
32+
return text.Remove(text.Length - 1);
33+
34+
foreach (var position in _positions)
35+
{
36+
if (text.Length >= position.Key + 1)
37+
{
38+
var value = position.Value.ToString();
39+
if (text.Substring(position.Key, 1) != value)
40+
text = text.Insert(position.Key, value);
41+
}
42+
}
43+
44+
return text;
45+
}
46+
47+
48+
void SetPositions()
49+
{
50+
if (string.IsNullOrEmpty(Mask))
51+
{
52+
_positions = null;
53+
return;
54+
}
55+
56+
var list = new Dictionary<int, char>();
57+
for (var i = 0; i < Mask.Length; i++)
58+
if (Mask[i] != 'X')
59+
list.Add(i, Mask[i]);
60+
61+
_positions = list;
62+
}
63+
64+
public void ChangeMask(string mask) => Mask = mask;
65+
}
66+
}

0 commit comments

Comments
 (0)