aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.BL/ObservableEntity.cs
diff options
context:
space:
mode:
authorRoy Ben-Shabat <Roy@Twine-s.com>2018-08-01 10:29:09 +0300
committerRoy Ben-Shabat <Roy@Twine-s.com>2018-08-01 10:29:09 +0300
commitd5c6067365e12674e95acaef4c4af45eaead8c3e (patch)
tree0345d7e407a3e714061dc9bc26152c9efe977bd9 /Software/Visual_Studio/Tango.BL/ObservableEntity.cs
parent91c672c0b0b01bc68a6adfa2aada337c6488a614 (diff)
downloadTango-d5c6067365e12674e95acaef4c4af45eaead8c3e.tar.gz
Tango-d5c6067365e12674e95acaef4c4af45eaead8c3e.zip
Implemented notification for observables entities modification..
Diffstat (limited to 'Software/Visual_Studio/Tango.BL/ObservableEntity.cs')
-rw-r--r--Software/Visual_Studio/Tango.BL/ObservableEntity.cs226
1 files changed, 162 insertions, 64 deletions
diff --git a/Software/Visual_Studio/Tango.BL/ObservableEntity.cs b/Software/Visual_Studio/Tango.BL/ObservableEntity.cs
index 4f002c073..fc037f16a 100644
--- a/Software/Visual_Studio/Tango.BL/ObservableEntity.cs
+++ b/Software/Visual_Studio/Tango.BL/ObservableEntity.cs
@@ -26,6 +26,19 @@ using System.ComponentModel;
namespace Tango.BL
{
+ internal class ObservableEntitiesContainer
+ {
+ static ObservableEntitiesContainer()
+ {
+ RegisteredEntities = new List<IObservableEntity>();
+ }
+
+ /// <summary>
+ /// Gets the list of all registered entities ever created.
+ /// </summary>
+ internal static List<IObservableEntity> RegisteredEntities { get; private set; }
+ }
+
/// <summary>
/// Represents a generic observable entity base class.
/// </summary>
@@ -35,13 +48,16 @@ namespace Tango.BL
[Serializable]
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.
+ #region Events
/// <summary>
- /// Occurs after this observable has been saved.
+ /// Occurs after this observable has been modified and saved by this entity context or another.
/// </summary>
- public event EventHandler Saved;
+ public event EventHandler<ObservableModifiedEventArgs> Modified;
+
+ #endregion
+
+ #region Properties
private Int32 _id;
/// <summary>
@@ -117,19 +133,25 @@ namespace Tango.BL
get { return GetType(); }
}
+ #endregion
+
+ #region Constructors
+
/// <summary>
/// Initializes a new instance of the <see cref="ObservableEntity{T}"/> class.
/// </summary>
public ObservableEntity()
{
+ ObservableEntitiesContainer.RegisteredEntities.Add(this);
+
Guid = System.Guid.NewGuid().ToString();
LastUpdated = DateTime.UtcNow;
- regExDAL = new Regex(@"
- (?<=[A-Z])(?=[A-Z][a-z]) |
- (?<=[^A-Z])(?=[A-Z]) |
- (?<=[A-Za-z])(?=[^A-Za-z])", RegexOptions.IgnorePatternWhitespace);
}
+ #endregion
+
+ #region Public Methods
+
/// <summary>
/// Saves the changes on this entity to database.
/// </summary>
@@ -155,7 +177,6 @@ namespace Tango.BL
{
context.SaveChanges();
}
- OnSaved();
}
/// <summary>
@@ -246,6 +267,11 @@ Maybe you have deleted an entity that was no yet inserted into database?", LogCa
});
}
+ /// <summary>
+ /// Gets the database set.
+ /// </summary>
+ /// <param name="context">The context.</param>
+ /// <returns></returns>
public DbSet<T> GetDbSet(ObservablesContext context)
{
String tabelName = this.GetType().Name.PluralizeMVC();
@@ -269,12 +295,77 @@ Maybe you have deleted an entity that was no yet inserted into database?", LogCa
return null;
}
- protected virtual void OnSaved()
+ /// <summary>
+ /// Gets the database set.
+ /// </summary>
+ /// <typeparam name="T1">The type of the 1.</typeparam>
+ /// <param name="context">The context.</param>
+ /// <returns></returns>
+ public DbSet<T1> GetDbSet<T1>(ObservablesContext context) where T1 : class, IObservableEntity
{
- Saved?.Invoke(this, new EventArgs());
+ return GetDbSet(context) as DbSet<T1>;
}
- public static DbContext GetDbContextFromEntity(object entity)
+ /// <summary>
+ /// Clones this entity.
+ /// </summary>
+ /// <returns></returns>
+ public virtual T Clone()
+ {
+ var cloned = Activator.CreateInstance<T>();
+
+ foreach (var prop in typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.SetMethod != null))
+ {
+ if (!prop.PropertyType.IsGenericType)
+ {
+ prop.SetValue(cloned, prop.GetValue(this));
+ }
+ }
+
+ cloned.ID = 0;
+ cloned.Guid = System.Guid.NewGuid().ToString();
+ cloned.LastUpdated = DateTime.UtcNow;
+ return cloned;
+ }
+
+ /// <summary>
+ /// Compares another entity using a a JSON string comparison.
+ /// </summary>
+ /// <param name="entity">The entity.</param>
+ /// <returns></returns>
+ public bool CompareUsingJson(T entity)
+ {
+ String me = JsonConvert.SerializeObject(this);
+ String other = JsonConvert.SerializeObject(entity);
+ return me == other;
+ }
+
+ /// <summary>
+ /// Deletes this entity using an SQL statement which will cause the database delete cascade effect.
+ /// </summary>
+ /// <param name="context"></param>
+ /// <returns></returns>
+ public Task DeleteCascadeAsync(ObservablesContext context)
+ {
+ return context.Database.ExecuteSqlCommandAsync(String.Format("DELETE FROM {0} WHERE GUID='{1}'", this.GetType().GetCustomAttribute<TableAttribute>().Name, Guid));
+ }
+
+ /// <summary>
+ /// Raises the <see cref="Modified" /> event.
+ /// </summary>
+ /// <param name="context"></param>
+ /// <param name="source"></param>
+ /// <param name="target"></param>
+ public void RaiseModified(ObservablesContext context, IObservableEntity source, IObservableEntity target)
+ {
+ Modified?.Invoke(this, new ObservableModifiedEventArgs(context, source, target));
+ }
+
+ #endregion
+
+ #region Private Methods
+
+ private static DbContext GetDbContextFromEntity(object entity)
{
var object_context = GetObjectContextFromEntity(entity);
@@ -298,26 +389,52 @@ Maybe you have deleted an entity that was no yet inserted into database?", LogCa
return context;
}
- public virtual T Clone()
+ #endregion
+
+ #region Validation
+
+ //Holds the current validation errors.
+ private List<KeyValuePair<String, String>> _currentErrors = new List<KeyValuePair<string, string>>();
+
+ /// <summary>
+ /// Occurs when the validation errors have changed for a property or for the entire entity.
+ /// </summary>
+ public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
+
+ private ObservableCollection<String> _validationErrors;
+ /// <summary>
+ /// Gets or sets the validation errors.
+ /// </summary>
+ [NotMapped]
+ [ParameterIgnore]
+ [XmlIgnore]
+ public ObservableCollection<String> ValidationErrors
{
- return (this as T).CloneEntity() as T;
+ get { return _validationErrors; }
+ protected set { _validationErrors = value; RaisePropertyChangedAuto(); }
}
- public bool CompareUsingJson(T entity)
+ private bool _validateOnPropertyChanged;
+ [NotMapped]
+ [ParameterIgnore]
+ [XmlIgnore]
+ public bool ValidateOnPropertyChanged
{
- String me = JsonConvert.SerializeObject(this);
- String other = JsonConvert.SerializeObject(entity);
- return me == other;
+ get { return _validateOnPropertyChanged; }
+ set { _validateOnPropertyChanged = value; RaisePropertyChangedAuto(); }
}
+ private bool _hasErrors;
/// <summary>
- /// Deletes this entity using an SQL statement which will cause the database delete cascade effect.
+ /// Gets a value that indicates whether the entity has validation errors.
/// </summary>
- /// <param name="context"></param>
- /// <returns></returns>
- public Task DeleteCascadeAsync(ObservablesContext context)
+ [NotMapped]
+ [ParameterIgnore]
+ [XmlIgnore]
+ public bool HasErrors
{
- return context.Database.ExecuteSqlCommandAsync(String.Format("DELETE FROM {0} WHERE GUID='{1}'", this.GetType().GetCustomAttribute<TableAttribute>().Name, Guid));
+ get { return _hasErrors; }
+ set { _hasErrors = value; RaisePropertyChangedAuto(); }
}
/// <summary>
@@ -357,37 +474,36 @@ Maybe you have deleted an entity that was no yet inserted into database?", LogCa
return !HasErrors;
}
- public DbSet<T1> GetDbSet<T1>(ObservablesContext context) where T1 : class, IObservableEntity
- {
- return GetDbSet(context) as DbSet<T1>;
- }
-
+ /// <summary>
+ /// Gets the validation errors for a specified property or for the entire entity.
+ /// </summary>
+ /// <param name="propertyName">The name of the property to retrieve validation errors for; or null or <see cref="F:System.String.Empty" />, to retrieve entity-level errors.</param>
+ /// <returns>
+ /// The validation errors for the property or entity.
+ /// </returns>
public IEnumerable GetErrors(string propertyName)
{
return _currentErrors.Where(x => x.Key == propertyName).Select(x => x.Value).ToList();
}
+ /// <summary>
+ /// Called when entity is validating.
+ /// </summary>
protected virtual void OnValidating(ObservablesContext context)
{
}
+ /// <summary>
+ /// Inserts the error.
+ /// </summary>
+ /// <param name="propName">Name of the property.</param>
+ /// <param name="error">The error.</param>
protected void InsertError(String propName, String error)
{
_currentErrors.Add(new KeyValuePair<string, string>(propName, error));
}
- private bool _validateOnPropertyChanged;
- [NotMapped]
- [ParameterIgnore]
- [XmlIgnore]
- public bool ValidateOnPropertyChanged
- {
- get { return _validateOnPropertyChanged; }
- set { _validateOnPropertyChanged = value; RaisePropertyChangedAuto(); }
- }
-
-
/// <summary>
/// Invoked the <see cref="ErrorsChanged"/> event.
/// </summary>
@@ -397,34 +513,14 @@ Maybe you have deleted an entity that was no yet inserted into database?", LogCa
ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(propName));
}
- private bool _hasErrors;
- /// <summary>
- /// Gets a value that indicates whether the entity has validation errors.
- /// </summary>
- [NotMapped]
- [ParameterIgnore]
- [XmlIgnore]
- public bool HasErrors
- {
- get { return _hasErrors; }
- set { _hasErrors = value; RaisePropertyChangedAuto(); }
- }
+ #endregion
+
+ #region INotify Property Changed
- private ObservableCollection<String> _validationErrors;
/// <summary>
- /// Gets or sets the validation errors.
+ /// Raises the property changed event.
/// </summary>
- [NotMapped]
- [ParameterIgnore]
- [XmlIgnore]
- public ObservableCollection<String> ValidationErrors
- {
- get { return _validationErrors; }
- protected set { _validationErrors = value; RaisePropertyChangedAuto(); }
- }
-
- public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
-
+ /// <param name="propName">Name of the property.</param>
protected override void RaisePropertyChanged(string propName)
{
base.RaisePropertyChanged(propName);
@@ -435,6 +531,8 @@ Maybe you have deleted an entity that was no yet inserted into database?", LogCa
}
}
+ #endregion
+
#region Operator Overloading
//public static bool operator ==(ObservableEntity<T> observable1, ObservableEntity<T> observable2)