예제 #1
0
        public DependencyInjectionFixture()
        {
            _container = new PillarDefaultIoc();

            _container.RegisterType <IFoo, Foo>();
            _container.RegisterType <IBar, Bar>();
        }
        public void MissingPrimitiveDefaultValue(IContainerAdapter adapter)
        {
            adapter.RegisterType <IService, IndependentService>();
            adapter.RegisterType <ServiceWithDependencyAndOptionalInt32Parameter>();

            var component = adapter.Resolve <ServiceWithDependencyAndOptionalInt32Parameter>();

            Assert.Equal(5, component.Optional);
        }
        public void MissingPrimitiveDefaultValue(IContainerAdapter adapter)
        {
            adapter.RegisterType<IService, IndependentService>();
            adapter.RegisterType<ServiceWithDependencyAndOptionalInt32Parameter>();

            var component = adapter.Resolve<ServiceWithDependencyAndOptionalInt32Parameter>();

            Assert.Equal(5, component.Optional);
        }
        public void GracefulRecursionHandling(IContainerAdapter adapter)
        {
            this.AssertIsNotCrashingOnRecursion(adapter);

            adapter.RegisterType <ServiceWithRecursiveDependency1>();
            adapter.RegisterType <ServiceWithRecursiveDependency2>();

            this.AssertGivesCorrectExceptionWhenResolvingRecursive <ServiceWithRecursiveDependency1>(adapter);
        }
        public void GracefulRecursionHandlingForListDependency(IContainerAdapter adapter)
        {
            this.AssertIsNotCrashingOnListRecursion(adapter);

            adapter.RegisterType <ServiceWithListConstructorDependency <IService[]> >();
            adapter.RegisterType <IService, ServiceThatCreatesRecursiveArrayDependency>();

            this.AssertGivesCorrectExceptionWhenResolvingRecursive <ServiceWithListConstructorDependency <IService[]> >(adapter);
        }
        public void MissingPrimitive(IContainerAdapter adapter)
        {
            adapter.RegisterType <IService, IndependentService>();
            adapter.RegisterType <ServiceWithDependencyAndOptionalInt32Parameter>();

            var component = adapter.Resolve <ServiceWithDependencyAndOptionalInt32Parameter>();

            Assert.NotNull(component);
        }
        public void MissingPrimitive(IContainerAdapter adapter)
        {
            adapter.RegisterType<IService, IndependentService>();
            adapter.RegisterType<ServiceWithDependencyAndOptionalInt32Parameter>();

            var component = adapter.Resolve<ServiceWithDependencyAndOptionalInt32Parameter>();

            Assert.NotNull(component);
        }
예제 #8
0
        public void BasicLazySupport(IContainerAdapter adapter)
        {
            adapter.RegisterType<IService, IndependentService>();
            adapter.RegisterType<ServiceWithSimpleConstructorDependency>();

            var lazy = adapter.Resolve<Lazy<ServiceWithSimpleConstructorDependency>>();

            Assert.NotNull(lazy);
            Assert.NotNull(lazy.Value);
        }
예제 #9
0
        public void NotCreatingLazyPrematurely(IContainerAdapter adapter)
        {
            adapter.RegisterType<IService, IndependentService>();
            adapter.RegisterType<ServiceWithSimpleConstructorDependency>();

            var lazy = adapter.Resolve<Lazy<ServiceWithSimpleConstructorDependency>>();

            Assert.NotNull(lazy);
            Assert.False(lazy.IsValueCreated);
        }
예제 #10
0
        public void ConstructorDependency(IContainerAdapter adapter)
        {
            adapter.RegisterType <IService, IndependentService>();
            adapter.RegisterType <ServiceWithSimpleConstructorDependency>();

            var component = adapter.Resolve <ServiceWithSimpleConstructorDependency>();

            Assert.NotNull(component.Service);
            Assert.IsAssignableFrom <IndependentService>(component.Service);
        }
예제 #11
0
        public void ConstructorDependency(IContainerAdapter adapter)
        {
            adapter.RegisterType<IService, IndependentService>();
            adapter.RegisterType<ServiceWithSimpleConstructorDependency>();

            var component = adapter.Resolve<ServiceWithSimpleConstructorDependency>();

            Assert.NotNull(component.Service);
            Assert.IsAssignableFrom<IndependentService>(component.Service);
        }
        public void RegistrationAtAnyStage(IContainerAdapter adapter)
        {
            adapter.RegisterType <IService, IndependentService>();
            adapter.Resolve <IService>();
            adapter.RegisterType <IService2, IndependentService2>();

            var resolved = adapter.Resolve <IService2>();

            Assert.NotNull(resolved);
        }
예제 #13
0
        public void BasicLazySupport(IContainerAdapter adapter)
        {
            adapter.RegisterType <IService, IndependentService>();
            adapter.RegisterType <ServiceWithSimpleConstructorDependency>();

            var lazy = adapter.Resolve <Lazy <ServiceWithSimpleConstructorDependency> >();

            Assert.NotNull(lazy);
            Assert.NotNull(lazy.Value);
        }
