using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace bnhtrade.Core.Logic.Account { public class GetAccountCodeInfo { private string sqlConnectionString; private Data.Database.Account.ReadAccountCode readAccountCode; private Dictionary cache; public GetAccountCodeInfo(string sqlConnectionString) { this.sqlConnectionString = sqlConnectionString; readAccountCode = new Data.Database.Account.ReadAccountCode(sqlConnectionString); cache = new Dictionary(); } public void CacheClear() { cache.Clear(); } public void CacheFill() { CacheClear(); var resultList = readAccountCode.All(); foreach (var result in resultList) { cache.Add(result.AccountCodeId, result); } } public void CacheFill(List accountCodeList, bool forceDbRead = false) { if (accountCodeList == null || !accountCodeList.Any()) { return; } var accountCodeQueryList = new List(); foreach (var code in accountCodeList.Distinct().ToList()) { if (forceDbRead) { cache.Remove(code); accountCodeQueryList.Add(code); } else if (!cache.ContainsKey(code)) { accountCodeQueryList.Add(code); } } // get db list var dbList = readAccountCode.ByAccountCode(accountCodeQueryList); // add to cache foreach (var item in dbList) { cache.Add(item.AccountCodeId, item); } } public Model.Account.AccountCode ByAccountCode(int accountCode, bool forceDbRead = false) { CacheFill(new List { accountCode }, forceDbRead); if (cache.ContainsKey(accountCode)) { return cache[accountCode]; } else { return null; } } } }