/// <summary> /// 添加Sql的策略 /// </summary> /// <param name="policy"></param> public static void AddPolicy(this IConfigSqlInfoModifier info, ISqlConfigPolicy policy) { policy.CheckNull(nameof(policy)); info.Policies.Add(EFHelper.Services.EFCoreExUtility.GetSqlConfigPolicyName(policy.GetType()), policy); }
/// <summary> /// 用于初始化的策略执行器,就是在程序运行期间只执行一次,除非sql的配置数据发生了改变(例如:替换表名 / 合并分部sql等的执行器) /// </summary> /// <param name="info"></param> /// <returns></returns> protected void DoSqlInitPolicyExecutors(IDictionary <string, ISqlConfigPolicy> policies, IPolicyExecutorInfo info) { var pinfo = (ISqlInitPolicyExecutorInfo)info; ISqlConfigPolicy policy = null; if (policies?.Count > 0) { foreach (var pair in _sqlInitPolicyExecutorsOrder) { info.PolicyName = pair.Key; //全局的策略对象 _globalPolicies.TryGetValue(pair.Key, out policy); info.GlobalPolicy = policy; //获取通过方法形参传递过来的策略对象 policies.TryGetValue(pair.Key, out policy); info.ParameterPolicy = policy; pair.Value.Item2.Invoke().Execute(pinfo); } } else { info.ParameterPolicy = null; foreach (var pair in _sqlInitPolicyExecutorsOrder) { info.PolicyName = pair.Key; //全局的策略对象 _globalPolicies.TryGetValue(pair.Key, out policy); info.GlobalPolicy = policy; pair.Value.Item2.Invoke().Execute(pinfo); } } }
/// <summary> /// 转换成策略存储的Dictionary /// </summary> /// <param name="policy"></param> /// <returns></returns> public static IDictionary <string, ISqlConfigPolicy> ToPolicies(this ISqlConfigPolicy policy) { return(policy == null ? null : new Dictionary <string, ISqlConfigPolicy>() { { EFHelper.Services.EFCoreExUtility.GetSqlConfigPolicyName(policy.GetType(), true), policy } }); }
/// <summary> /// 设置全局策略对象 /// </summary> public static void SetGlobalPolicy(this ILuaSqlConfigManager mgr, ISqlConfigPolicy policy) { policy.CheckNull(nameof(policy)); var policyName = EFHelper.Services.EFCoreExUtility.GetSqlConfigPolicyName(policy.GetType()); if (string.IsNullOrEmpty(policyName)) { throw new ArgumentException($"Could not find {nameof(SqlConfigPolicyAttribute)} in {policy.GetType().Name} type"); } mgr.PolicyMgr.GlobalPolicies[policyName] = policy; }
/// <summary> /// 用于lua sql执行时的策略执行器(例如:查询缓存,查询缓存清理,日志记录等等) /// </summary> /// <param name="info"></param> /// <returns></returns> protected void DoLuaSqlExecutePolicyExecutors(IDictionary <string, ISqlConfigPolicy> policies, ILuaSqlPolicyExecutorInfo info) { var pinfo = (ILuaSqlExecutePolicyExecutorInfo)info; ISqlConfigPolicy policy = null; if (policies?.Count > 0) { foreach (var pair in _luasqlExecutePolicyExecutorsOrder) { info.PolicyName = pair.Key; //全局的策略对象 _globalPolicies.TryGetValue(pair.Key, out policy); info.GlobalPolicy = policy; //获取通过方法形参传递过来的策略对象 policies.TryGetValue(pair.Key, out policy); info.ParameterPolicy = policy; pair.Value.Item2.Invoke().Execute(pinfo); //如果是执行完毕,那么跳出循环不再继续执行 if (pinfo.IsEnd) { break; } } } else { info.ParameterPolicy = null; foreach (var pair in _luasqlExecutePolicyExecutorsOrder) { info.PolicyName = pair.Key; //全局的策略对象 _globalPolicies.TryGetValue(pair.Key, out policy); info.GlobalPolicy = policy; pair.Value.Item2.Invoke().Execute(pinfo); //如果是执行完毕,那么跳出循环不再继续执行 if (pinfo.IsEnd) { break; } } } }
/// <summary> /// 设置全局策略对象 /// </summary> /// <param name="mgr"></param> /// <param name="policyName"></param> /// <param name="policy"></param> public static void SetGlobalPolicy(this ILuaSqlConfigManager mgr, string policyName, ISqlConfigPolicy policy) { policy.CheckNull(nameof(policy)); policyName.CheckStringIsNullOrEmpty(nameof(policyName)); mgr.PolicyMgr.GlobalPolicies[policyName] = policy; }
public bool IsUsePolicy(ISqlConfigPolicy policy) { return(policy != null && (policy.IsUse == null || policy.IsUse.Value)); }