示例#1
0
        public void TestDuplicateUnbinding()
        {
            Binder binder = new Binder();
            var equivalent = new EventEquivalent();
            List<Handler> handlerChain = new List<Handler>();

            binder.Bind(new SampleEvent1 { Foo = 1 }, new MethodHandler<SampleEvent1>(OnSampleEvent1));
            binder.Bind(new SampleEvent1 { Foo = 2 }, new MethodHandler<SampleEvent1>(OnSpecificSampleEvent1));

            binder.Unbind(new SampleEvent1 { Foo = 1 }, new MethodHandler<SampleEvent1>(OnSampleEvent1));
            binder.Unbind(new SampleEvent1 { Foo = 1 }, new MethodHandler<SampleEvent1>(OnSampleEvent1));

            handlerChain.Clear();
            Assert.AreEqual(0, binder.BuildHandlerChain(new SampleEvent1 { Foo = 1 }, equivalent, handlerChain));
            Assert.AreEqual(1, binder.BuildHandlerChain(new SampleEvent1 { Foo = 2 }, equivalent, handlerChain));
            Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSpecificSampleEvent1), handlerChain[0]);

            // with EventSink

            var sink = new SampleEventSink();
            sink.Bind(new SampleEvent1 { Foo = 1 }, sink.OnSampleEvent1);

            sink.Unbind(new SampleEvent1 { Foo = 1 }, sink.OnSampleEvent1);
            sink.Bind(new SampleEvent1 { Foo = 1 }, sink.OnSampleEvent1);

            handlerChain.Clear();
            Assert.AreEqual(0, binder.BuildHandlerChain(new SampleEvent1 { Foo = 1 }, equivalent, handlerChain));
            Assert.AreEqual(1, binder.BuildHandlerChain(new SampleEvent1 { Foo = 2 }, equivalent, handlerChain));
            Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSpecificSampleEvent1), handlerChain[0]);
        }
示例#2
0
        public void TestContainsByType()
        {
            var binder = new Binder();

            binder.Bind<MockClassToDepend>().ToSelf();
            binder.Bind<IMockInterface>().To<MockIClassWithAttributes>();

            var contains = binder.ContainsBindingFor(typeof(IMockInterface));

            Assert.AreEqual(true, contains);
        }
示例#3
0
        public void TestGetAll()
        {
            var binder = new Binder();

            binder.Bind(typeof(IMockInterface)).To<MockIClassWithAttributes>();
            binder.Bind<IMockInterface>().To<MockIClassWithAttributes>().As("test");

            var bindings = binder.GetBindings();

            Assert.AreEqual(2, bindings.Count);
        }
示例#4
0
        public void TestContainsByIdentifier()
        {
            var binder = new Binder();

            binder.Bind<IMockInterface>().To<MockIClass>();
            binder.Bind<IMockInterface>().To<MockIClassWithAttributes>().As("Identifier");

            var contains = binder.ContainsBindingFor("Identifier");

            Assert.AreEqual(true, contains);
        }
