Ejemplo n.º 1
0
        public override ICallHandler CreateHandler(IUnityContainer container, MethodImplementationInfo member)
        {
            var keyGenerator = GetKeyGenerator(container, member);
            var callHandler  = container.Resolve <CacheCallHandler>(new ParameterOverride("keyGenerator", keyGenerator));

            return(callHandler);
        }
Ejemplo n.º 2
0
        public void ShouldGetCorrectHandlersGivenAttributesOnInterfaceMethodsAfterAddingAttributeDrivenPolicy()
        {
            PolicySet policies = new PolicySet();

            List <ICallHandler> oneHandlers
                = new List <ICallHandler>(policies.GetHandlersFor(GetMethodImplInfo <TwoType>("One"), container));

            Assert.AreEqual(0, oneHandlers.Count);

            policies.Add(new AttributeDrivenPolicy());

            MethodImplementationInfo oneInfo = new MethodImplementationInfo(
                typeof(IOne).GetMethod("One"),
                typeof(TwoType).GetMethod("One"));

            oneHandlers
                = new List <ICallHandler>(policies.GetHandlersFor(oneInfo, container));

            Assert.AreEqual(2, oneHandlers.Count);
            Assert.IsTrue(oneHandlers[0] is MarkerCallHandler);
            Assert.IsTrue(oneHandlers[1] is MarkerCallHandler);

            Assert.AreEqual("IOneOne", ((MarkerCallHandler)oneHandlers[0]).HandlerName);
            Assert.AreEqual("MethodOneOverride", ((MarkerCallHandler)oneHandlers[1]).HandlerName);
        }
Ejemplo n.º 3
0
 public override ICallHandler CreateHandler(IUnityContainer container, MethodImplementationInfo member)
 {
     return(container.Resolve <LoggingCallHandler>(
                new LambdaPropertyOverride <LoggingCallHandler>(h => h.Builder, CreateStringBuilder(member.ImplementationMethodInfo, container)),
                new LambdaPropertyOverride <LoggingCallHandler>(h => h.IncludesArguments, IncludesArguments),
                new LambdaPropertyOverride <LoggingCallHandler>(h => h.IndentSize, IndentSize),
                new LambdaPropertyOverride <LoggingCallHandler>(h => h.Order, Order)));
 }
