Ejemplo n.º 1
0
 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);
     }
 }
Ejemplo n.º 2
0
 /// <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> ());
     }
 }
Ejemplo n.º 3
0
 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));
 }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        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));
            }
        }
Ejemplo n.º 6
0
 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));
     }
 }