示例#5
0
        public void TestBinder()
        {
            var model = new ModelTestClass
            {
                Number = 1,
                Text = "First"
            };
            var control = new ControlTestClass
            {
                Text = "Wrong",
                Number = -1
            };

            var textProp = new BindableProperty<ControlTestClass, string>(control, o => o.Text, (o, action) => o.TextChanged += (sender, args) => action());

            int[] controlCounts = { 0 };
            textProp.PropertyChanged += (sender, args) => controlCounts[0]++;
            int[] modelCounts = { 0 };
            model.PropertyChanged += (sender, args) => modelCounts[0]++;

            var binder = new Binder<ModelTestClass, ControlTestClass, string, string>(m => m.Text, () => DataConverter<string>.EmptyConverter);

            var bInfo = binder.Bind(model, textProp, BindingMode.OneTime);
            TestOneTimeBindingInfo(bInfo, model, control, ref modelCounts[0], ref controlCounts[0]);
            bInfo.Unbind();

            model.Text = "First";
            modelCounts[0] = 0;
            control.Text = "Wrong";
            controlCounts[0] = 0;
            textProp = new BindableProperty<ControlTestClass, string>(control, o => o.Text, (o, action) => o.TextChanged += (sender, args) => action());
            bInfo = binder.Bind(model, textProp, BindingMode.OneWay);
            TestOneWayBindingInfo(bInfo, model, control, ref modelCounts[0], ref controlCounts[0]);
            bInfo.Unbind();

            model.Text = "Wrong";
            control.Text = "First";
            modelCounts[0] = 0;
            controlCounts[0] = 0;
            textProp = new BindableProperty<ControlTestClass, string>(control, o => o.Text, (o, action) => o.TextChanged += (sender, args) => action());
            bInfo = binder.Bind(model, textProp, BindingMode.OneWayToSource);
            TestOneWayToSourceBindingInfo(bInfo, model, control, modelCounts, controlCounts);
            bInfo.Unbind();

            model.Text = "First";
            modelCounts[0] = 0;
            control.Text = "Wrong";
            controlCounts[0] = 0;
            textProp = new BindableProperty<ControlTestClass, string>(control, o => o.Text, (o, action) => o.TextChanged += (sender, args) => action());
            bInfo = binder.Bind(model, textProp, BindingMode.TwoWay);
            TestTwoWayBindingInfo(bInfo, model, control, ref modelCounts[0], ref controlCounts[0]);
            bInfo.Unbind();
        }
示例#6
0
        public void TestBindNotAssignableInstanceTypeToInstance()
        {
            var binder = new Binder();

            var instance = new MockClassToDepend();
            binder.Bind<MockClassToDepend>().To(typeof(MockClassVerySimple), instance);
        }
示例#7
0
        public void TestAfterInject()
        {
            var eventCalled = false;

            IReflectionCache cache = new ReflectionCache();
            IBinder binder = new Binder();
            IInjector injector = new Injector(cache, binder);
            var instanceToInject = new MockClassVerySimple();

            injector.afterInject += delegate(IInjector source, ref object instance, ReflectedClass reflectedClass) {
                //The if below is just to avoid checking when injecting on MockIClass.
                if (reflectedClass.type != typeof(MockClassVerySimple)) return;

                Assert.AreEqual(injector, source);
                Assert.AreEqual(instanceToInject, instance);
                Assert.AreEqual(typeof(MockIClass), instanceToInject.field.GetType());

                eventCalled = true;
            };

            binder.Bind<IMockInterface>().To<MockIClass>();
            injector.Inject(instanceToInject);

            Assert.IsTrue(eventCalled);
        }
示例#8
0
        public void TestAfterResolve()
        {
            var eventCalled = false;

            IReflectionCache cache = new ReflectionCache();
            IBinder binder = new Binder();
            IInjector injector = new Injector(cache, binder);
            IMockInterface resolvedInstance = null;

            injector.afterResolve += delegate(IInjector source,
                Type type,
                InjectionMember member,
                object parentInstance,
              	object identifier,
                ref object resolutionInstance) {
                Assert.AreEqual(injector, source);
                Assert.AreEqual(typeof(IMockInterface), type);
                Assert.AreEqual(InjectionMember.None, member);
                Assert.IsNull(parentInstance);
                Assert.IsNull(identifier);
                Assert.IsNotNull(resolutionInstance);

                resolvedInstance = (IMockInterface)resolutionInstance;
                eventCalled = true;

                return false;
            };

            binder.Bind<IMockInterface>().To<MockIClass>();
            var instance = injector.Resolve<IMockInterface>();

            Assert.IsTrue(eventCalled);
            Assert.AreEqual(typeof(MockIClass), instance.GetType());
            Assert.AreEqual(resolvedInstance, instance);
        }
示例#9
0
        public void TestGetBindingsForGenerics()
        {
            var binder = new Binder();

            binder.Bind<MockClassToDepend>().ToSelf();
            binder.Bind(typeof(IMockInterface)).To<MockIClassWithAttributes>();
            binder.Bind<IMockInterface>().To<MockIClassWithAttributes>().As("test");

            var bindings = binder.GetBindingsFor<IMockInterface>();

            Assert.AreEqual(2, bindings.Count);
            Assert.AreEqual(typeof(IMockInterface), bindings[0].type);
            Assert.IsNull(bindings[0].identifier);
            Assert.AreEqual(typeof(IMockInterface), bindings[1].type);
            Assert.AreEqual("test", bindings[1].identifier);
        }
