using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SqlSugar { public class MapperCache { private Dictionary caches = new Dictionary(); private List _list { get; set; } private ISqlSugarClient _context { get; set; } public int GetIndex { get; set; } private MapperCache() { } public MapperCache(List list, ISqlSugarClient context) { _list = list; _context = context; } public Result Get(Func, Result> action) { GetIndex++; string key = "Get" +typeof(Result)+action.GetHashCode()+action.Method.Name; if (caches.ContainsKey(key)) { return (Result)caches[key]; } else { var result = action(_list); caches.Add(key, result); return result; } } public List GetListByPrimaryKeys(Func action) where Result : class, new() { { string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString(); return GetListByPrimaryKeys(action, key); } } public List GetListByPrimaryKeys(Func action) where Result : class, new() { { string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString(); return GetListByPrimaryKeys(action, key); } } public List GetListByPrimaryKeys(Func action) where Result : class, new() { { string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString(); return GetListByPrimaryKeys(action, key); } } public List GetListByPrimaryKeys(Func action) where Result : class, new() { { string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString(); return GetListByPrimaryKeys(action, key); } } public List GetListByPrimaryKeys(Func action) where Result : class, new() { { string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString(); return GetListByPrimaryKeys(action, key); } } public List GetListByPrimaryKeys(Func action) where Result : class, new() { { string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString(); return GetListByPrimaryKeys(action, key); } } public List GetListByPrimaryKeys(Func action) where Result : class, new() { { string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString(); return GetListByPrimaryKeys(action, key); } } public List GetListByPrimaryKeys(Func action) where Result : class, new() { { string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString(); return GetListByPrimaryKeys(action, key); } } public List GetListByPrimaryKeys(Func action) where Result : class, new() { { string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString(); return GetListByPrimaryKeys(action, key); } } public List GetListByPrimaryKeys(Func action) where Result : class, new() { { string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString(); return GetListByPrimaryKeys(action, key); } } public List GetListByPrimaryKeys(Func action) where Result : class, new() { { string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString(); return GetListByPrimaryKeys(action, key); } } public List GetListByPrimaryKeys(Func action) where Result : class, new() { { string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString(); return GetListByPrimaryKeys(action, key); } } public List GetListByPrimaryKeys(Func action) where Result : class, new() { { string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString(); return GetListByPrimaryKeys(action, key); } } private List GetListByPrimaryKeys(Func action, string key) where Result : class, new() { if (caches.ContainsKey(key)) { return (List)caches[key]; } else { var ids = _list.Select(action).ToList().Distinct().ToList(); var result = _context.Queryable().In(ids).ToList(); caches.Add(key, result); return result; } } } }