public T Get <T> (IEnumerable <string> scopes) where T : class, IEquatable <T>, new () { // The search is done vertically, looking first at the parents foreach (string scope in scopes) { PolicyContainer currentBag = this; while (currentBag != null) { if (currentBag.DirectHas <T> (scope)) { T pol = currentBag.DirectGet <T> (scope); if (!PolicyService.IsUndefinedPolicy(pol)) { return(pol); } // If the bag has the policy (Has<> returns true) but the policy is undefined, // then we have to keep looking using the base scopes. // We start looking from the original bag, using the new scope. break; } else { currentBag = currentBag.ParentPolicies; } } } if (InheritDefaultPolicies) { return(PolicyService.GetDefaultPolicy <T>(scopes)); } else { return(null); } }
/// <summary> /// The Get methods return policies taking into account inheritance. If a policy /// can't be found it may return null, but never an 'undefined' policy. /// </summary> /// <returns> /// The policy of the given type, or null if not found. /// </returns> public T Get <T> () where T : class, IEquatable <T>, new () { if (policies != null) { object policy; if (policies.TryGetValue(typeof(T), null, out policy)) { if (!PolicyService.IsUndefinedPolicy(policy)) { return((T)policy); } else if (InheritDefaultPolicies) { return(PolicyService.GetDefaultPolicy <T> ()); } else { return(null); } } } if (!InheritDefaultPolicies) { return(null); } else if (IsRoot) { return(PolicyService.GetDefaultPolicy <T> ()); } else { return(ParentPolicies.Get <T> ()); } }
internal object Get(Type type, IEnumerable <string> scopes) { // The search is done vertically, looking first at the parents foreach (string scope in scopes) { PolicyContainer currentBag = this; while (currentBag != null) { if (currentBag.DirectHas(type, scope)) { object pol = currentBag.DirectGet(type, scope); if (!PolicyService.IsUndefinedPolicy(pol)) { return(pol); } // If the bag has the policy (Has<> returns true) but the policy is undefined, // then we have to keep looking using the base scopes. // We start looking from the original bag, using the new scope. break; } else { currentBag = currentBag.ParentPolicies; } } } return(GetDefaultPolicy(type, scopes)); }
internal object Get(Type type, string scope) { if (policies == null) { return(null); } object o; if (policies.TryGetValue(type, scope, out o)) { if (PolicyService.IsUndefinedPolicy(o)) { return(null); } } return(o); }
public IEnumerable <ScopedPolicy <T> > GetScoped <T> () where T : class, IEquatable <T>, new () { if (policies == null) { yield break; } foreach (KeyValuePair <PolicyKey, object> pinfo in policies) { if (pinfo.Key.PolicyType == typeof(T)) { yield return(new ScopedPolicy <T> ((T)pinfo.Value, pinfo.Key.Scope)); } } T pol = Get <T> (); if (pol != null && !PolicyService.IsUndefinedPolicy(pol)) { yield return(new ScopedPolicy <T> (pol, null)); } }
internal object Get(Type type) { if (policies != null) { object policy; if (policies.TryGetValue(type, null, out policy)) { if (!PolicyService.IsUndefinedPolicy(policy)) { return(policy); } return(GetDefaultPolicy(type)); } } if (IsRoot) { return(GetDefaultPolicy(type)); } else { return(ParentPolicies.Get(type)); } }