示例#10
0
        public void TestBinding()
        {
            Binder binder = new Binder();
            var equivalent = new EventEquivalent();

            binder.Bind(new SampleEvent1(), new MethodHandler<SampleEvent1>(OnSampleEvent1));

            var e1 = new SampleEvent1();
            var e2 = new SampleEvent1 { Foo = 1 };
            var e3 = new SampleEvent1 { Foo = 1, Bar = "bar" };

            List<Handler> handlerChain = new List<Handler>();

            handlerChain.Clear();
            Assert.AreEqual(1, binder.BuildHandlerChain(e1, equivalent, handlerChain));
            Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSampleEvent1), handlerChain[0]);
            handlerChain.Clear();
            Assert.AreEqual(1, binder.BuildHandlerChain(e2, equivalent, handlerChain));
            Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSampleEvent1), handlerChain[0]);
            handlerChain.Clear();
            Assert.AreEqual(1, binder.BuildHandlerChain(e3, equivalent, handlerChain));
            Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSampleEvent1), handlerChain[0]);

            binder.Unbind(new SampleEvent1(), new MethodHandler<SampleEvent1>(OnSampleEvent1));

            handlerChain.Clear();
            Assert.AreEqual(0, binder.BuildHandlerChain(e1, equivalent, handlerChain));
            handlerChain.Clear();
            Assert.AreEqual(0, binder.BuildHandlerChain(e2, equivalent, handlerChain));
            handlerChain.Clear();
            Assert.AreEqual(0, binder.BuildHandlerChain(e3, equivalent, handlerChain));
        }
示例#11
0
        public void TestBindNotAssignableKeyTypeToInstance()
        {
            var binder = new Binder();

            var instance = new MockClassToDepend();
            binder.Bind<IMockInterface>().To<MockClassToDepend>(instance);
        }
示例#12
0
        public void TestBindingToMultipleSingletonByInstance()
        {
            var binder = new Binder();

            binder.Bind<MockIClassWithAttributes>().To(new MockIClassWithAttributes());
            binder.Bind<IMockInterface>().ToSingleton<MockIClassWithAttributes>();

            var bindings1 = binder.GetBindingsFor<MockIClassWithAttributes>();
            var bindings2 = binder.GetBindingsFor<IMockInterface>();

            Assert.AreEqual(1, bindings1.Count);
            Assert.AreEqual(1, bindings2.Count);
            Assert.AreEqual(BindingInstance.Singleton, bindings1[0].instanceType);
            Assert.AreEqual(BindingInstance.Singleton, bindings2[0].instanceType);
            Assert.AreEqual(typeof(MockIClassWithAttributes), bindings1[0].type);
            Assert.AreEqual(typeof(IMockInterface), bindings2[0].type);
            Assert.AreEqual(bindings1[0].value, bindings1[0].value);
        }
示例#13
0
        public void TestBindingToFactoryTypeFromInterface()
        {
            var binder = new Binder();

            var type = typeof(MockFactory);
            binder.Bind<IMockInterface>().ToFactory(type);
            var bindings = binder.GetBindingsFor<IMockInterface>();

            Assert.AreEqual(1, bindings.Count);
            Assert.AreEqual(BindingInstance.Factory, bindings[0].instanceType);
            Assert.AreEqual(typeof(IMockInterface), bindings[0].type);
            Assert.AreEqual(type, bindings[0].value);
        }
示例#14
0
        public void TestBindingToInstanceFromInterface()
        {
            var binder = new Binder();

            var instance = new MockIClassWithAttributes();
            binder.Bind<IMockInterface>().To<MockIClassWithAttributes>(instance);
            var bindings = binder.GetBindingsFor<IMockInterface>();

            Assert.AreEqual(1, bindings.Count);
            Assert.AreEqual(BindingInstance.Singleton, bindings[0].instanceType);
            Assert.AreEqual(typeof(IMockInterface), bindings[0].type);
            Assert.AreEqual(instance, bindings[0].value);
        }
