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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,129 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using xDataService.Models;
|
||||||
|
using xModels.Dtos;
|
||||||
|
|
||||||
|
namespace xDataService.Interfaces
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 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 ...
|
||||||
|
/// </summary>
|
||||||
|
public interface IXBaseReferencedProvider<TModel, TKey>
|
||||||
|
{
|
||||||
|
//
|
||||||
|
#region Props ...
|
||||||
|
/// <summary>
|
||||||
|
/// Provider Identifier ...
|
||||||
|
/// </summary>
|
||||||
|
string Provider { get; }
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Identifier ...
|
||||||
|
Task<int> LastIndex(TKey providedForId);
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Referenced Actions ...
|
||||||
|
/// <summary>
|
||||||
|
/// Get Specified Indexed Reference for Specific Provided ID ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="providedForId"></param>
|
||||||
|
/// <param name="forIndex"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<TModel> GetReference(
|
||||||
|
TKey providedForId,
|
||||||
|
int forIndex = 0
|
||||||
|
);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieve all Exists References of Specific Provided ID ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="providedForId"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<IEnumerable<TModel>> GetAllReferences(TKey providedForId);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieve References of Specific Provided ID as Query ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="providedForId"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<XQueryResult<TModel>> QueryReferences(
|
||||||
|
XQuery query,
|
||||||
|
TKey providedForId
|
||||||
|
);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Add Reference a Model to Specific Provided ID ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="model"></param>
|
||||||
|
/// <param name="providedForId"></param>
|
||||||
|
/// <param name="forIndex"></param>
|
||||||
|
/// <param name="connectionId"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<bool> AddReference(
|
||||||
|
TModel model,
|
||||||
|
TKey providedForId,
|
||||||
|
int forIndex = 0,
|
||||||
|
string connectionId = null
|
||||||
|
);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Add Reference a Model to Specific XRefence<TKey> ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="model"></param>
|
||||||
|
/// <param name="reference"></param>
|
||||||
|
/// <param name="connectionId"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<bool> AddReference(
|
||||||
|
TModel model,
|
||||||
|
XReference<TKey> reference,
|
||||||
|
string connectionId = null
|
||||||
|
);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Remove Reference a Model for Specific Provided ID ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="model"></param>
|
||||||
|
/// <param name="providedForId"></param>
|
||||||
|
/// <param name="forIndex">if null, removes all</param>
|
||||||
|
/// <param name="connectionId"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<bool> RemoveReference(
|
||||||
|
TModel model,
|
||||||
|
TKey providedForId,
|
||||||
|
int? forIndex = null,
|
||||||
|
string connectionId = null
|
||||||
|
);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Remove Reference a Model for Specific XRefence<TKey> ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="model"></param>
|
||||||
|
/// <param name="providedForId"></param>
|
||||||
|
/// <param name="connectionId"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<bool> RemoveReference(
|
||||||
|
TModel model,
|
||||||
|
XReference<TKey> reference,
|
||||||
|
string connectionId = null
|
||||||
|
);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Remove All References to Specified Key ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="providedForId"></param>
|
||||||
|
/// <param name="connectionId"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<bool> RemoveReferences(
|
||||||
|
TKey providedForId,
|
||||||
|
string connectionId = null
|
||||||
|
);
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
namespace xDataService.Models
|
||||||
|
{
|
||||||
|
public class XReference<T>
|
||||||
|
{
|
||||||
|
public int Index { get; set; }
|
||||||
|
public T ReferencedTo { get; set; }
|
||||||
|
public string ProvidedFor { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user