aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.BL
diff options
context:
space:
mode:
authorRoy Ben-Shabat <Roy@Twine-s.com>2018-07-16 15:51:33 +0300
committerRoy Ben-Shabat <Roy@Twine-s.com>2018-07-16 15:51:33 +0300
commit13e34402f91fae6229b2d9719ddb48ced1d37fbf (patch)
tree1b60f7f1f719403ede6f3ebbfe6077ca673fe792 /Software/Visual_Studio/Tango.BL
parentc326bf5d9c1290ecc79739a1938c0a75b276f552 (diff)
downloadTango-13e34402f91fae6229b2d9719ddb48ced1d37fbf.tar.gz
Tango-13e34402f91fae6229b2d9719ddb48ced1d37fbf.zip
Some fixed and improvements.
Diffstat (limited to 'Software/Visual_Studio/Tango.BL')
-rw-r--r--Software/Visual_Studio/Tango.BL/EntitiesExtensions/Contact.cs8
-rw-r--r--Software/Visual_Studio/Tango.BL/EntitiesExtensions/Job.cs14
-rw-r--r--Software/Visual_Studio/Tango.BL/EntitiesExtensions/User.cs25
-rw-r--r--Software/Visual_Studio/Tango.BL/IObservableEntity.cs10
-rw-r--r--Software/Visual_Studio/Tango.BL/ObservableEntity.cs98
5 files changed, 128 insertions, 27 deletions
diff --git a/Software/Visual_Studio/Tango.BL/EntitiesExtensions/Contact.cs b/Software/Visual_Studio/Tango.BL/EntitiesExtensions/Contact.cs
index 26f0329b0..8dcfda32d 100644
--- a/Software/Visual_Studio/Tango.BL/EntitiesExtensions/Contact.cs
+++ b/Software/Visual_Studio/Tango.BL/EntitiesExtensions/Contact.cs
@@ -34,18 +34,18 @@ namespace Tango.BL.Entities
FullName = FirstName + " " + LastName;
}
- public override void Validate(ObservablesContext context)
+ protected override void OnValidating(ObservablesContext context)
{
- base.Validate(context);
+ base.OnValidating(context);
if (FirstName.IsNullOrWhiteSpace() || LastName.IsNullOrWhiteSpace())
{
- throw new ArgumentException("Contact first name and last name was not provided.");
+ InsertError(nameof(FirstName), "Contact first name and last name was not provided.");
}
if (!Email.IsEmail())
{
- throw new ArgumentException("Invalid contact email format.");
+ InsertError(nameof(Email), "Invalid contact email format.");
}
}
}
diff --git a/Software/Visual_Studio/Tango.BL/EntitiesExtensions/Job.cs b/Software/Visual_Studio/Tango.BL/EntitiesExtensions/Job.cs
index 0c5a72bd8..e5144386a 100644
--- a/Software/Visual_Studio/Tango.BL/EntitiesExtensions/Job.cs
+++ b/Software/Visual_Studio/Tango.BL/EntitiesExtensions/Job.cs
@@ -379,5 +379,19 @@ namespace Tango.BL.Entities
}
#endregion
+
+ #region Validation
+
+ protected override void OnValidating(ObservablesContext context)
+ {
+ base.OnValidating(context);
+
+ if (String.IsNullOrWhiteSpace(Name))
+ {
+ InsertError(nameof(Name), "Job name is required");
+ }
+ }
+
+ #endregion
}
}
diff --git a/Software/Visual_Studio/Tango.BL/EntitiesExtensions/User.cs b/Software/Visual_Studio/Tango.BL/EntitiesExtensions/User.cs
index 0d1e864ee..6c1b03145 100644
--- a/Software/Visual_Studio/Tango.BL/EntitiesExtensions/User.cs
+++ b/Software/Visual_Studio/Tango.BL/EntitiesExtensions/User.cs
@@ -85,37 +85,34 @@ namespace Tango.BL.Entities
}
}
- public override void Validate(ObservablesContext context)
+ public override bool Validate(ObservablesContext context)
{
+ return base.Validate(context) && Contact.Validate(context) && Address.Validate(context);
+ }
+
+ protected override void OnValidating(ObservablesContext context)
+ {
+ base.OnValidating(context);
+
var users = context.Users.ToList();
if (users.Exists(x => x.Guid != Guid && x.Email.ToLower() == Email.ToLower()))
{
- throw new ArgumentException("The specified email is already taken by another account.");
+ InsertError(nameof(Email), "The specified email is already taken by another account.");
}
if (!Email.IsEmail())
{
- throw new ArgumentException("The specified email address is invalid.");
+ InsertError(nameof(Email), "The specified email address is invalid.");
}
if (_passwordGatewayModified)
{
if (!PasswordGateWay.IsBetweenLength(4, 30))
{
- throw new ArgumentException("A user password must be at least 4 characters long and maximum 30.");
+ InsertError(nameof(PasswordGateWay), "A user password must be at least 4 characters long and maximum 30.");
}
}
-
- if (Contact != null)
- {
- Contact.Validate(context);
- }
-
- if (Address != null)
- {
- Address.Validate(context);
- }
}
/// <summary>
diff --git a/Software/Visual_Studio/Tango.BL/IObservableEntity.cs b/Software/Visual_Studio/Tango.BL/IObservableEntity.cs
index dcb7ad5da..d83f3a6b2 100644
--- a/Software/Visual_Studio/Tango.BL/IObservableEntity.cs
+++ b/Software/Visual_Studio/Tango.BL/IObservableEntity.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
@@ -13,7 +14,7 @@ namespace Tango.BL
/// <summary>
/// Represents an observable database entity.
/// </summary>
- public interface IObservableEntity : IParameterized
+ public interface IObservableEntity : INotifyDataErrorInfo, IParameterized
{
/// <summary>
/// Occurs after this observable has been saved.
@@ -85,13 +86,6 @@ namespace Tango.BL
Task DeleteCascadeAsync(ObservablesContext context);
/// <summary>
- /// Performs entity field validation.
- /// Will throw an exception with the proper message if one of the fields is invalid.
- /// </summary>
- /// <param name="context">The context.</param>
- void Validate(ObservablesContext context);
-
- /// <summary>
/// Gets the database set containing this entity.
/// </summary>
/// <typeparam name="T"></typeparam>
diff --git a/Software/Visual_Studio/Tango.BL/ObservableEntity.cs b/Software/Visual_Studio/Tango.BL/ObservableEntity.cs
index 7c82d927b..99d7360ef 100644
--- a/Software/Visual_Studio/Tango.BL/ObservableEntity.cs
+++ b/Software/Visual_Studio/Tango.BL/ObservableEntity.cs
@@ -22,6 +22,7 @@ using Tango.Serialization;
using System.Xml.Serialization;
using Newtonsoft.Json;
using Tango.Logging;
+using System.ComponentModel;
namespace Tango.BL
{
@@ -35,6 +36,7 @@ namespace Tango.BL
public abstract class ObservableEntity<T> : ExtendedObject, IObservableEntity where T : class, IObservableEntity
{
private Regex regExDAL;
+ private List<KeyValuePair<String, String>> _currentErrors = new List<KeyValuePair<string, string>>(); //Holds the current validation errors.
/// <summary>
/// Occurs after this observable has been saved.
@@ -323,9 +325,36 @@ Maybe you have deleted an entity that was no yet inserted into database?", LogCa
/// Will throw an exception with the proper message if one of the fields is invalid.
/// </summary>
/// <param name="context">The context.</param>
- public virtual void Validate(ObservablesContext context)
+ public virtual bool Validate(ObservablesContext context)
{
+ HasErrors = false;
+ _currentErrors.Clear();
+ if (ValidationErrors == null)
+ {
+ ValidationErrors = new ObservableCollection<string>();
+ }
+
+ ValidationErrors.Clear();
+
+ OnValidating(context);
+
+ foreach (var prop in this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
+ {
+ foreach (var validation in prop.GetCustomAttributes<ValidationAttribute>())
+ {
+ if (!validation.IsValid(prop.GetValue(this)))
+ {
+ _currentErrors.Add(new KeyValuePair<string, string>(prop.Name, validation.ErrorMessage));
+ ValidationErrors.Add(validation.ErrorMessage);
+ }
+ }
+
+ HasErrors = _currentErrors.Count > 0;
+ RaiseError(prop.Name);
+ }
+
+ return !HasErrors;
}
public DbSet<T1> GetDbSet<T1>(ObservablesContext context) where T1 : class, IObservableEntity
@@ -333,6 +362,73 @@ Maybe you have deleted an entity that was no yet inserted into database?", LogCa
return GetDbSet(context) as DbSet<T1>;
}
+ public IEnumerable GetErrors(string propertyName)
+ {
+ return _currentErrors.Where(x => x.Key == propertyName).Select(x => x.Value).ToList();
+ }
+
+ protected virtual void OnValidating(ObservablesContext context)
+ {
+
+ }
+
+ protected void InsertError(String propName, String error)
+ {
+ _currentErrors.Add(new KeyValuePair<string, string>(propName, error));
+ }
+
+ private bool _validateOnPropertyChanged;
+ [NotMapped]
+ public bool ValidateOnPropertyChanged
+ {
+ get { return _validateOnPropertyChanged; }
+ set { _validateOnPropertyChanged = value; RaisePropertyChangedAuto(); }
+ }
+
+
+ /// <summary>
+ /// Invoked the <see cref="ErrorsChanged"/> event.
+ /// </summary>
+ /// <param name="propName">Name of the property.</param>
+ protected void RaiseError(String propName)
+ {
+ ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(propName));
+ }
+
+ private bool _hasErrors;
+ /// <summary>
+ /// Gets a value that indicates whether the entity has validation errors.
+ /// </summary>
+ [NotMapped]
+ public bool HasErrors
+ {
+ get { return _hasErrors; }
+ set { _hasErrors = value; RaisePropertyChangedAuto(); }
+ }
+
+ private ObservableCollection<String> _validationErrors;
+ /// <summary>
+ /// Gets or sets the validation errors.
+ /// </summary>
+ [NotMapped]
+ public ObservableCollection<String> ValidationErrors
+ {
+ get { return _validationErrors; }
+ protected set { _validationErrors = value; RaisePropertyChangedAuto(); }
+ }
+
+ public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
+
+ protected override void RaisePropertyChanged(string propName)
+ {
+ base.RaisePropertyChanged(propName);
+
+ if (ValidateOnPropertyChanged)
+ {
+ Validate(null);
+ }
+ }
+
#region Operator Overloading
//public static bool operator ==(ObservableEntity<T> observable1, ObservableEntity<T> observable2)