示例#15
0
        public void TestDuplicateBinding()
        {
            Binder binder = new Binder();
            List<Handler> handlerChain = new List<Handler>();

            binder.Bind(new SampleEvent1 { Foo = 1 }, new MethodHandler<SampleEvent1>(OnSampleEvent1));
            binder.Bind(new SampleEvent1 { Foo = 1 }, new MethodHandler<SampleEvent1>(OnSampleEvent1));

            binder.Unbind(new SampleEvent1 { Foo = 1 }, new MethodHandler<SampleEvent1>(OnSampleEvent1));

            Assert.AreEqual(0, binder.BuildHandlerChain(new SampleEvent1 { Foo = 1 }, handlerChain));

            // with EventSink

            var sink = new SampleEventSink();
            sink.Bind(new SampleEvent1 { Foo = 1 }, sink.OnSampleEvent1);
            sink.Bind(new SampleEvent1 { Foo = 1 }, sink.OnSampleEvent1);

            sink.Unbind(new SampleEvent1 { Foo = 1 }, sink.OnSampleEvent1);

            handlerChain.Clear();
            Assert.AreEqual(0, binder.BuildHandlerChain(new SampleEvent1 { Foo = 1 }, handlerChain));
        }
示例#16
0
        public void TestBeforeAddBindingEvent()
        {
            var eventCalled = false;

            IBinder binder = new Binder();
            binder.beforeAddBinding += delegate(IBinder source, ref BindingInfo binding) {
                Assert.AreEqual(binder, source);
                Assert.AreEqual(typeof(IMockInterface), binding.type);
                Assert.AreEqual(0, binder.GetBindings().Count);

                eventCalled = true;
            };

            binder.Bind<IMockInterface>().To<MockIClass>();

            Assert.IsTrue(eventCalled);
        }
示例#17
0
        public void TestBasicPerformance()
        {
            Binder binder = new Binder();
            var equivalent = new EventEquivalent();
            List<Handler> handlerChain = new List<Handler>();

            binder.Bind(new SampleEvent1 { Foo = 1 }, new MethodHandler<SampleEvent1>(OnSampleEvent1));

            // const int testCount = 1000000;
            const int testCount = 1;

            for (var i = 0; i < testCount; ++i)
            {
                binder.BuildHandlerChain(new SampleEvent1 {Foo = 1}, equivalent, handlerChain);
            }
            // 1,000,000 counts in 342 ms in release mode
        }
示例#18
0
        public void TestAfterRemoveBindingEvent()
        {
            var eventCalled = false;

            IBinder binder = new Binder();
            binder.afterRemoveBinding += delegate(IBinder source, Type type, IList<BindingInfo> bindings) {
                Assert.AreEqual(binder, source);
                Assert.AreEqual(typeof(IMockInterface), type);
                Assert.AreEqual(1, bindings.Count);
                Assert.AreEqual(typeof(MockIClass), bindings[0].value);
                Assert.AreEqual(0, binder.GetBindings().Count);

                eventCalled = true;
            };

            binder.Bind<IMockInterface>().To<MockIClass>();
            binder.Unbind<IMockInterface>();

            Assert.IsTrue(eventCalled);
        }
        public void ShouldBindIndexedField()
        {
            var binder = new Binder<UniversalStub>();
            var stub = new UniversalStub();
            stub.Dictionary = new ObservableDictionary<string>();

            binder.Bind(x => x.Dictionary["test"]).To(x => x.String);

            using (binder.Attach(stub))
            {
                stub.String.ShouldBe(null);

                using (stub.VerifyChangedOnce("String"))
                {
                    stub.Dictionary.Add("test", "a");
                }

                stub.String.ShouldBe("a");
            }
        }
        public void ShouldBindAggregatedCollectionOfValueTypes()
        {
            var binder = new Binder<AggregatedCollection<int>>();
            binder.Bind(x => x.Sum()).To(x => x.Aggregate);
            var collection = new AggregatedCollection<int>();

            using (binder.Attach(collection))
            {
                collection.Aggregate.ShouldBe(0);
                using (collection.VerifyChangedOnce("Aggregate"))
                {
                    collection.Add(1);
                }
                collection.Aggregate.ShouldBe(1);

                using (collection.VerifyChangedOnce("Aggregate"))
                {
                    collection.Add(2);
                }
                collection.Aggregate.ShouldBe(3);

                using (collection.VerifyChangedOnce("Aggregate"))
                {
                    collection[0] = 3;
                }
                collection.Aggregate.ShouldBe(5);

                using (collection.VerifyChangedOnce("Aggregate"))
                {
                    collection.RemoveAt(1);
                }
                collection.Aggregate.ShouldBe(3);

                using (collection.VerifyChangedOnce("Aggregate"))
                {
                    collection.Clear();
                }
                collection.Aggregate.ShouldBe(0);
            }
        }
 public MvxBindableTableViewCell(string bindingText, IntPtr handle)
     : base(handle)
 {
     InitialiseImageHelper();
     _bindings = Binder.Bind(null, this, bindingText).ToList();
 }
 public MvxBindableTableViewCell(IEnumerable <MvxBindingDescription> bindingDescriptions, IntPtr handle)
     : base(handle)
 {
     InitialiseImageHelper();
     _bindings = Binder.Bind(null, this, bindingDescriptions).ToList();
 }
