add Requirement fo Refactoring XReference Mechanism ...
This commit is contained in:
@@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts an XReference<T> to String Representation ...
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="splitter"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToXReferenceString<T>(
|
||||
this XReference<T> 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parse an String and Extract to XReference<T> if it's Described ...
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="splitter"></param>
|
||||
/// <returns></returns>
|
||||
public static XReference<T> FromXReference<T>(
|
||||
this string source,
|
||||
char? splitter = '_'
|
||||
)
|
||||
{
|
||||
//
|
||||
XReference<T> 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<T>
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a Collection of XReference<T> to it's String Representation ...
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="source"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToXReferenceString<T>(this IEnumerable<XReference<T>> source)
|
||||
{
|
||||
//
|
||||
var result = string.Empty;
|
||||
|
||||
//
|
||||
if (source.IsNull() && source.HasChild())
|
||||
{
|
||||
//
|
||||
var stringList = new List<string>();
|
||||
foreach (var s in source)
|
||||
{
|
||||
//
|
||||
var sr = ToXReferenceString(s);
|
||||
if (!sr.IsNullOrEmpty())
|
||||
{
|
||||
stringList.Add(sr);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
result = stringList.ToListString();
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
public static string ToXReferenceString<T>(this ICollection<XReference<T>> source)
|
||||
{
|
||||
//
|
||||
var result = source
|
||||
.AsEnumerable()
|
||||
.ToXReferenceString();
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
public static string ToXReferenceString<T>(this IList<XReference<T>> source)
|
||||
{
|
||||
//
|
||||
var result = source
|
||||
.AsEnumerable()
|
||||
.ToXReferenceString();
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parse an string to XReference<T> Collection Representation ...
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="source"></param>
|
||||
/// <returns></returns>
|
||||
public static IList<XReference<T>> ParseXReferenceList<T>(this string source)
|
||||
{
|
||||
//
|
||||
IList<XReference<T>> result = new List<XReference<T>>();
|
||||
|
||||
//
|
||||
if (!source.IsNullOrEmpty())
|
||||
{
|
||||
//
|
||||
var stringList = source.ParseListString<string>();
|
||||
if (!stringList.IsNull() && stringList.HasChild())
|
||||
{
|
||||
//
|
||||
foreach (var s in stringList)
|
||||
{
|
||||
//
|
||||
var sr = FromXReference<T>(s);
|
||||
if (!sr.IsNullOrDefault())
|
||||
{
|
||||
result.Add(sr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user