Exemple #1
0
        public static void Check()
        {
            if (_checked)
            {
                return;
            }

            _checked = true;

            // if Application.Current == null Forms is in design mode
            if (Application.Current != null)
            {
                return;
            }

            if (MvxSingleton <IMvxIoCProvider> .Instance == null)
            {
                var iocProvider = MvxIoCProvider.Initialize();
                Mvx.RegisterSingleton(iocProvider);
            }

            if (!Mvx.CanResolve <IMvxBindingParser>())
            {
                //We might want to look into returning the platform specific Forms binder
                var builder = new MvxBindingBuilder();
                builder.DoRegistration();
            }
        }
        public static void Check()
        {
            if (_checked)
            {
                return;
            }

            _checked = true;

            if (!(bool)DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue)
            {
                return;
            }

            if (MvxSingleton <IMvxIoCProvider> .Instance == null)
            {
                var iocProvider = MvxIoCProvider.Initialize();
                Mvx.IoCProvider.RegisterSingleton(iocProvider);
            }

            if (!Mvx.IoCProvider.CanResolve <IMvxBindingParser>())
            {
                var builder = new MvxWindowsBindingBuilder(MvxWindowsBindingBuilder.BindingType.MvvmCross);
                builder.DoRegistration();
            }
        }
Exemple #3
0
        public static void Check()
        {
            if (_checked)
            {
                return;
            }

            _checked = true;

            if (!IsDesignTime)
            {
                return;
            }

            try
            {
                if (MvxSingleton <IMvxIoCProvider> .Instance == null)
                {
                    var iocProvider = MvxIoCProvider.Initialize();
                    Mvx.IoCProvider.RegisterSingleton(iocProvider);
                }

                if (!Mvx.IoCProvider.CanResolve <IMvxBindingParser>())
                {
                    //We might want to look into returning the platform specific Forms binder
                    var builder = new MvxBindingBuilder();
                    builder.DoRegistration();
                }
            }
            catch
            {
                // ignore
            }
        }
        public void TryResolve_WithAttrInjection_AttrMarkedProperiesGetInjected()
        {
            MvxSingleton.ClearAllSingletons();
            var options = new MvxIocOptions
            {
                PropertyInjectorOptions = new MvxPropertyInjectorOptions()
                {
                    InjectIntoProperties = MvxPropertyInjection.MvxInjectInterfaceProperties
                }
            };
            var instance = MvxIoCProvider.Initialize(options);

            Mvx.IoCProvider.RegisterType <IA, A>();
            Mvx.IoCProvider.RegisterType <IB, B>();
            Mvx.IoCProvider.RegisterType <IC, C>();

            IA  a;
            var result = Mvx.IoCProvider.TryResolve(out a);

            Assert.True(result);
            Assert.NotNull(a);
            Assert.IsType <A>(a);
            var castA = (A)a;

            Assert.NotNull(castA.B);
            Assert.IsType <B>(castA.B);
            Assert.Null(castA.C);
            Assert.Null(castA.BNever);
            Assert.Null(castA.CNever);
        }
Exemple #5
0
        public static void Check()
        {
            if (_checked)
            {
                return;
            }

            _checked = true;

            if (!DesignMode.DesignModeEnabled)
            {
                return;
            }

            if (MvxSingleton <IMvxIoCProvider> .Instance == null)
            {
                var iocProvider = MvxIoCProvider.Initialize();
                Mvx.IoCProvider.RegisterSingleton(iocProvider);
            }

            if (!Mvx.IoCProvider.CanResolve <IMvxBindingParser>())
            {
                var builder = new MvxWindowsBindingBuilder(MvxWindowsBindingBuilder.BindingType.MvvmCross);
                builder.DoRegistration();
            }
        }
Exemple #6
0
        public void IocConstruct_WithAnonymousTypeArguments_CreatesObject()
        {
            MvxSingleton.ClearAllSingletons();
            var instance = MvxIoCProvider.Initialize();

            var c = new C2();

            instance.IoCConstruct <B>(new { c });
        }