예제 #14
0
        public void NotCreatingLazyPrematurely(IContainerAdapter adapter)
        {
            adapter.RegisterType <IService, IndependentService>();
            adapter.RegisterType <ServiceWithSimpleConstructorDependency>();

            var lazy = adapter.Resolve <Lazy <ServiceWithSimpleConstructorDependency> >();

            Assert.NotNull(lazy);
            Assert.False(lazy.IsValueCreated);
        }
예제 #15
0
        public void FactoryWithNoParameters(IContainerAdapter adapter)
        {
            adapter.RegisterType<IService, IndependentService>();
            adapter.RegisterType<ServiceWithSimpleConstructorDependency>();

            var func = adapter.Resolve<Func<ServiceWithSimpleConstructorDependency>>();

            Assert.NotNull(func);
            var result = func();
            Assert.NotNull(result);
        }
예제 #16
0
        public void FactoryWithNoParameters(IContainerAdapter adapter)
        {
            adapter.RegisterType <IService, IndependentService>();
            adapter.RegisterType <ServiceWithSimpleConstructorDependency>();

            var func = adapter.Resolve <Func <ServiceWithSimpleConstructorDependency> >();

            Assert.NotNull(func);
            var result = func();

            Assert.NotNull(result);
        }
예제 #17
0
        public void FactoryWithParameter(IContainerAdapter adapter)
        {
            adapter.RegisterType<IService, IndependentService>();
            adapter.RegisterType<ServiceWithTwoConstructorDependencies>();
            var service2 = new IndependentService2();

            var func = adapter.Resolve<Func<IService2, ServiceWithTwoConstructorDependencies>>();

            Assert.NotNull(func);
            var result = func(service2);
            Assert.NotNull(result);
            Assert.Same(service2, result.Service2);
        }
예제 #18
0
        public void AssertResolvesListDependencyFor <TTestComponent>(IContainerAdapter adapter)
            where TTestComponent : IServiceWithListDependency <IEnumerable <IService> >
        {
            adapter.RegisterType <IService, IndependentService>();
            adapter.RegisterType <TTestComponent>();

            var resolved = adapter.Resolve <TTestComponent>();

            Assert.NotNull(resolved);
            Assert.NotNull(resolved.Services);
            Assert.Equal(1, resolved.Services.Count());
            Assert.IsAssignableFrom <IndependentService>(resolved.Services.First());
        }
        public void ReasonableConstructorSelection(IContainerAdapter adapter)
        {
            adapter.RegisterType <IService, IndependentService>();
            adapter.RegisterType <ServiceWithMultipleConstructors>();

            var resolved = adapter.Resolve <ServiceWithMultipleConstructors>();

            Assert.NotNull(resolved);
            Assert.Equal(
                ServiceWithMultipleConstructors.ConstructorNames.MostResolvable,
                resolved.UsedConstructorName
                );
        }
예제 #20
0
        public void FactoryWithParameterForSubdependency(IContainerAdapter adapter)
        {
            adapter.RegisterType<ServiceWithSimpleConstructorDependency>();
            adapter.RegisterType<ServiceWithDependencyOnServiceWithOtherDependency>();

            var service = new IndependentService();
            var func = adapter.Resolve<Func<IService, ServiceWithDependencyOnServiceWithOtherDependency>>();

            Assert.NotNull(func);
            var result = func(service);
            Assert.NotNull(result);
            Assert.Same(service, result.Service.Service);
        }
예제 #21
0
        public void FactoryWithParameterForSubdependency(IContainerAdapter adapter)
        {
            adapter.RegisterType <ServiceWithSimpleConstructorDependency>();
            adapter.RegisterType <ServiceWithDependencyOnServiceWithOtherDependency>();

            var service = new IndependentService();
            var func    = adapter.Resolve <Func <IService, ServiceWithDependencyOnServiceWithOtherDependency> >();

            Assert.NotNull(func);
            var result = func(service);

            Assert.NotNull(result);
            Assert.Same(service, result.Service.Service);
        }
예제 #22
0
        public void FactoryWithParameter(IContainerAdapter adapter)
        {
            adapter.RegisterType <IService, IndependentService>();
            adapter.RegisterType <ServiceWithTwoConstructorDependencies>();
            var service2 = new IndependentService2();

            var func = adapter.Resolve <Func <IService2, ServiceWithTwoConstructorDependencies> >();

            Assert.NotNull(func);
            var result = func(service2);

            Assert.NotNull(result);
            Assert.Same(service2, result.Service2);
        }
        public void PropertyDependencyIsOptional(IContainerAdapter adapter)
        {
            adapter.RegisterType<ServiceWithSimplePropertyDependency>();
            var component = adapter.Resolve<ServiceWithSimplePropertyDependency>();

            Assert.Null(component.Service);
        }