示例#23
0
        public void TestUnbindByType()
        {
            var binder = new Binder();

            binder.Bind<MockClassToDepend>().ToSelf();
            binder.Bind<IMockInterface>().To<MockIClassWithAttributes>();
            binder.Unbind(typeof(IMockInterface));

            var bindings = binder.GetBindings();

            Assert.AreEqual(1, bindings.Count);
            Assert.AreEqual(typeof(MockClassToDepend), bindings[0].type);
        }
示例#24
0
 public void Render()
 {
     Binder?.Bind();
     Renderer?.Render();
 }
示例#25
0
 public virtual void OnViewModelChanged(IViewModel newValue)
 {
     Binder.Unbind();
     Binder.Bind(newValue);
 }
示例#26
0
        /// <summary>
        /// Generates the appropriate MSI file for the package.
        /// </summary>
        /// <param name="sourceDoc">WiX document to create MSI from.</param>
        /// <param name="outputFile">File path for the MSI file.</param>
        /// <returns>True if generation works, false if anything goes wrong.</returns>
        private bool GenerateMsi(XmlDocument sourceDoc, string outputFile)
        {
            // Create the Compiler.
            Compiler compiler = new Compiler();
            compiler.Message += this.core.MessageEventHandler;

            // Compile the source document.
            Intermediate intermediate = compiler.Compile(sourceDoc);
            if (intermediate == null)
            {
                return false;
            }

            // Create the variable resolver that will be used in the Linker and Binder.
            WixVariableResolver wixVariableResolver = new WixVariableResolver();
            wixVariableResolver.Message += this.core.MessageEventHandler;

            // Create the Linker.
            Linker linker = new Linker();
            linker.Message += this.core.MessageEventHandler;
            linker.WixVariableResolver = wixVariableResolver;

            // Load the isolatedapp.wixlib.
            System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
            Library appLib = LoadLibraryHelper(assembly, "Microsoft.Tools.WindowsInstallerXml.Extensions.OfficeAddin.Data.OfficeAddin.wixlib", linker.TableDefinitions);

            // Link the compiled source document and the isolatedapp.wixlib together.
            SectionCollection sections = new SectionCollection();
            sections.AddRange(intermediate.Sections);
            sections.AddRange(appLib.Sections);

            Output output = linker.Link(sections);
            if (output == null)
            {
                return false;
            }

            // Tweak the compiled output to add a few GUIDs for Components from the oaddin.wixlib.
            Table components = output.Tables["Component"];
            foreach (Row row in components.Rows)
            {
                switch ((string)row[0])
                {
                    case "ThisApplicationVersionRegistryKeyComponent":
                        row[1] = Guid.NewGuid().ToString("B");
                        break;
                    case "ThisApplicationCacheFolderComponent":
                        row[1] = Guid.NewGuid().ToString("B");
                        break;
                    case "ThisApplicationShortcutComponent":
                        row[1] = Guid.NewGuid().ToString("B");
                        break;
                }
            }

            // Bind the final output.
            Binder binder = new Binder();
            binder.FileManager = new BinderFileManager();
            binder.FileManager.SourcePaths.Add(Path.GetDirectoryName(outputFile));
            binder.FileManager.SourcePaths.Add(this.source);
            binder.FileManager.SourcePaths.Add(Path.GetDirectoryName(assembly.Location));
            binder.Message += this.core.MessageEventHandler;
            binder.WixVariableResolver = wixVariableResolver;
            return binder.Bind(output, outputFile);
        }