Exemple #7
0
        public void Non_generic_RegisterType_with_constructor_throws_if_constructor_returns_incompatible_value()
        {
            MvxSingleton.ClearAllSingletons();
            var instance = MvxIoCProvider.Initialize();

            instance.RegisterType(typeof(IC), () => 36);

            Assert.Throws <MvxIoCResolveException>(() => {
                var c1 = Mvx.Resolve <IC>();
            });
        }
 public virtual void ClearAll(IMvxIocOptions options = null)
 {
     // fake set up of the IoC
     Reset();
     Ioc = MvxIoCProvider.Initialize(options ?? CreateIocOptions());
     Ioc.RegisterSingleton(Ioc);
     CreateLog();
     InitializeSingletonCache();
     InitializeMvxSettings();
     AdditionalSetup();
 }
            static TheConvertMethod()
            {
                var log         = Substitute.For <IMvxLog>();
                var logProvider = Substitute.For <IMvxLogProvider>();

                var ioc = MvxIoCProvider.Initialize();

                ioc.RegisterSingleton(ioc);
                ioc.RegisterSingleton(logProvider);

                logProvider.GetLogFor(Arg.Any <Type>()).Returns(log);
            }
Exemple #10
0
        public static void Resolves_unsuccessfully_when_registered_open_generic_with_one_generic_parameter_that_was_not_registered()
        {
            var instance = MvxIoCProvider.Initialize();

            ((MvxIoCProvider)instance).CleanAllResolvers();

            IOG <C2> toResolve = null;

            var isResolved = Mvx.TryResolve <IOG <C2> >(out toResolve);

            Assert.False(isResolved);
            Assert.Null(toResolve);
        }
Exemple #11
0
        public void IocConstruct_WithDictionaryArguments_CreatesObject()
        {
            MvxSingleton.ClearAllSingletons();
            var instance = MvxIoCProvider.Initialize();

            var c         = new C2();
            var arguments = new Dictionary <string, object>
            {
                ["c"] = c
            };

            instance.IoCConstruct <B>(arguments);
        }
        protected MvxDesignTimeHelper()
        {
            if (!IsInDesignTool)
            {
                return;
            }

            if (MvxSingleton <IMvxIoCProvider> .Instance == null)
            {
                var iocProvider = MvxIoCProvider.Initialize();
                Mvx.IoCProvider.RegisterSingleton(iocProvider);
            }
        }
Exemple #13
0
        public void IocConstruct_WithMultipleTypedArguments_ThrowsFailedToFindCtor()
        {
            MvxSingleton.ClearAllSingletons();
            var instance = MvxIoCProvider.Initialize();

            var title    = "The title";
            var subtitle = "The subtitle";
            var enabled  = true;

            Assert.Throws <MvxIoCResolveException>(() => {
                var d = instance.IoCConstruct <D>(title, subtitle, enabled);
            });
        }
Exemple #14
0
        public void IocConstruct_WithMultipleTypedArguments_CreatesObject()
        {
            MvxSingleton.ClearAllSingletons();
            var instance = MvxIoCProvider.Initialize();

            var title   = "The title";
            var amount  = 5;
            var enabled = true;

            var d = instance.IoCConstruct <D>(title, amount, enabled);

            Assert.Equal(title, d.Title);
            Assert.Equal(amount, d.Amount);
            Assert.Equal(enabled, d.Enabled);
        }
Exemple #15
0
        public void browser_service_is_singleton()
        {
            // arrange
            IMvxIoCProvider ioc   = MvxIoCProvider.Initialize();
            var             setup = new Setup();

            // act
            setup.Initialize(ioc);

            // assert
            var service1 = ioc.Resolve <IBrowserService>();
            var service2 = ioc.Resolve <IBrowserService>();

            service1.Should().Be(service2);
        }
