You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Zero/ZeroLevel/Services/Collections/EverythingStorage.cs

212 lines
6.2 KiB

6 years ago
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using ZeroLevel.Services.Invokation;
namespace ZeroLevel.Services.Collections
{
public class EverythingStorage :
IEverythingStorage
{
6 years ago
public static IEverythingStorage Create()
{
return new EverythingStorage();
}
6 years ago
private class ConcreteTypeRepository
{
private readonly IInvokeWrapper _wrapper;
private readonly Invoker _insert;
private readonly Invoker _containsKey;
private readonly Invoker _remove;
private readonly Invoker _getter;
private readonly Invoker _keys_getter;
6 years ago
private readonly object _instance;
public ConcreteTypeRepository(Type entityType)
{
_wrapper = InvokeWrapper.Create();
var genericType = typeof(Dictionary<,>);
var instanceType = genericType.MakeGenericType(new Type[] { typeof(string), entityType });
_instance = Activator.CreateInstance(instanceType);
var insert_key = _wrapper.Configure(instanceType, "Add").Single();
6 years ago
_insert = _wrapper.GetInvoker(insert_key);
var contains_key = _wrapper.Configure(instanceType, mi => mi.Name.Equals("ContainsKey") && mi.GetParameters()?.Length == 1).Single();
6 years ago
_containsKey = _wrapper.GetInvoker(contains_key);
var remove_key = _wrapper.Configure(instanceType, mi => mi.Name.Equals("Remove") && mi.GetParameters()?.Length == 1).Single();
6 years ago
_remove = _wrapper.GetInvoker(remove_key);
var p = instanceType.GetProperty("Item", entityType);
var getter = p.GetGetMethod();
var get_key = _wrapper.Configure(getter);
_getter = _wrapper.GetInvoker(get_key);
var k = instanceType.GetProperty("Keys", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public| System.Reflection.BindingFlags.NonPublic);
var keys_getter = k.GetGetMethod();
var get_keys = _wrapper.Configure(keys_getter);
_keys_getter = _wrapper.GetInvoker(get_keys);
6 years ago
}
public void Insert<T>(string key, T entity)
{
_insert.Invoke(_instance, new object[] { key, entity });
6 years ago
}
public void InsertOrUpdate<T>(string key, T entity)
{
if ((bool)_containsKey.Invoke(_instance, key))
_remove.Invoke(_instance, key);
_insert.Invoke(_instance, new object[] { key, entity });
6 years ago
}
public bool ContainsKey(string key)
{
return (bool)_containsKey.Invoke(_instance, key);
}
6 years ago
public void Remove(string key)
6 years ago
{
_remove.Invoke(_instance, key);
}
public T Get<T>(string key)
{
return (T)_getter.Invoke(_instance, key);
}
6 years ago
public IEnumerable<string> Keys()
{
return (IEnumerable<string>)_keys_getter.Invoke(_instance);
}
6 years ago
public object Get(string key)
{
return _getter.Invoke(_instance, key);
}
6 years ago
}
6 years ago
private readonly ConcurrentDictionary<Type, ConcreteTypeRepository> _shardedRepositories =
new ConcurrentDictionary<Type, ConcreteTypeRepository>();
private ConcreteTypeRepository this[Type type]
{
get
{
if (_shardedRepositories.ContainsKey(type) == false)
{
var r = new ConcreteTypeRepository(type);
_shardedRepositories.AddOrUpdate(type, r, (t, old) => old);
}
return _shardedRepositories[type];
}
}
public bool TryAdd<T>(string key, T value)
{
try
{
this[typeof(T)].Insert<T>(key, value);
return true;
}
catch
{ }
return false;
}
public bool ContainsKey<T>(string key)
{
return this[typeof(T)].ContainsKey(key);
}
public bool TryRemove<T>(string key)
{
try
{
6 years ago
this[typeof(T)].Remove(key);
6 years ago
return true;
}
catch
{ }
return false;
}
public void Add<T>(string key, T value)
{
this[typeof(T)].Insert<T>(key, value);
}
public void Remove<T>(string key)
{
6 years ago
this[typeof(T)].Remove(key);
6 years ago
}
public T Get<T>(string key)
{
return this[typeof(T)].Get<T>(key);
}
6 years ago
public void AddOrUpdate<T>(string key, T value)
{
this[typeof(T)].InsertOrUpdate<T>(key, value);
6 years ago
}
6 years ago
public bool TryAdd(Type type, string key, object value)
6 years ago
{
6 years ago
try
{
this[type].Insert(key, value);
return true;
}
catch
{ }
return false;
6 years ago
}
6 years ago
public bool ContainsKey(Type type, string key)
6 years ago
{
6 years ago
return this[type].ContainsKey(key);
}
public bool TryRemove(Type type, string key)
{
try
{
this[type].Remove(key);
return true;
}
catch
{ }
return false;
}
public void Add(Type type, string key, object value)
{
this[type].Insert(key, value);
}
public void AddOrUpdate(Type type, string key, object value)
{
this[type].InsertOrUpdate(key, value);
}
public void Remove(Type type, string key)
{
this[type].Remove(key);
}
public object Get(Type type, string key)
{
return this[type].Get(key);
6 years ago
}
public IEnumerable<string> Keys<T>()
{
return this[typeof(T)].Keys();
}
6 years ago
}
}

Powered by TurnKey Linux.