示例#27
0
        public static void AddBindings(this IMvxBindingContextOwner view, object target, string bindingText, object clearKey = null)
        {
            var bindings = Binder.Bind(view.BindingContext.DataContext, target, bindingText);

            view.AddBindings(bindings, clearKey);
        }
示例#28
0
 internal override Node Bind(Binder b)
 {
     b.Bind(Args);
     return(null);
 }
示例#29
0
 void IBindable.Bind()
 {
     Binder?.Bind();
 }
示例#30
0
 public void Configure(Binder binder)
 {
     binder.Bind <ApplicationACLsManager>().ToInstance(new ApplicationACLsManager(new Configuration
                                                                                      ()));
 }
示例#31
0
 internal override Node Bind(Binder b)
 {
     b.Bind(ref Expr);
     Expr.RequireGetAccess();
     return(null);
 }
示例#32
0
        public void TestGetBindingsForIdentifier()
        {
            var binder = new Binder();

            binder.Bind<MockClassToDepend>().ToSelf();
            binder.Bind(typeof(MockIClassWithAttributes)).ToSelf();
            binder.Bind<IMockInterface>().To<MockIClass>();
            binder.Bind<IMockInterface>().To<MockIClassWithAttributes>().As("Identifier");
            binder.Bind<IMockInterface>().To<MockIClassWithoutAttributes>().As("Identifier");

            var bindings = binder.GetBindingsFor("Identifier");

            Assert.AreEqual(2, bindings.Count);
            Assert.AreEqual(typeof(IMockInterface), bindings[0].type);
            Assert.AreEqual(typeof(MockIClassWithAttributes), bindings[0].value);
            Assert.AreEqual("Identifier", bindings[0].identifier);
            Assert.AreEqual(typeof(IMockInterface), bindings[1].type);
            Assert.AreEqual(typeof(MockIClassWithoutAttributes), bindings[1].value);
            Assert.AreEqual("Identifier", bindings[1].identifier);
        }
示例#33
0
        public void TestBindToInterfaceSingleton()
        {
            var binder = new Binder();

            binder.Bind<IMockInterface>().ToSingleton();
        }