Ejemplo n.º 4
0
        private bool DoesNotHaveNoPoliciesAttributeRule(MethodImplementationInfo method)
        {
            bool doesNotHaveRule = true;

            doesNotHaveRule &= method.InterfaceMethodInfo != null?_doesNotHaveNoPoliciesAttributeRule.Matches(method.InterfaceMethodInfo) : true;

            doesNotHaveRule &= _doesNotHaveNoPoliciesAttributeRule.Matches(method.ImplementationMethodInfo);
            return(doesNotHaveRule);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Return ordered collection of handlers in order that apply to the given member.
 /// </summary>
 /// <param name="member">Member that may or may not be assigned handlers by this policy.</param>
 /// <param name="container">The <see cref="IUnityContainer"/> to use when creating handlers,
 /// if necessary.</param>
 /// <returns>Collection of handlers (possibly empty) that apply to this member.</returns>
 protected override IEnumerable <ICallHandler> DoGetHandlersFor(MethodImplementationInfo member, IUnityContainer container)
 {
     if (Matches(member))
     {
         foreach (string callHandlerName in _callHandlerNames)
         {
             yield return(container.Resolve <ICallHandler>(callHandlerName));
         }
     }
 }
Ejemplo n.º 6
0
        public void ShouldNotMatchPolicyWhenNoRulesMatch()
        {
            PolicySet policies = GetMultiplePolicySet();

            MethodImplementationInfo noMatchMember   = GetMethodImplInfo <NoMatchAnywhere>("NoMatchHere");
            List <ICallHandler>      noMatchHandlers =
                new List <ICallHandler>(policies.GetHandlersFor(noMatchMember, container));

            Assert.AreEqual(0, noMatchHandlers.Count);
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Gets the policies that apply to the given member.
 /// </summary>
 /// <param name="member">Member to get policies for.</param>
 /// <returns>Collection of policies that apply to this member.</returns>
 public IEnumerable <InjectionPolicy> GetPoliciesFor(MethodImplementationInfo member)
 {
     foreach (InjectionPolicy policy in this)
     {
         if (policy.Matches(member))
         {
             yield return(policy);
         }
     }
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Derived classes implement this method to calculate if the policy
        /// will provide any handler to the specified member.
        /// </summary>
        /// <param name="member">Member to check.</param>
        /// <returns>true if policy applies to this member, false if not.</returns>
        protected override bool DoesMatch(MethodImplementationInfo member)
        {
            Guard.ArgumentNotNull(member, "member");

            bool matchesInterface = member.InterfaceMethodInfo != null?_attributeMatchRule.Matches(member.InterfaceMethodInfo) : false;

            bool matchesImplementation = _attributeMatchRule.Matches(member.ImplementationMethodInfo);

            return(matchesInterface | matchesImplementation);
        }
Ejemplo n.º 9
0
        public void HandlersOrderedProperlyUsingRelativeAndAbsoluteOrder()
        {
            RuleDrivenPolicy policy
                = new RuleDrivenPolicy("MatchesInterfacePolicy",
                                       new IMatchingRule[] { new TypeMatchingRule("ITwo") },
                                       new string[] { "Handler1", "Handler2", "Handler3", "Handler4", "Handler5", "Handler6" });

            ICallHandler handler1 = new CallCountHandler();

            handler1.Order = 0;

            ICallHandler handler2 = new CallCountHandler();

            handler2.Order = 3;

            ICallHandler handler3 = new CallCountHandler();

            handler3.Order = 3;

            ICallHandler handler4 = new CallCountHandler();

            handler4.Order = 2;

            ICallHandler handler5 = new CallCountHandler();

            handler5.Order = 4;

            ICallHandler handler6 = new CallCountHandler();

            handler6.Order = 1;

            container
            .RegisterInstance <ICallHandler>("Handler1", handler1)
            .RegisterInstance <ICallHandler>("Handler2", handler2)
            .RegisterInstance <ICallHandler>("Handler3", handler3)
            .RegisterInstance <ICallHandler>("Handler4", handler4)
            .RegisterInstance <ICallHandler>("Handler5", handler5)
            .RegisterInstance <ICallHandler>("Handler6", handler6);

            PolicySet policies = new PolicySet(policy);

            MethodImplementationInfo twoInfo = new MethodImplementationInfo(
                typeof(ITwo).GetMethod("Two"), typeof(TwoType).GetMethod("Two"));

            List <ICallHandler> handlers
                = new List <ICallHandler>(policies.GetHandlersFor(twoInfo, container));

            Assert.AreEqual(handler6, handlers[0]);
            Assert.AreEqual(handler4, handlers[1]);
            Assert.AreEqual(handler2, handlers[2]);
            Assert.AreEqual(handler3, handlers[3]);
            Assert.AreEqual(handler5, handlers[4]);
            Assert.AreEqual(handler1, handlers[5]);
        }
Ejemplo n.º 10
0
 public void Setup()
 {
     nothingSpecialMethod      = MakeMethodImpl <AttributeTestTarget>("NothingSpecial");
     doSomethingMethod         = MakeMethodImpl <AttributeTestTarget>("DoSomething");
     getCriticalInfoMethod     = MakeMethodImpl <AttributeTestTarget>("GetCriticalInformation");
     mustBeFastMethod          = MakeMethodImpl <AttributeTestTarget>("MustBeFast");
     getNameMethod             = new MethodImplementationInfo(null, typeof(AttributeTestTarget).GetProperty("Name").GetGetMethod());
     hasAttributeMethod        = MakeMethodImpl <SecondAttributeTestTarget>("HasAttribute");
     doesntHaveAttributeMethod = MakeMethodImpl <SecondAttributeTestTarget>("DoesntHaveAttribute");
     aNewMethod = MakeMethodImpl <DerivedAttributeTestTarget>("ANewMethod");
 }
        public void ShouldInheritHandlersFromInterface()
        {
            MethodImplementationInfo getNewsMethod = new MethodImplementationInfo(
                typeof(INewsService).GetMethod("GetNews"),
                typeof(NewsService).GetMethod("GetNews"));
            AttributeDrivenPolicy policy = new AttributeDrivenPolicy();
            List <ICallHandler>   handlers
                = new List <ICallHandler>(policy.GetHandlersFor(getNewsMethod, new UnityContainer()));

            Assert.AreEqual(1, handlers.Count);
            Assert.AreSame(typeof(CallHandler1), handlers[0].GetType());
        }
Ejemplo n.º 12
0
        /// <summary>
        /// GetOrDefault the pipeline for the given method, creating it if necessary.
        /// </summary>
        /// <param name="method">Method to retrieve the pipeline for.</param>
        /// <param name="handlers">Handlers to initialize the pipeline with</param>
        /// <returns>True if the pipeline has any handlers in it, false if not.</returns>
        public bool InitializePipeline(MethodImplementationInfo method, IEnumerable <ICallHandler> handlers)
        {
            Guard.ArgumentNotNull(method, "method");

            var pipeline = CreatePipeline(method.ImplementationMethodInfo, handlers);

            if (method.InterfaceMethodInfo != null)
            {
                _pipelines[HandlerPipelineKey.ForMethod(method.InterfaceMethodInfo)] = pipeline;
            }

            return(pipeline.Count > 0);
        }
Ejemplo n.º 13
0
        public void ShouldHaveNoHandlersWhenPolicyDoesntMatch()
        {
            IMatchingRule[] rules     = { };
            IUnityContainer container = CreateConfiguredContainer();

            InjectionPolicy p = CreatePolicy(container, rules);

            MethodImplementationInfo thisMember = GetMethodImplInfo <PolicyFixture>("ShouldHaveNoHandlersWhenPolicyDoesntMatch");
            List <ICallHandler>      memberHandlers
                = new List <ICallHandler>(p.GetHandlersFor(thisMember, container));

            Assert.AreEqual(0, memberHandlers.Count);
        }
Ejemplo n.º 14
0
 /// <summary>
 /// Returns ordered collection of handlers in order that apply to the given member.
 /// </summary>
 /// <param name="member">Member that may or may not be assigned handlers by this policy.</param>
 /// <param name="container">The <see cref="IUnityContainer"/> to use when creating handlers,
 /// if necessary.</param>
 /// <returns>Collection of handlers (possibly empty) that apply to this member.</returns>
 public virtual IEnumerable <ICallHandler> GetHandlersFor(MethodImplementationInfo member, IUnityContainer container)
 {
     if (DoesNotHaveNoPoliciesAttributeRule(member))
     {
         List <ICallHandler> handlers = new List <ICallHandler>(DoGetHandlersFor(member, container));
         if (handlers.Count > 0)
         {
             foreach (ICallHandler handler in handlers)
             {
                 yield return(handler);
             }
         }
     }
 }
 protected override IEnumerable <ICallHandler> DoGetHandlersFor(MethodImplementationInfo member, IUnityContainer container)
 {
     if (member.InterfaceMethodInfo != null)
     {
         foreach (var handlerAttribute in ReflectionHelper.GetAllAttributes <PerMethodHandlerAttribute>(member.InterfaceMethodInfo, true))
         {
             yield return(handlerAttribute.CreateHandler(container, member));
         }
     }
     foreach (var handlerAttribute in ReflectionHelper.GetAllAttributes <PerMethodHandlerAttribute>(member.ImplementationMethodInfo, true))
     {
         yield return(handlerAttribute.CreateHandler(container, member));
     }
 }
Ejemplo n.º 16
0
        public void ShouldMatchPolicyByMethodName()
        {
            PolicySet policies = GetMultiplePolicySet();

            MethodImplementationInfo noMatchMember   = GetMethodImplInfo <MatchesByMemberName>("NoMatch");
            MethodImplementationInfo nameMatchMember = GetMethodImplInfo <MatchesByMemberName>("NameMatch");
            List <ICallHandler>      noMatchHandlers =
                new List <ICallHandler>(policies.GetHandlersFor(noMatchMember, container));
            List <ICallHandler> nameMatchHandlers =
                new List <ICallHandler>(policies.GetHandlersFor(nameMatchMember, container));

            Assert.AreEqual(0, noMatchHandlers.Count);
            Assert.AreEqual(1, nameMatchHandlers.Count);
            Assert.IsTrue(typeof(Handler2) == nameMatchHandlers[0].GetType());
        }
Ejemplo n.º 17
0
        public void WhenConfiguredContainer_ThenCanResolvePolicy()
        {
            using (var container = new UnityContainer())
            {
                this.policyData.ConfigureContainer(container);

                var policy = container.Resolve <RuleDrivenPolicy>("policy");

                var method = new MethodImplementationInfo(StaticReflection.GetMethodInfo <object>(o => o.ToString()), StaticReflection.GetMethodInfo <object>(o => o.ToString()));

                Assert.AreEqual("policy", policy.Name);
                Assert.IsFalse(policy.Matches(method));
                Assert.AreEqual(0, policy.GetHandlersFor(method, container).Count());
            }
        }
Ejemplo n.º 18
0
        public void ShouldGetHandlersInOrderWithGetHandlersFor()
        {
            IMatchingRule[] rules     = { new MemberNameMatchingRule("ShouldGetHandlersInOrderWithGetHandlersFor") };
            IUnityContainer container = CreateConfiguredContainer();

            InjectionPolicy p = CreatePolicy(container, rules);

            MethodImplementationInfo member = GetMethodImplInfo <PolicyFixture>("ShouldGetHandlersInOrderWithGetHandlersFor");

            List <ICallHandler> expectedHandlers = new List <ICallHandler>(container.ResolveAll <ICallHandler>());
            List <ICallHandler> actualHandlers   = new List <ICallHandler>(p.GetHandlersFor(member, container));

            CollectionAssertExtensions.AreEqual(
                expectedHandlers,
                actualHandlers,
                new TypeComparer());
        }
Ejemplo n.º 19
0
        public void ShouldBeAbleToMatchPropertyGet()
        {
            IMatchingRule[] rules     = { new MemberNameMatchingRule("get_Balance") };
            IUnityContainer container = CreateConfiguredContainer();

            InjectionPolicy p = CreatePolicy(container, rules);

            PropertyInfo             balanceProperty = typeof(MockDal).GetProperty("Balance");
            MethodImplementationInfo getMethod       = new MethodImplementationInfo(null, balanceProperty.GetGetMethod());

            List <ICallHandler> expectedHandlers = new List <ICallHandler>(container.ResolveAll <ICallHandler>());
            List <ICallHandler> actualHandlers   = new List <ICallHandler>(p.GetHandlersFor(getMethod, container));

            CollectionAssertExtensions.AreEqual(
                expectedHandlers,
                actualHandlers,
                new TypeComparer());
        }
Ejemplo n.º 20
0
        public void WhenConfiguredContainer_ThenCanResolvePolicy()
        {
            using (var container = new UnityContainer())
            {
                this.policyData.ConfigureContainer(container);

                var policy = container.Resolve <RuleDrivenPolicy>("policy");

                var method = new MethodImplementationInfo(StaticReflection.GetMethodInfo <object>(o => o.ToString()), StaticReflection.GetMethodInfo <object>(o => o.ToString()));

                Assert.AreEqual("policy", policy.Name);
                Assert.IsTrue(policy.Matches(method));
                Assert.AreEqual(0, policy.GetHandlersFor(method, container).Count());

                Assert.AreNotSame(policy, container.Resolve <RuleDrivenPolicy>("policy"));

                Assert.IsTrue(container.Registrations.Any(r => r.Name == "type-policy" && r.RegisteredType == typeof(IMatchingRule) && r.MappedToType == typeof(TypeMatchingRule)));
            }
        }
Ejemplo n.º 21
0
        public void ShouldNotDuplicateHandlersWhenCreatingViaInterface()
        {
            container
            .RegisterInstance <ICallHandler>("Handler1", new CallCountHandler())
            .RegisterInstance <ICallHandler>("Handler2", new CallCountHandler());

            RuleDrivenPolicy policy
                = new RuleDrivenPolicy("MatchesInterfacePolicy",
                                       new IMatchingRule[] { new TypeMatchingRule("ITwo") },
                                       new string[] { "Handler1", "Handler2" });

            PolicySet policies = new PolicySet(policy);
            MethodImplementationInfo twoInfo = new MethodImplementationInfo(
                typeof(ITwo).GetMethod("Two"), typeof(TwoType).GetMethod("Two"));

            List <ICallHandler> handlers
                = new List <ICallHandler>(policies.GetHandlersFor(twoInfo, container));

            Assert.AreEqual(2, handlers.Count);
        }
        public void Setup()
        {
            nothingSpecialMethod      = MakeMethodImpl <AttributeTestTarget>("NothingSpecial");
            doSomethingMethod         = MakeMethodImpl <AttributeTestTarget>("DoSomething");
            getCriticalInfoMethod     = MakeMethodImpl <AttributeTestTarget>("GetCriticalInformation");
            mustBeFastMethod          = MakeMethodImpl <AttributeTestTarget>("MustBeFast");
            getNameMethod             = new MethodImplementationInfo(null, typeof(AttributeTestTarget).GetProperty("Name").GetGetMethod());
            hasAttributeMethod        = MakeMethodImpl <SecondAttributeTestTarget>("HasAttribute");
            doesntHaveAttributeMethod = MakeMethodImpl <SecondAttributeTestTarget>("DoesntHaveAttribute");
            newMethod        = MakeMethodImpl <DerivedAttributeTestTarget>("ANewMethod");
            getNewNameMethod = new MethodImplementationInfo(null, StaticReflection.GetPropertyGetMethodInfo((DerivedAttributeTestTarget t) => t.Name));

            getItemMethod = new MethodImplementationInfo(null, StaticReflection.GetPropertyGetMethodInfo((DerivedAttributeTestTarget t) => t.Item));
            setItemMethod = new MethodImplementationInfo(null, StaticReflection.GetPropertySetMethodInfo((DerivedAttributeTestTarget t) => t.Item));

            getItemIntMethod = new MethodImplementationInfo(null, typeof(DerivedAttributeTestTarget).GetMethod("get_Item", new[] { typeof(int) }));
            setItemIntMethod = new MethodImplementationInfo(null, typeof(DerivedAttributeTestTarget).GetMethod("set_Item", new[] { typeof(int), typeof(object) }));

            getItemStringMethod = new MethodImplementationInfo(null, typeof(DerivedAttributeTestTarget).GetMethod("get_Item", new[] { typeof(string), typeof(double) }));
            setItemStringMethod = new MethodImplementationInfo(null, typeof(DerivedAttributeTestTarget).GetMethod("set_Item", new[] { typeof(string), typeof(double), typeof(object) }));
        }
Ejemplo n.º 23
0
        internal static IEnumerable <ICallHandler> CalculateHandlersFor(
            IEnumerable <InjectionPolicy> policies,
            MethodImplementationInfo member,
            IUnityContainer container)
        {
            List <ICallHandler> ordered    = new List <ICallHandler>();
            List <ICallHandler> nonOrdered = new List <ICallHandler>();

            foreach (InjectionPolicy p in policies)
            {
                foreach (ICallHandler handler in p.GetHandlersFor(member, container))
                {
                    if (handler.Order != 0)
                    {
                        bool inserted = false;
                        // add in order to ordered
                        for (int i = ordered.Count - 1; i >= 0; i--)
                        {
                            if (ordered[i].Order <= handler.Order)
                            {
                                ordered.Insert(i + 1, handler);
                                inserted = true;
                                break;
                            }
                        }
                        if (!inserted)
                        {
                            ordered.Insert(0, handler);
                        }
                    }
                    else
                    {
                        nonOrdered.Add(handler);
                    }
                }
            }
            ordered.AddRange(nonOrdered);
            return(ordered);
        }
Ejemplo n.º 24
0
        public void ShouldBeAbleToAddOnePolicy()
        {
            container
            .RegisterInstance <ICallHandler>("Handler1", new Handler1())
            .RegisterInstance <ICallHandler>("Handler2", new Handler2());

            PolicySet policies = new PolicySet();

            RuleDrivenPolicy p
                = new RuleDrivenPolicy(
                      "NameMatching",
                      new IMatchingRule[] { new MemberNameMatchingRule("ShouldBeAbleToAddOnePolicy") },
                      new string[] { "Handler1", "Handler2" });

            policies.Add(p);

            MethodImplementationInfo thisMember = GetMethodImplInfo <PolicySetFixture>("ShouldBeAbleToAddOnePolicy");
            List <ICallHandler>      handlers   =
                new List <ICallHandler>(policies.GetHandlersFor(thisMember, container));

            Assert.AreEqual(2, handlers.Count);
            Assert.IsTrue(typeof(Handler1) == handlers[0].GetType());
            Assert.IsTrue(typeof(Handler2) == handlers[1].GetType());
        }
Ejemplo n.º 25
0
 /// <summary>
 /// Gets the handlers that apply to the given member based on all policies in the <see cref="PolicySet"/>.
 /// </summary>
 /// <param name="member">Member to get handlers for.</param>
 /// <param name="container">The <see cref="IUnityContainer"/> to use when creating handlers,
 /// if necessary.</param>
 /// <returns>Collection of call handlers for <paramref name="member"/>.</returns>
 public IEnumerable <ICallHandler> GetHandlersFor(MethodImplementationInfo member, IUnityContainer container)
 {
     return(new List <ICallHandler>(CalculateHandlersFor(this, member, container)));
 }
 protected override bool DoesMatch(MethodImplementationInfo member)
 {
     Guard.ArgumentNotNull(member, "member");
     return((member.InterfaceMethodInfo != null && _attributeMatchRule.Matches(member.InterfaceMethodInfo)) ||
            _attributeMatchRule.Matches(member.ImplementationMethodInfo));
 }
Ejemplo n.º 27
0
 public abstract ICallHandler CreateHandler(IUnityContainer container, MethodImplementationInfo member);
Ejemplo n.º 28
0
 /// <summary>
 /// Derived classes implement this method to calculate if the policy
 /// will provide any handler to the specified member.
 /// </summary>
 /// <param name="member">Member to check.</param>
 /// <returns>true if policy applies to this member, false if not.</returns>
 protected abstract bool DoesMatch(MethodImplementationInfo member);
Ejemplo n.º 29
0
 /// <summary>
 /// Derived classes implement this method to supply the list of handlers for
 /// this specific member.
 /// </summary>
 /// <param name="member">Member to get handlers for.</param>
 /// <param name="container">The <see cref="IUnityContainer"/> to use when creating handlers,
 /// if necessary.</param>
 /// <returns>Enumerable collection of handlers for this method.</returns>
 protected abstract IEnumerable <ICallHandler> DoGetHandlersFor(MethodImplementationInfo member, IUnityContainer container);
Ejemplo n.º 30
0
 /// <summary>
 /// Checks if the rules in this policy match the given member info.
 /// </summary>
 /// <param name="member">MemberInfo to check against.</param>
 /// <returns>true if ruleset matches, false if it does not.</returns>
 public bool Matches(MethodImplementationInfo member)
 {
     return(DoesNotHaveNoPoliciesAttributeRule(member) &&
            DoesMatch(member));
 }