using System; using System.Collections.Generic; using System.ComponentModel; 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) { // T referencedTo; try { referencedTo = (T)Convert.ChangeType(parts[1], typeof(T)); } catch { referencedTo = (T)TypeDescriptor.GetConverter(typeof(T)).ConvertFrom(parts[1]); } // // Parse String Parts ... result = new XReference { ReferencedTo = referencedTo, Index = (int)Convert.ChangeType(parts[2], typeof(int)), 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) { // var 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; } } }