Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
23 changed files
with
592 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
using System; | ||
using System.Text; | ||
|
||
namespace YesSql.Core.Collections | ||
{ | ||
public abstract class Collection : IDisposable | ||
{ | ||
IDisposable _scope; | ||
|
||
public Collection() | ||
{ | ||
_scope = CollectionHelper.EnterScope(this); | ||
} | ||
|
||
public abstract string GetSafeName(); | ||
public static string CreateSafeName(string name) | ||
{ | ||
if (name == null) | ||
{ | ||
throw new ArgumentNullException(nameof(name)); | ||
} | ||
|
||
var builder = new StringBuilder(); | ||
bool first = true; | ||
foreach (var c in name) | ||
{ | ||
if (Char.IsLetter(c) || (!first && Char.IsDigit(c))) | ||
{ | ||
builder.Append(c); | ||
} | ||
|
||
first = false; | ||
} | ||
|
||
return builder.ToString(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_scope.Dispose(); | ||
} | ||
} | ||
|
||
public static class CollectionExtensions | ||
{ | ||
public static string GetPrefixedName(this Collection collection, string table) | ||
{ | ||
var name = collection.GetSafeName(); | ||
|
||
if (String.IsNullOrEmpty(name)) | ||
{ | ||
return table; | ||
} | ||
|
||
return String.Concat(name, "_", table); | ||
} | ||
} | ||
|
||
public class DefaultCollection : Collection | ||
{ | ||
public DefaultCollection() | ||
{ | ||
|
||
} | ||
|
||
public override string GetSafeName() | ||
{ | ||
return ""; | ||
} | ||
} | ||
|
||
public class TypeCollection<T> : Collection | ||
{ | ||
private readonly string _safeName; | ||
|
||
public TypeCollection() | ||
{ | ||
_safeName = CreateSafeName(typeof(T).Name); | ||
|
||
if(String.IsNullOrEmpty(_safeName)) | ||
{ | ||
throw new ArgumentException("Invalid collection name: " + _safeName); | ||
} | ||
} | ||
|
||
public override string GetSafeName() | ||
{ | ||
return _safeName; | ||
} | ||
} | ||
|
||
public class NamedCollection : Collection | ||
{ | ||
private readonly string _safeName; | ||
|
||
public NamedCollection(string name) | ||
{ | ||
_safeName = CreateSafeName(name); | ||
|
||
if (String.IsNullOrEmpty(_safeName)) | ||
{ | ||
throw new ArgumentException("Invalid collection name: " + _safeName); | ||
} | ||
} | ||
|
||
public override string GetSafeName() | ||
{ | ||
return _safeName; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using System; | ||
#if NETSTANDARD1_5 | ||
using System.Threading; | ||
#else | ||
using System.Runtime.Remoting; | ||
using System.Runtime.Remoting.Messaging; | ||
#endif | ||
|
||
namespace YesSql.Core.Collections | ||
{ | ||
public class CollectionHelper | ||
{ | ||
|
||
#if NETSTANDARD1_5 | ||
private static readonly AsyncLocal<CollectionStack> _scopes = new AsyncLocal<CollectionStack>(); | ||
|
||
internal static CollectionStack Scopes | ||
{ | ||
get { return _scopes.Value; } | ||
set { _scopes.Value = value; } | ||
} | ||
#else | ||
private static readonly string CollectionDataName = "Collection.Scopes" + AppDomain.CurrentDomain.Id; | ||
|
||
internal static CollectionStack Scopes | ||
{ | ||
get | ||
{ | ||
var handle = CallContext.LogicalGetData(CollectionDataName) as ObjectHandle; | ||
|
||
if (handle == null) | ||
{ | ||
return null; | ||
} | ||
|
||
return handle.Unwrap() as CollectionStack; | ||
} | ||
set | ||
{ | ||
CallContext.LogicalSetData(CollectionDataName, new ObjectHandle(value)); | ||
} | ||
} | ||
#endif | ||
public static Collection Current | ||
{ | ||
get | ||
{ | ||
var scopes = GetOrCreateScopes(); | ||
return scopes.Peek(); | ||
} | ||
} | ||
|
||
internal static IDisposable EnterScope(Collection collection) | ||
{ | ||
var scopes = GetOrCreateScopes(); | ||
|
||
var scopeLease = new ScopeLease(scopes); | ||
Scopes = scopes.Push(collection); | ||
|
||
return scopeLease; | ||
} | ||
|
||
private static CollectionStack GetOrCreateScopes() | ||
{ | ||
var scopes = Scopes; | ||
if (scopes == null) | ||
{ | ||
scopes = CollectionStack.Empty; | ||
Scopes = scopes; | ||
} | ||
|
||
return scopes; | ||
} | ||
|
||
private sealed class ScopeLease : IDisposable | ||
{ | ||
readonly CollectionStack _collectionStack; | ||
|
||
public ScopeLease(CollectionStack collectionStack) | ||
{ | ||
_collectionStack = collectionStack; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Scopes = _collectionStack; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System; | ||
|
||
namespace YesSql.Core.Collections | ||
{ | ||
internal class CollectionStack | ||
{ | ||
private readonly CollectionStack _previous; | ||
private readonly Collection _collection; | ||
|
||
private CollectionStack() | ||
{ | ||
|
||
} | ||
|
||
private CollectionStack(CollectionStack previous, Collection collection) | ||
{ | ||
if (previous == null) | ||
{ | ||
throw new ArgumentNullException(nameof(previous)); | ||
} | ||
|
||
_previous = previous; | ||
_collection = collection; | ||
} | ||
|
||
public static readonly CollectionStack Empty = new CollectionStack(); | ||
|
||
public CollectionStack Push(Collection c) | ||
{ | ||
return new CollectionStack(this, c); | ||
} | ||
|
||
public Collection Peek() | ||
{ | ||
return _collection ?? new DefaultCollection(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.