diff options
Diffstat (limited to 'Software/Visual_Studio/Tango.BL/ObservableEntityDTO.cs')
| -rw-r--r-- | Software/Visual_Studio/Tango.BL/ObservableEntityDTO.cs | 145 |
1 files changed, 11 insertions, 134 deletions
diff --git a/Software/Visual_Studio/Tango.BL/ObservableEntityDTO.cs b/Software/Visual_Studio/Tango.BL/ObservableEntityDTO.cs index 1c3edac82..5950a3cd2 100644 --- a/Software/Visual_Studio/Tango.BL/ObservableEntityDTO.cs +++ b/Software/Visual_Studio/Tango.BL/ObservableEntityDTO.cs @@ -1,82 +1,39 @@ -using LiteDB; -using System; +using System; using System.Collections; using System.Collections.Generic; -using System.Diagnostics; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; -using Tango.BL.ActionLogs; -using Tango.BL.ValueObjects; -using Tango.Core.ExtensionMethods; namespace Tango.BL { - /// <summary> - /// Represents an auto generated observable entity transport object mirror. - /// </summary> - /// <typeparam name="DTO">The type of to.</typeparam> - /// <typeparam name="T"></typeparam> - /// <seealso cref="Tango.BL.IObservableEntityDTO" /> - /// <seealso cref="System.IEquatable{T}" /> - /// <seealso cref="Tango.BL.ActionLogs.IActionLogComparable{DTO}" /> - public abstract class ObservableEntityDTO<DTO, T> : IObservableEntityDTO, IEquatable<T>, IActionLogComparable where T : IObservableEntity where DTO : ObservableEntityDTO<DTO, T> + public abstract class ObservableEntityDTO<DTO, T> : IObservableEntityDTO, IEquatable<T> where T : IObservableEntity where DTO : ObservableEntityDTO<DTO, T> { - /// <summary> - /// Gets or sets the ID of the entity. - /// </summary> public int ID { get; set; } - /// <summary> - /// Gets or sets the unique identifier of the entity. - /// </summary> - [BsonId] public String Guid { get; set; } - /// <summary> - /// Gets or sets the last updated date of the entity. - /// </summary> public DateTime LastUpdated { get; set; } - /// <summary> - /// Creates an instance of type <see cref="DTO"/> for the specified entity of type <see cref="T"/>. - /// </summary> - /// <param name="observable">The observable.</param> - /// <returns></returns> public static DTO FromObservable(T observable) { - return FromObservableInternal<DTO>(observable); - } - - /// <summary> - /// Creates an instance of type <see cref="DTOResult"/> for the specified entity of type <see cref="T"/>. - /// </summary> - /// <param name="observable">The observable.</param> - /// <returns></returns> - public static DTOResult FromObservable<DTOResult>(T observable) where DTOResult : DTO - { - return FromObservableInternal<DTOResult>(observable); - } - - internal static DTOResult FromObservableInternal<DTOResult>(T observable) where DTOResult : DTO - { if (observable == null) return null; - var dto = Activator.CreateInstance<DTOResult>(); + var dto = Activator.CreateInstance<DTO>(); - foreach (var prop in typeof(DTOResult).GetProperties()) + foreach (var prop in typeof(DTO).GetProperties()) { var observableProp = typeof(T).GetProperty(prop.Name); if (observableProp != null) { - if (prop.PropertyType.IsValueTypeOrString() || prop.PropertyType == typeof(byte[])) + if (prop.PropertyType.IsPrimitive || prop.PropertyType.IsValueType || prop.PropertyType == typeof(String)) { prop.SetValue(dto, observableProp.GetValue(observable)); } else if (!prop.PropertyType.IsGenericType) { - prop.SetValue(dto, prop.PropertyType.GetMethod(nameof(FromObservableInternal), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy).MakeGenericMethod(prop.PropertyType).Invoke(null, new object[] { observableProp.GetValue(observable) })); + prop.SetValue(dto, prop.PropertyType.GetMethod(nameof(FromObservable), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy).Invoke(null, new object[] { observableProp.GetValue(observable) })); } else { @@ -85,36 +42,17 @@ namespace Tango.BL foreach (var item in source) { - collection.Add(prop.PropertyType.GenericTypeArguments[0].GetMethod(nameof(FromObservableInternal), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy).MakeGenericMethod(prop.PropertyType.GenericTypeArguments[0]).Invoke(null, new object[] { item })); + collection.Add(prop.PropertyType.GenericTypeArguments[0].GetMethod(nameof(FromObservable), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy).Invoke(null, new object[] { item })); } prop.SetValue(dto, collection); } } - else if (prop.GetCustomAttribute<ObservableDTOPropertyAttribute>() != null) - { - var att = prop.GetCustomAttribute<ObservableDTOPropertyAttribute>(); - - try - { - prop.SetValue(dto, observable.GetPropertyValueByPath(att.MapsTo)); - } - catch - { - Debug.WriteLine($"Error mapping '{typeof(DTOResult).Name}.{prop.PropertyType}' to '{typeof(T)}.{att.MapsTo}'."); - } - } } - dto.OnFromObservableCompleted(observable); - return dto; } - /// <summary> - /// Creates an entity of type <see cref="T"/> from this DTO. - /// </summary> - /// <returns></returns> public T ToObservable() { T observable = Activator.CreateInstance<T>(); @@ -122,18 +60,14 @@ namespace Tango.BL return observable; } - /// <summary> - /// Maps this instance to an instance of <see cref="T"/>. - /// </summary> - /// <param name="observable">The observable.</param> - public virtual void MapToObservable(T observable) + public void MapToObservable(T observable) { - foreach (var prop in GetType().GetProperties()) + foreach (var prop in typeof(DTO).GetProperties()) { var observableProp = typeof(T).GetProperty(prop.Name); if (observableProp != null) { - if (prop.PropertyType.IsValueTypeOrString() || prop.PropertyType == typeof(byte[])) + if (prop.PropertyType.IsPrimitive || prop.PropertyType.IsValueType || prop.PropertyType == typeof(String)) { observableProp.SetValue(observable, prop.GetValue(this)); } @@ -194,11 +128,6 @@ namespace Tango.BL } } - /// <summary> - /// Returns true if this instance is equal in terms of values (not references) to the specified entity. - /// </summary> - /// <param name="observable">The observable.</param> - /// <returns></returns> public bool EqualsToObservable(T observable) { if (observable == null) return false; @@ -208,7 +137,7 @@ namespace Tango.BL var observableProp = typeof(T).GetProperty(prop.Name); if (observableProp != null) { - if (prop.PropertyType.IsValueTypeOrString() || prop.PropertyType == typeof(byte[])) + if (prop.PropertyType.IsPrimitive || prop.PropertyType.IsValueType || prop.PropertyType == typeof(String)) { var observableValue = observableProp.GetValue(observable); var dtoValue = prop.GetValue(this); @@ -274,61 +203,9 @@ namespace Tango.BL return true; } - /// <summary> - /// Returns true if this instance is equal in terms of values (not references) to the specified entity. - /// </summary> - /// <param name="observable">The observable.</param> - /// <returns></returns> public bool Equals(T observable) { return EqualsToObservable(observable); } - - /// <summary> - /// Returns true if the specified property should be ignored during ActionLog comparison. - /// </summary> - /// <param name="propName">Name of the property.</param> - /// <returns></returns> - bool IActionLogComparable.ShouldActionLogIgnore(string propName) - { - return propName == nameof(LastUpdated) || OnShouldActionLogIgnore(propName); - } - - /// <summary> - /// override to specified properties to be ignored when doing ActionLog comparison. - /// </summary> - /// <param name="propName">Name of the property.</param> - /// <returns></returns> - protected virtual bool OnShouldActionLogIgnore(string propName) - { - return false; - } - - /// <summary> - /// Returns an optional custom name for this instance in the comparison tree. - /// </summary> - /// <returns></returns> - string IActionLogComparable.GetActionLogName() - { - return OnGetActionLogName(); - } - - /// <summary> - /// override to specified a custom name for this instance in the ActionLog comparison tree. - /// </summary> - /// <returns></returns> - protected virtual String OnGetActionLogName() - { - return this.GetType().Name; - } - - /// <summary> - /// Called when the static method <see cref="FromObservable(T)"/> completes. - /// </summary> - /// <param name="observable">The observable.</param> - protected virtual void OnFromObservableCompleted(T observable) - { - //Just for override - } } } |
