59 lines
2.0 KiB
C#
59 lines
2.0 KiB
C#
using Kit.Helpers.Log;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Kit.Helpers.Repository
|
|
{
|
|
public interface ISelectContext : IDictionaryCacheProvider
|
|
{
|
|
#if NETCOREAPP
|
|
IDictionary<object, object> Items { get; }
|
|
#elif NETFRAMEWORK
|
|
IDictionary Items { get; }
|
|
#endif
|
|
ICallStackLog CallStackLog { get; set; }
|
|
}
|
|
|
|
public class SelectContext : ISelectContext
|
|
{
|
|
public SelectContext()
|
|
{
|
|
Items = new Dictionary<object, object>();
|
|
CallStackLog = Log.CallStackLog.Empty;
|
|
}
|
|
|
|
#if NETCOREAPP
|
|
public IDictionary<object, object> Items { get; }
|
|
#elif NETFRAMEWORK
|
|
public IDictionary Items { get; }
|
|
#endif
|
|
public ICallStackLog CallStackLog { get; set; }
|
|
|
|
private Dictionary<TKey, TEntity> GetOrCreateDictionary<TKey, TEntity>(string cacheKey)
|
|
{
|
|
#if NETCOREAPP
|
|
Dictionary<TKey, TEntity> cacheDictionary = Items.ContainsKey(cacheKey) ? (Dictionary<TKey, TEntity>)Items[cacheKey] : null;
|
|
#elif NETFRAMEWORK
|
|
Dictionary<TKey, TEntity> cacheDictionary = Items.Contains(cacheKey) ? (Dictionary<TKey, TEntity>)Items[cacheKey] : null;
|
|
#endif
|
|
|
|
if (cacheDictionary == null)
|
|
{
|
|
cacheDictionary = new Dictionary<TKey, TEntity>();
|
|
Items[cacheKey] = cacheDictionary;
|
|
}
|
|
|
|
return cacheDictionary;
|
|
}
|
|
|
|
public void SetByKey<TKey, TValue>(string cacheKey, TKey key, TValue value) => GetOrCreateDictionary<TKey, TValue>(cacheKey).SetByKey(key, value);
|
|
public TValue GetByKey<TKey, TValue>(string cacheKey, TKey key) => GetOrCreateDictionary<TKey, TValue>(cacheKey).GetByKey(key);
|
|
public TValue GetByKey<TKey, TValue>(string cacheKey, TKey key, TValue defaultValue) => GetOrCreateDictionary<TKey, TValue>(cacheKey).GetByKey(key, defaultValue);
|
|
|
|
public bool TryGet<TKey, TValue>(string cacheKey, TKey key, TValue defaultValue, out TValue value)
|
|
{
|
|
throw new System.NotImplementedException();
|
|
}
|
|
}
|
|
}
|