示例#34
0
        public void TestHandlerChainBuilding()
        {
            Binder binder = new Binder();
            var equivalent = new EventEquivalent();

            binder.Bind(new SampleEvent1(), new MethodHandler<SampleEvent1>(OnSampleEvent1));
            binder.Bind(new SampleEvent1 { Foo = 1 }, new MethodHandler<SampleEvent1>(OnSpecificSampleEvent1));
            binder.Bind(new Event(), new MethodHandler<Event>(OnEvent));

            var e1 = new SampleEvent1();
            var e2 = new SampleEvent1 { Foo = 1 };
            var e3 = new SampleEvent1 { Foo = 1, Bar = "bar" };
            var e4 = new SampleEvent1 { Foo = 2 };
            var e5 = new SampleEvent2 { Foo = 1, Bar = "bar" };
            var e6 = new SampleEvent2 { Foo = 2, Bar = "bar" };

            List<Handler> handlerChain = new List<Handler>();

            handlerChain.Clear();
            Assert.AreEqual(2, binder.BuildHandlerChain(e1, equivalent, handlerChain));
            Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSampleEvent1), handlerChain[0]);
            Assert.AreEqual(new MethodHandler<Event>(OnEvent), handlerChain[1]);

            handlerChain.Clear();
            Assert.AreEqual(3, binder.BuildHandlerChain(e2, equivalent, handlerChain));
            Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSampleEvent1), handlerChain[0]);
            Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSpecificSampleEvent1), handlerChain[1]);
            Assert.AreEqual(new MethodHandler<Event>(OnEvent), handlerChain[2]);

            handlerChain.Clear();
            Assert.AreEqual(3, binder.BuildHandlerChain(e3, equivalent, handlerChain));
            Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSampleEvent1), handlerChain[0]);
            Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSpecificSampleEvent1), handlerChain[1]);
            Assert.AreEqual(new MethodHandler<Event>(OnEvent), handlerChain[2]);

            handlerChain.Clear();
            Assert.AreEqual(2, binder.BuildHandlerChain(e4, equivalent, handlerChain));
            Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSampleEvent1), handlerChain[0]);
            Assert.AreEqual(new MethodHandler<Event>(OnEvent), handlerChain[1]);

            handlerChain.Clear();
            Assert.AreEqual(3, binder.BuildHandlerChain(e5, equivalent, handlerChain));
            Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSampleEvent1), handlerChain[0]);
            Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSpecificSampleEvent1), handlerChain[1]);
            Assert.AreEqual(new MethodHandler<Event>(OnEvent), handlerChain[2]);

            handlerChain.Clear();
            Assert.AreEqual(2, binder.BuildHandlerChain(e6, equivalent, handlerChain));
            Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSampleEvent1), handlerChain[0]);
            Assert.AreEqual(new MethodHandler<Event>(OnEvent), handlerChain[1]);

            binder.Unbind(new SampleEvent1(), new MethodHandler<SampleEvent1>(OnSampleEvent1));

            handlerChain.Clear();
            Assert.AreEqual(1, binder.BuildHandlerChain(e1, equivalent, handlerChain));
            Assert.AreEqual(new MethodHandler<Event>(OnEvent), handlerChain[0]);

            binder.Unbind(new Event(), new MethodHandler<Event>(OnEvent));

            handlerChain.Clear();
            Assert.AreEqual(1, binder.BuildHandlerChain(e2, equivalent, handlerChain));
            Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSpecificSampleEvent1), handlerChain[0]);

            handlerChain.Clear();
            Assert.AreEqual(1, binder.BuildHandlerChain(e3, equivalent, handlerChain));
            Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSpecificSampleEvent1), handlerChain[0]);

            handlerChain.Clear();
            Assert.AreEqual(0, binder.BuildHandlerChain(e4, equivalent, handlerChain));
        }