Exemple #16
0
        public void TryResolve_NonCircularRegistration_ReturnsTrue()
        {
            MvxSingleton.ClearAllSingletons();
            var instance = MvxIoCProvider.Initialize();

            Mvx.LazyConstructAndRegisterSingleton <IA, A>();
            Mvx.LazyConstructAndRegisterSingleton <IB, B>();
            Mvx.LazyConstructAndRegisterSingleton <IC, C2>();

            IA  a;
            var result = Mvx.TryResolve(out a);

            Assert.True(result);
            Assert.NotNull(a);
        }
Exemple #17
0
        public void Non_generic_RegisterType_with_constructor_creates_different_objects()
        {
            MvxSingleton.ClearAllSingletons();
            var instance = MvxIoCProvider.Initialize();

            instance.RegisterType(typeof(IC), () => new C2());

            var c1 = Mvx.Resolve <IC>();
            var c2 = Mvx.Resolve <IC>();

            Assert.NotNull(c1);
            Assert.NotNull(c2);

            Assert.NotEqual(c1, c2);
        }
Exemple #18
0
        public static void Resolves_successfully_when_registered_closed_generic_with_two_generic_type_parameter()
        {
            var instance = MvxIoCProvider.Initialize();

            ((MvxIoCProvider)instance).CleanAllResolvers();

            instance.RegisterType(typeof(IOG2 <C2, C>), typeof(OG2 <C2, C>));

            IOG2 <C2, C> toResolve = null;

            Mvx.TryResolve <IOG2 <C2, C> >(out toResolve);

            Assert.NotNull(toResolve);
            Assert.Contains(toResolve.GetType().GetTypeInfo().ImplementedInterfaces, i => i == typeof(IOG2 <C2, C>));
            Assert.True(toResolve.GetType() == typeof(OG2 <C2, C>));
        }
Exemple #19
0
        public static void Initialize()
        {
            if (!IsInDesignTime)
            {
                return;
            }

            if (MvxSingleton <IMvxIoCProvider> .Instance == null)
            {
                var iocProvider = MvxIoCProvider.Initialize();
                Mvx.IoCProvider.RegisterSingleton(iocProvider);
            }

            MvxSetup.RegisterSetupType <MvxWpfSetup <App> >(System.Reflection.Assembly.GetExecutingAssembly());
            var instance = MvxWpfSetupSingleton.EnsureSingletonAvailable(Application.Current.Dispatcher, new Content());
        }
Exemple #20
0
        public void IocConstruct_WithMultipleAnonymousArguments_CreatesObject()
        {
            MvxSingleton.ClearAllSingletons();
            var instance = MvxIoCProvider.Initialize();

            var title       = "The title";
            var subtitle    = "The subtitle";
            var description = "The description";

            var arguments = new { title, subtitle, description };
            var d         = instance.IoCConstruct <D>(arguments);

            Assert.Equal(title, d.Title);
            Assert.Equal(subtitle, d.Subtitle);
            Assert.Equal(description, d.Description);
        }
Exemple #21
0
        public void resolves_correct_types()
        {
            // arrange
            IMvxIoCProvider ioc   = MvxIoCProvider.Initialize();
            var             setup = new Setup();

            // act
            setup.Initialize(ioc);

            // assert
            ioc.Resolve <IWebpageReaderService>().Should().BeOfType <WebpageReaderService>();
            ioc.Resolve <IWebpageConverterService>().Should().BeOfType <WebpageConverterService>();
            ioc.Resolve <IWebpageService>().Should().BeOfType <WebpageService>();
            ioc.Resolve <IDateTime>().Should().BeOfType <DateTimeService>();

            ioc.Resolve <IBrowserService>().Should().BeOfType <BrowserService>();
        }