예제 #24
0
        public void PropertyDependencyIsOptional(IContainerAdapter adapter)
        {
            adapter.RegisterType <ServiceWithSimplePropertyDependency>();
            var component = adapter.Resolve <ServiceWithSimplePropertyDependency>();

            Assert.Null(component.Service);
        }
예제 #25
0
        public void IndependentServiceRegisteredAsSelf(IContainerAdapter adapter)
        {
            adapter.RegisterType<IndependentService>();
            var component = adapter.Resolve<IndependentService>();

            Assert.NotNull(component);
        }
예제 #26
0
        public void IndependentServiceRegisteredAsSelf(IContainerAdapter adapter)
        {
            adapter.RegisterType <IndependentService>();
            var component = adapter.Resolve <IndependentService>();

            Assert.NotNull(component);
        }
예제 #27
0
        public static IContainerAdapter RegisterInstanceType <TFrom, TTo>(this IContainerAdapter adapter) where TTo : TFrom
        {
            {
                Lokad.Enforce.Argument(() => adapter);
            }

            return(adapter.RegisterType <TFrom, TTo>(ContainerRegistrationScope.Instance));
        }
예제 #28
0
        public void ConstructorDependencyUsingInstance(IContainerAdapter adapter)
        {
            var instance = new IndependentService();
            adapter.RegisterInstance<IService>(instance);
            adapter.RegisterType<ServiceWithSimpleConstructorDependency>();

            var dependent = adapter.Resolve<ServiceWithSimpleConstructorDependency>();

            Assert.Same(instance, dependent.Service);
        }
예제 #29
0
        public void RegisterTypeFactory()
        {
            _container = new PillarDefaultIoc();

            _container.RegisterType <IFoo>(() => new Foo());

            var foo1 = _container.Resolve <IFoo>();
            var foo2 = _container.Resolve <IFoo>();

            Assert.NotSame(foo1, foo2);
        }
예제 #30
0
        public void ConstructorDependencyUsingInstance(IContainerAdapter adapter)
        {
            var instance = new IndependentService();

            adapter.RegisterInstance <IService>(instance);
            adapter.RegisterType <ServiceWithSimpleConstructorDependency>();

            var dependent = adapter.Resolve <ServiceWithSimpleConstructorDependency>();

            Assert.Same(instance, dependent.Service);
        }
예제 #31
0
        internal static void RegisterPillarDependencies(this IContainerAdapter container)
        {
            // service registration
            container.RegisterSingleton(container); // the container itself can be injected

            container.RegisterSingleton <IDialogProvider, DialogService>();
            container.RegisterSingleton <IViewFactory, ViewFactory>();
            container.RegisterSingleton <INavigator, Navigator>();
            container.RegisterSingleton <IMessenger, Messenger>();

            // current page resolver
            container.RegisterType <Func <Page> >(() => GetCurrentPage);

            // current PageProxy
            container.RegisterSingleton <IPage, PageProxy>();
        }
예제 #32
0
        protected override void RegisterDependencies(IContainerAdapter container)
        {
            container.RegisterSingleton <LoginViewModel>();
            container.RegisterSingleton <LoginView>();

            container.RegisterSingleton <HomeViewModel>();
            container.RegisterSingleton <HomeView>();

            container.RegisterType <EventToCommandViewModel>();
            container.RegisterType <EventToCommandView>();

            container.RegisterType <DialogViewModel>();
            container.RegisterType <DialogView>();

            container.RegisterType <TemplateSelectorViewModel>();
            container.RegisterType <TemplateSelectorView>();

            container.RegisterType <MessengerViewModel>();
            container.RegisterType <MessengerView>();
        }
예제 #33
0
        // transient generics

        public static void RegisterType <TFrom, TTo>(this IContainerAdapter adapter)
            where TFrom : class
            where TTo : class, TFrom
        {
            adapter.RegisterType(typeof(TFrom), typeof(TTo));
        }
예제 #34
0
 public static void RegisterType <TInterface>(this IContainerAdapter adapter, Func <TInterface> implementationFactory)
     where TInterface : class
 {
     adapter.RegisterType(typeof(TInterface), implementationFactory);
 }
예제 #35
0
 protected override void RegisterDependencies(IContainerAdapter container)
 {
     container.RegisterType <MockViewModel>();
     container.RegisterType <MockView>();
 }
예제 #36
0
 public virtual void Load(IContainerAdapter adapter)
 {
     adapter.RegisterType(typeof(PluginBase), this.GetType());
 }
예제 #37
0
 public static void RegisterType <TInterface>(this IContainerAdapter adapter)
     where TInterface : class
 {
     adapter.RegisterType(typeof(TInterface), typeof(TInterface));
 }
예제 #38
0
        public void CannotRegisterDependencyAfterResolve()
        {
            _container.Resolve <IFoo>();

            Assert.Throws <InvalidOperationException>(() => _container.RegisterType <ContentPage>());
        }