示例#35
0
        public void TestBindToInterfaceTransient()
        {
            var binder = new Binder();

            binder.Bind<IMockInterface>().To<IMockInterface>();
        }
        public IEnumerable <object> BuildCallingArguments(RequestContext context, IApiMethodCall methodToCall)
        {
            var callArg       = new List <object>();
            var requestParams = GetRequestParams(context);

            var methodParams = methodToCall.GetParams().Where(x => !x.IsRetval).OrderBy(x => x.Position);


            foreach (var parameterInfo in methodParams)
            {
                if (requestParams[parameterInfo.Name] != null)
                {
                    //convert
                    var values = requestParams.GetValues(parameterInfo.Name);
                    if (values != null && values.Any())
                    {
                        if (Binder.IsCollection(parameterInfo.ParameterType))
                        {
                            callArg.Add(Binder.Bind(parameterInfo.ParameterType, requestParams, parameterInfo.Name));
                            continue; //Go to next loop
                        }

                        try
                        {
                            callArg.Add(ConvertUtils.GetConverted(values.First(), parameterInfo)); //NOTE; Get first value!
                        }
                        catch (ApiArgumentMismatchException)
                        {
                            //Failed to convert. Try bind
                            callArg.Add(Binder.Bind(parameterInfo.ParameterType, requestParams, parameterInfo.Name));
                        }
                    }
                }
                else
                {
                    //try get request param first. It may be form\url-encoded
                    if (!"GET".Equals(context.HttpContext.Request.HttpMethod, StringComparison.InvariantCultureIgnoreCase) && !string.IsNullOrEmpty(context.HttpContext.Request[parameterInfo.Name]))
                    {
                        //Drop to
                        callArg.Add(ConvertUtils.GetConverted(context.HttpContext.Request[parameterInfo.Name], parameterInfo));
                    }
                    else if (parameterInfo.ParameterType == typeof(ContentType) && !string.IsNullOrEmpty(context.HttpContext.Request.ContentType))
                    {
                        callArg.Add(new ContentType(context.HttpContext.Request.ContentType));
                    }
                    else if (parameterInfo.ParameterType == typeof(ContentDisposition) && !string.IsNullOrEmpty(context.HttpContext.Request.Headers["Content-Disposition"]))
                    {
                        var disposition = new ContentDisposition(context.HttpContext.Request.Headers["Content-Disposition"]);
                        disposition.FileName = HttpUtility.UrlDecode(disposition.FileName); //Decode uri name
                        callArg.Add(disposition);
                    }
                    else if (parameterInfo.ParameterType.IsSubclassOf(typeof(HttpPostedFile)) && context.HttpContext.Request.Files[parameterInfo.Name] != null)
                    {
                        callArg.Add(context.HttpContext.Request.Files[parameterInfo.Name]);
                    }
                    else if (Binder.IsCollection(parameterInfo.ParameterType) && parameterInfo.ParameterType.IsGenericType && parameterInfo.ParameterType.GetGenericArguments().First() == typeof(HttpPostedFileBase))
                    {
                        //File catcher
                        var files = new List <HttpPostedFileBase>(context.HttpContext.Request.Files.Count);
                        files.AddRange(from string key in context.HttpContext.Request.Files select context.HttpContext.Request.Files[key]);
                        callArg.Add(files);
                    }
                    else
                    {
                        if (parameterInfo.ParameterType.IsSubclassOf(typeof(Stream)) || parameterInfo.ParameterType == typeof(Stream))
                        {
                            //First try get files
                            var file = context.HttpContext.Request.Files[parameterInfo.Name];
                            callArg.Add(file != null ? file.InputStream : context.HttpContext.Request.InputStream);
                        }
                        else
                        {
                            //Try bind
                            //Note: binding moved here
                            if (IsTypeBindable(parameterInfo.ParameterType))
                            {
                                //Custom type
                                var binded = Binder.Bind(parameterInfo.ParameterType,
                                                         requestParams,
                                                         parameterInfo.Name);

                                if (binded != null)
                                {
                                    callArg.Add(binded);
                                    continue; //Go to next loop
                                }
                            }
                            //Create null
                            var obj = parameterInfo.ParameterType.IsValueType ? Activator.CreateInstance(parameterInfo.ParameterType) : null;
                            callArg.Add(obj);
                        }
                    }
                }
            }
            return(callArg);
        }
示例#37
0
        public void TestUnbindByIdentifier()
        {
            var binder = new Binder();

            binder.Bind<MockClassToDepend>().ToSelf();
            binder.Bind<IMockInterface>().To<MockIClassWithAttributes>().As("Mock1");
            binder.Bind<IMockInterface>().To<MockIClassWithAttributes>().As("Mock2");
            binder.Unbind("Mock1");

            var bindings = binder.GetBindings();

            Assert.AreEqual(2, bindings.Count);
            Assert.AreEqual(typeof(MockClassToDepend), bindings[0].type);
            Assert.AreEqual(typeof(IMockInterface), bindings[1].type);
            Assert.AreEqual("Mock2", bindings[1].identifier);
        }
示例#38
0
        public void TestBindingEvaluation()
        {
            var eventCalled = false;

            IReflectionCache cache = new ReflectionCache();
            IBinder binder = new Binder();
            IInjector injector = new Injector(cache, binder);

            injector.bindingEvaluation += delegate(IInjector source, ref BindingInfo binding) {
                Assert.AreEqual(injector, source);
                Assert.NotNull(binding);

                eventCalled = true;

                return new MockIClassWithoutAttributes();
            };

            binder.Bind<IMockInterface>().To<MockIClass>();
            var instance = injector.Resolve<IMockInterface>();

            Assert.IsTrue(eventCalled);
            Assert.AreEqual(typeof(MockIClassWithoutAttributes), instance.GetType());
        }
示例#39
0
        public void TestBindNotAssignableKeyTypeToTransient()
        {
            var binder = new Binder();

            binder.Bind<IMockInterface>().To<MockClassToDepend>();
        }
示例#40
0
 public void Bind(IRestApiClient apiClient)
 {
     _binder.Bind(apiClient);
 }
示例#41
0
        public void TestBindToInterfaceSelf()
        {
            var binder = new Binder();

            binder.Bind<IMockInterface>().ToSelf();
        }