Exemple #22
0
        public void TryResolve_ParameterConstructors_CreatesParametersUsingIocResolution()
        {
            MvxSingleton.ClearAllSingletons();
            var instance = MvxIoCProvider.Initialize();

            Mvx.RegisterType <IB, B>();
            Mvx.LazyConstructAndRegisterSingleton <IC, C2>();
            Mvx.RegisterType <IA, A>();

            IA  a1;
            var result = Mvx.TryResolve(out a1);

            Assert.True(result);
            Assert.NotNull(a1);
            Assert.NotNull(a1.B);
            Assert.IsType <B>(a1.B);
        }
Exemple #23
0
        public static void Resolves_successfully_when_resolving_entity_that_has_injected_an_open_generic_parameter()
        {
            var instance = MvxIoCProvider.Initialize();

            ((MvxIoCProvider)instance).CleanAllResolvers();

            instance.RegisterType <IHasOGParameter, HasOGParameter>();
            instance.RegisterType(typeof(IOG <>), typeof(OG <>));

            IHasOGParameter toResolve = null;

            Mvx.TryResolve <IHasOGParameter>(out toResolve);

            Assert.NotNull(toResolve);
            Assert.Contains(toResolve.GetType().GetTypeInfo().ImplementedInterfaces, i => i == typeof(IHasOGParameter));
            Assert.True(toResolve.GetType() == typeof(HasOGParameter));
            Assert.Contains(toResolve.OpenGeneric.GetType().GetTypeInfo().ImplementedInterfaces, i => i == typeof(IOG <C>));
            Assert.True(toResolve.OpenGeneric.GetType() == typeof(OG <C>));
        }
Exemple #24
0
        public void TryResolve_Dynamic_ReturnsDifferentInstanceEachTime()
        {
            MvxSingleton.ClearAllSingletons();
            var instance = MvxIoCProvider.Initialize();

            Mvx.LazyConstructAndRegisterSingleton <IB, B>();
            Mvx.LazyConstructAndRegisterSingleton <IC, C2>();
            Mvx.RegisterType <IA, A>();

            var previous = new Dictionary <IA, bool>();

            for (int i = 0; i < 100; i++)
            {
                IA  a1;
                var result = Mvx.TryResolve(out a1);
                Assert.True(result);
                Assert.False(previous.ContainsKey(a1));
                Assert.Equal(i, previous.Count);
                previous.Add(a1, true);
            }
        }
Exemple #25
0
        public static void Resolves_successfully_when_using_childcontainer()
        {
            var container = MvxIoCProvider.Initialize();

            ((MvxIoCProvider)container).CleanAllResolvers();

            container.RegisterType <IC, C2>();
            var childContainer = container.CreateChildContainer();

            childContainer.RegisterType <IB, B>();

            var b = childContainer.Create <IB>();

            Assert.True(container.CanResolve <IC>());
            Assert.False(container.CanResolve <IB>());
            Assert.True(childContainer.CanResolve <IC>());
            Assert.True(childContainer.CanResolve <IB>());

            Assert.NotNull(b);
            Assert.NotNull(b.C);
        }
Exemple #26
0
        public void IocConstruct_WithMultipleDictionaryArguments_CreatesObject()
        {
            MvxSingleton.ClearAllSingletons();
            var instance = MvxIoCProvider.Initialize();

            var title       = "The title";
            var subtitle    = "The subtitle";
            var description = "The description";

            var arguments = new Dictionary <string, object>
            {
                ["title"]       = title,
                ["subtitle"]    = subtitle,
                ["description"] = description
            };
            var d = instance.IoCConstruct <D>(arguments);

            Assert.Equal(title, d.Title);
            Assert.Equal(subtitle, d.Subtitle);
            Assert.Equal(description, d.Description);
        }
        public void TryResolve_WithNoInjection_NothingGetsInjected()
        {
            MvxSingleton.ClearAllSingletons();
            var instance = MvxIoCProvider.Initialize();

            Mvx.IoCProvider.RegisterType <IA, A>();
            Mvx.IoCProvider.RegisterType <IB, B>();
            Mvx.IoCProvider.RegisterType <IC, C>();

            IA  a;
            var result = Mvx.IoCProvider.TryResolve(out a);

            Assert.True(result);
            Assert.NotNull(a);
            Assert.IsType <A>(a);
            var castA = (A)a;

            Assert.Null(castA.B);
            Assert.Null(castA.C);
            Assert.Null(castA.BNever);
            Assert.Null(castA.CNever);
        }
