diff --git a/Extensions/XReferenceExtensions.cs b/Extensions/XReferenceExtensions.cs new file mode 100644 index 0000000..f9caa8d --- /dev/null +++ b/Extensions/XReferenceExtensions.cs @@ -0,0 +1,176 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using xCommons.Extensions; +using xDataService.Models; + +namespace xDataService.Extensions +{ + public static class XReferenceExtensions + { + /// + /// Converts an XReference to String Representation ... + /// + /// + /// + /// + /// + public static string ToXReferenceString( + this XReference source, + char? splitter = '_' + ) + { + // + var result = string.Empty; + + // + // Normalize Splitter ... + if (!splitter.HasValue) + { + splitter = '_'; + } + + // + if (!source.IsNullOrDefault() && source.Index >= 0 && !source.ReferencedTo.IsNull()) + { + result = $"{source.ProvidedFor}{splitter.Value}{source.ReferencedTo}{splitter.Value}{source.Index}"; + } + + // + return result; + } + + /// + /// Parse an String and Extract to XReference if it's Described ... + /// + /// + /// + /// + /// + public static XReference FromXReference( + this string source, + char? splitter = '_' + ) + { + // + XReference result = null; + + // + // Normalize Splitter ... + if (!splitter.HasValue) + { + splitter = '_'; + } + + // + if (!source.IsNullOrEmpty()) + { + // + var parts = source.Split(splitter.Value); + if (parts.Count() >= 3) + { + // + // Parse String Parts ... + result = new XReference + { + Index = (int)Convert.ChangeType(parts[2], typeof(int)), + ReferencedTo = (T)Convert.ChangeType(parts[1], typeof(T)), + ProvidedFor = (string)Convert.ChangeType(parts[0], typeof(string)), + }; + } + } + + // + return result; + } + + /// + /// Converts a Collection of XReference to it's String Representation ... + /// + /// + /// + /// + public static string ToXReferenceString(this IEnumerable> source) + { + // + var result = string.Empty; + + // + if (source.IsNull() && source.HasChild()) + { + // + var stringList = new List(); + foreach (var s in source) + { + // + var sr = ToXReferenceString(s); + if (!sr.IsNullOrEmpty()) + { + stringList.Add(sr); + } + } + + // + result = stringList.ToListString(); + } + + // + return result; + } + public static string ToXReferenceString(this ICollection> source) + { + // + var result = source + .AsEnumerable() + .ToXReferenceString(); + + // + return result; + } + public static string ToXReferenceString(this IList> source) + { + // + var result = source + .AsEnumerable() + .ToXReferenceString(); + + // + return result; + } + + /// + /// Parse an string to XReference Collection Representation ... + /// + /// + /// + /// + public static IList> ParseXReferenceList(this string source) + { + // + IList> result = new List>(); + + // + if (!source.IsNullOrEmpty()) + { + // + var stringList = source.ParseListString(); + if (!stringList.IsNull() && stringList.HasChild()) + { + // + foreach (var s in stringList) + { + // + var sr = FromXReference(s); + if (!sr.IsNullOrDefault()) + { + result.Add(sr); + } + } + } + } + + // + return result; + } + } +} \ No newline at end of file diff --git a/Interfaces/IXBaseReferencedProvider.cs b/Interfaces/IXBaseReferencedProvider.cs new file mode 100644 index 0000000..20b9819 --- /dev/null +++ b/Interfaces/IXBaseReferencedProvider.cs @@ -0,0 +1,129 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using xDataService.Models; +using xModels.Dtos; + +namespace xDataService.Interfaces +{ + /// + /// in Some Cases we have Entities Which Can Referenced for Specified + /// Provider and Types ... + /// this interface Globaly introduce all available Actions for those ... + /// + /// TKey: type of Provider Models Keys ... + /// TModel: type of Dto which Referenced ... + /// + public interface IXBaseReferencedProvider + { + // + #region Props ... + /// + /// Provider Identifier ... + /// + string Provider { get; } + #endregion + + // + #region Identifier ... + Task LastIndex(TKey providedForId); + #endregion + + // + #region Referenced Actions ... + /// + /// Get Specified Indexed Reference for Specific Provided ID ... + /// + /// + /// + /// + Task GetReference( + TKey providedForId, + int forIndex = 0 + ); + + /// + /// Retrieve all Exists References of Specific Provided ID ... + /// + /// + /// + Task> GetAllReferences(TKey providedForId); + + /// + /// Retrieve References of Specific Provided ID as Query ... + /// + /// + /// + Task> QueryReferences( + XQuery query, + TKey providedForId + ); + + /// + /// Add Reference a Model to Specific Provided ID ... + /// + /// + /// + /// + /// + /// + Task AddReference( + TModel model, + TKey providedForId, + int forIndex = 0, + string connectionId = null + ); + + /// + /// Add Reference a Model to Specific XRefence ... + /// + /// + /// + /// + /// + Task AddReference( + TModel model, + XReference reference, + string connectionId = null + ); + + /// + /// Remove Reference a Model for Specific Provided ID ... + /// + /// + /// + /// if null, removes all + /// + /// + Task RemoveReference( + TModel model, + TKey providedForId, + int? forIndex = null, + string connectionId = null + ); + + /// + /// Remove Reference a Model for Specific XRefence ... + /// + /// + /// + /// + /// + Task RemoveReference( + TModel model, + XReference reference, + string connectionId = null + ); + + /// + /// Remove All References to Specified Key ... + /// + /// + /// + /// + Task RemoveReferences( + TKey providedForId, + string connectionId = null + ); + #endregion + } +} \ No newline at end of file diff --git a/Models/XReference.cs b/Models/XReference.cs new file mode 100644 index 0000000..037417d --- /dev/null +++ b/Models/XReference.cs @@ -0,0 +1,9 @@ +namespace xDataService.Models +{ + public class XReference + { + public int Index { get; set; } + public T ReferencedTo { get; set; } + public string ProvidedFor { get; set; } + } +} \ No newline at end of file