Initial Commit ...
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using xCommons.Extensions;
|
||||
|
||||
namespace xDataService.Helpers {
|
||||
public partial class XCursorHelper {
|
||||
public static string ToCursor<T> (T id) {
|
||||
//
|
||||
// Check Numbers ...
|
||||
if (id.ToString ().IsDigits ()) {
|
||||
return Convert
|
||||
.ToBase64String (BitConverter
|
||||
.GetBytes (Convert.ToInt32 (id))
|
||||
);
|
||||
} else
|
||||
//
|
||||
// Check Guid ...
|
||||
if (id.ToString ().IsGuid ()) {
|
||||
return Convert
|
||||
.ToBase64String (new Guid (id.ToString ())
|
||||
.ToByteArray ());
|
||||
} else
|
||||
//
|
||||
// Assume as String ...
|
||||
{
|
||||
return Convert
|
||||
.ToBase64String (id
|
||||
.ToString ().ToBytes ());
|
||||
}
|
||||
}
|
||||
|
||||
public static T FromCursor<T> (string cursor) {
|
||||
//
|
||||
// Check Numbers ...
|
||||
var returnType = typeof (T);
|
||||
if (returnType == typeof (int) ||
|
||||
returnType == typeof (long) ||
|
||||
returnType == typeof (decimal)) {
|
||||
return Convert.ChangeType ((BitConverter
|
||||
.ToInt32 (Convert
|
||||
.FromBase64String (cursor),
|
||||
0
|
||||
))
|
||||
.ToDynamicObject (), returnType);
|
||||
} else
|
||||
//
|
||||
// Check Guid ...
|
||||
if (returnType == typeof (Guid)) {
|
||||
return Convert.ChangeType ((new Guid (
|
||||
Convert.FromBase64String (cursor)
|
||||
))
|
||||
.ToDynamicObject (), returnType);
|
||||
} else
|
||||
//
|
||||
// Assume as String ...
|
||||
{
|
||||
return Convert.ChangeType ((
|
||||
Convert.FromBase64String (cursor)
|
||||
)
|
||||
.ToDynamicObject (), returnType);
|
||||
}
|
||||
}
|
||||
|
||||
public static (string firstCursor, string lastCursor) GetFirstAndLastCursor<T> (IEnumerable<T> ids) {
|
||||
//
|
||||
if (ids?.Any () != true) {
|
||||
return (null, null);
|
||||
}
|
||||
|
||||
//
|
||||
var firstCursor = ToCursor (ids.First ());
|
||||
var lastCursor = ToCursor (ids.Last ());
|
||||
|
||||
//
|
||||
return (firstCursor, lastCursor);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user