Exemple #28
0
        public void RegisterType_with_no_reflection_creates_different_objects()
        {
            MvxSingleton.ClearAllSingletons();
            var instance = MvxIoCProvider.Initialize();

            instance.RegisterType <IA, IB>(b => new A(b));
            instance.RegisterType <IB, IC>(c => new B(c));
            instance.RegisterType <IC>(() => new C2());

            var typesToResolve = new[] { typeof(IA), typeof(IB), typeof(IC) };

            foreach (var type in typesToResolve)
            {
                var obj1 = Mvx.IoCProvider.Resolve(type);
                var obj2 = Mvx.IoCProvider.Resolve(type);

                Assert.NotNull(obj1);
                Assert.NotNull(obj2);

                Assert.NotEqual(obj1, obj2);
            }
        }
Exemple #29
0
        public void TryResolve_NonLazySingleton_ReturnsSameSingletonEachTime()
        {
            MvxSingleton.ClearAllSingletons();
            var instance = MvxIoCProvider.Initialize();

            Mvx.LazyConstructAndRegisterSingleton <IB, B>();
            Mvx.LazyConstructAndRegisterSingleton <IC, C2>();
            Mvx.ConstructAndRegisterSingleton <IA, A>();

            IA  a0;
            var result = Mvx.TryResolve(out a0);

            Assert.True(result);
            Assert.NotNull(a0);

            for (int i = 0; i < 100; i++)
            {
                IA a1;
                result = Mvx.TryResolve(out a1);
                Assert.True(result);
                Assert.Equal(a0, a1);
            }
        }
Exemple #30
0
        public void RegisterType_with_no_reflection_with_up_to_5_parameters()
        {
            MvxSingleton.ClearAllSingletons();
            var instance = MvxIoCProvider.Initialize();

            instance.RegisterType <IA, IB>(b => new A(b));
            instance.RegisterType <IB, IC>(c => new B(c));
            instance.RegisterType <IC>(() => new C2());
            instance.RegisterType <ID>(() => new D("Test", "Test subtitle", "Description"));
            instance.RegisterType <IE>(() => new E());

            instance.RegisterType <IHasFiveParameters, IA, IB, IC, ID, IE>((a, b, c, d, e) =>
                                                                           new HasMultipleConstructors(a, b, c, d, e));
            instance.RegisterType <IHasFourParameters, IA, IB, IC, ID>((a, b, c, d) =>
                                                                       new HasMultipleConstructors(a, b, c, d));
            instance.RegisterType <IHasThreeParameters, IA, IB, IC>((a, b, c) => new HasMultipleConstructors(a, b, c));
            instance.RegisterType <IHasTwoParameters, IA, IB>((a, b) => new HasMultipleConstructors(a, b));
            instance.RegisterType <IHasOneParameter, IA>(a => new HasMultipleConstructors(a));

            var obj1 = Mvx.IoCProvider.Resolve <IHasOneParameter>();
            var obj2 = Mvx.IoCProvider.Resolve <IHasTwoParameters>();
            var obj3 = Mvx.IoCProvider.Resolve <IHasThreeParameters>();
            var obj4 = Mvx.IoCProvider.Resolve <IHasFourParameters>();
            var obj5 = Mvx.IoCProvider.Resolve <IHasFiveParameters>();

            Assert.NotNull(obj1);
            Assert.NotNull(obj1.A);
            Assert.NotNull(obj2);
            Assert.NotNull(obj2.B);
            Assert.NotNull(obj3);
            Assert.NotNull(obj3.C);
            Assert.NotNull(obj4);
            Assert.NotNull(obj4.D);
            Assert.NotNull(obj5);
            Assert.NotNull(obj5.E);
        }