コード例 #1
0
        public void CanSetUpAPolicyWithNamedInjectedRulesAndHandlers()
        {
            IUnityContainer container = new UnityContainer();
            container.AddNewExtension<Interception>();

            container
                .Configure<Interception>()
                    .AddPolicy("foo")
                        .AddMatchingRule<AlwaysMatchingRule>("rule1")
                        .AddCallHandler<GlobalCountCallHandler>(
                            "handler1",
                            new InjectionConstructor("handler1"))
                        .AddCallHandler<GlobalCountCallHandler>(
                            "handler2",
                            new InjectionConstructor("handler2"),
                            new InjectionProperty("Order", 10));

            GlobalCountCallHandler.Calls.Clear();

            container
                .Configure<Interception>()
                    .SetInterceptorFor<Wrappable>("wrappable", new TransparentProxyInterceptor());

            Wrappable wrappable1 = container.Resolve<Wrappable>("wrappable");
            wrappable1.Method2();

            GlobalCountCallHandler handler1 = (GlobalCountCallHandler)container.Resolve<ICallHandler>("handler1");
            GlobalCountCallHandler handler2 = (GlobalCountCallHandler)container.Resolve<ICallHandler>("handler2");

            Assert.AreEqual(1, GlobalCountCallHandler.Calls["handler1"]);
            Assert.AreEqual(1, GlobalCountCallHandler.Calls["handler2"]);
            Assert.AreEqual(0, handler1.Order);
            Assert.AreEqual(10, handler2.Order);
        }
コード例 #2
0
        public void ConfigureContainerbyInjectingProperty()
        {
            IUnityContainer container = new UnityContainer();

            container.AddNewExtension <Interception>();
            GlobalCountCallHandler.Calls.Clear();
            container.Configure <Interception>().
            AddPolicy("myRDP").
            AddCallHandler <GlobalCountCallHandler>("handler1", new InjectionConstructor("myHandler1")).
            AddCallHandler <GlobalCountCallHandler>("hanlder3", new InjectionConstructor("myHandler3"), new InjectionProperty("Order", 1000)).
            AddMatchingRule <AlwaysMatchingRule>();
            container.Configure <Interception>().
            SetInterceptorFor <InterceptionClass>("interceptionclass", new TransparentProxyInterceptor());
            InterceptionClass ic = container.Resolve <InterceptionClass>("interceptionclass");

            ic.MethodA();

            GlobalCountCallHandler handler3 = (GlobalCountCallHandler)container.Resolve <ICallHandler>("hanlder3");

            Assert.AreEqual(1000, handler3.Order);

            Assert.AreEqual(1, GlobalCountCallHandler.Calls["myHandler1"]);
            Assert.AreEqual(1, GlobalCountCallHandler.Calls["myHandler3"]);
        }