Contains() public méthode

Determines whether or not the given serviceType can be instantiated by the container.
public Contains ( Type serviceType, IEnumerable additionalParameterTypes ) : bool
serviceType System.Type The type of service to instantiate.
additionalParameterTypes IEnumerable The list of additional parameters that this factory type will support.
Résultat bool
        public void ShouldLoadAssemblyIntoLoaderAtRuntime()
        {
            string path = Path.Combine(@"..\..\..\SampleFileWatcherLibrary\bin\Debug",
                                       AppDomain.CurrentDomain.BaseDirectory);
            string targetFile = "SampleFileWatcherLibrary.dll";
            string sourceFileName = Path.Combine(path, targetFile);

            var container = new ServiceContainer();
            container.AutoLoadFrom(AppDomain.CurrentDomain.BaseDirectory, "dummy.dll");

            // There should be nothing loaded at this point since the assembly hasn't
            // been copied to the target directory yet
            Assert.IsFalse(container.Contains(typeof (ISampleService)));

            // Copy the assembly to the target directory
            // and watch for changes
            string targetFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "dummy.dll");
            File.Copy(sourceFileName, targetFileName, true);

            // Give the watcher thread enough time to load the assembly into memory
            Thread.Sleep(500);
            Assert.IsTrue(container.Contains(typeof (ISampleService)));

            var instance = container.GetService<ISampleService>();
            Assert.IsNotNull(instance);

            string typeName = instance.GetType().Name;
            Assert.AreEqual("SampleFileWatcherServiceClassAddedAtRuntime", typeName);
        }
        public void ContainerMustAllowInjectingCustomFactoriesForOpenGenericTypeDefinitions()
        {
            var container = new ServiceContainer();
            var factory = new SampleOpenGenericFactory();

            container.AddFactory(typeof (ISampleGenericService<>), factory);

            // The container must report that it *can* create
            // the generic service type
            Assert.IsTrue(container.Contains(typeof (ISampleGenericService<int>)));

            var result = container.GetService<ISampleGenericService<int>>();

            Assert.IsNotNull(result);
            Assert.IsTrue(result.GetType() == typeof (SampleGenericImplementation<int>));
        }
        public void LazyObjectShouldNeverBeInitialized()
        {
            var container = new ServiceContainer();
            container.AddService<IProxyFactory>(new ProxyFactory());
            container.AddService<IMethodBuilder<MethodInfo>>(new MethodBuilder());

            Assert.IsTrue(container.Contains(typeof (IProxyFactory)));

            var proxyFactory = container.GetService<IProxyFactory>();
            var interceptor = new LazyInterceptor<ISampleService>(() => new SampleLazyService());

            SampleLazyService.Reset();
            // The instance should be uninitialized at this point
            var proxy = proxyFactory.CreateProxy<ISampleService>(interceptor);
            Assert.IsFalse(SampleLazyService.IsInitialized);

            // The instance should be initialized once the method is called
            proxy.DoSomething();
            Assert.IsTrue(SampleLazyService.IsInitialized);
        }
        public void ContainerServicesShouldBeLazyIfProxyFactoryExists()
        {
            var container = new ServiceContainer();
            container.AddService<IProxyFactory>(new ProxyFactory());
            Assert.IsTrue(container.Contains(typeof (IProxyFactory)));

            // The instance should never be created
            container.AddService(typeof (ISampleService), typeof (SampleLazyService));

            var result = container.GetService<ISampleService>();
            Assert.IsFalse(SampleLazyService.IsInitialized);
        }
        public void ContainerMustUseUnnamedContainsMethodIfNameIsNull()
        {
            var mockFactory = new Mock<IFactory>();
            var mockService = new Mock<ISerializable>();
            var container = new ServiceContainer();

            Type serviceType = typeof (ISerializable);

            // Use unnamed AddFactory method
            container.AddFactory(serviceType, mockFactory.Object);

            // The container should use the
            // IContainer.Contains(Type) method instead of the
            // IContainer.Contains(string, Type) method if the
            // service name is blank
            Assert.IsTrue(container.Contains(null, typeof (ISerializable)));
        }
        public void ContainerMustLoadSpecificGenericServiceTypesFromAGenericConcreteClassMarkedWithTheImplementsAttribute()
        {
            var container = new ServiceContainer();
            container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "*.dll");

            string serviceName = "SpecificGenericService";

            // The container must be able to create both registered service types
            Assert.IsTrue(container.Contains(serviceName, typeof (ISampleGenericService<int>)));
            Assert.IsTrue(container.Contains(serviceName, typeof (ISampleGenericService<double>)));

            // Both service types must be valid services
            Assert.IsNotNull(container.GetService<ISampleGenericService<int>>());
            Assert.IsNotNull(container.GetService<ISampleGenericService<double>>());
        }
        public void ContainerMustLoadAssemblyFromMemory()
        {
            var container = new ServiceContainer();
            container.LoadFrom(typeof (SampleClass).Assembly);

            // Verify that the container loaded the sample assembly into memory
            Assert.IsTrue(container.Contains(typeof (ISampleService)));
        }
        public void ContainerMustLoadAnyGenericServiceTypeInstanceFromAGenericConcreteClassMarkedWithTheImplementsAttribute()
        {
            var container = new ServiceContainer();
            container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "*.dll");

            string serviceName = "NonSpecificGenericService";

            // The container must be able to create any type that derives from ISampleService<T>
            // despite whether or not the specific generic service type is explicitly registered as a service
            Assert.IsTrue(container.Contains(serviceName, typeof (ISampleGenericService<int>)));
            Assert.IsTrue(container.Contains(serviceName, typeof (ISampleGenericService<double>)));
            Assert.IsTrue(container.Contains(serviceName, typeof (ISampleGenericService<string>)));

            // Both service types must be valid services
            Assert.IsNotNull(container.GetService<ISampleGenericService<int>>());
            Assert.IsNotNull(container.GetService<ISampleGenericService<double>>());
            Assert.IsNotNull(container.GetService<ISampleGenericService<string>>());
        }
        public void ContainerMustHoldNamedFactoryInstance()
        {
            var mockFactory = new Mock<IFactory>();
            var container = new ServiceContainer();

            // Randomly assign an interface type
            // NOTE: The actual interface type doesn't matter
            Type serviceType = typeof (ISerializable);

            container.AddFactory("MyService", serviceType, mockFactory.Object);
            Assert.IsTrue(container.Contains("MyService", serviceType),
                          "The container is supposed to contain a service named 'MyService'");

            var instance = new object();
            mockFactory.Expect(f => f.CreateInstance(
                It.Is<IFactoryRequest>(
                    request => request.ServiceName == "MyService" && request.ServiceType == serviceType)))
                .Returns(instance);

            Assert.AreSame(instance, container.GetService("MyService", serviceType));
        }
        public void ContainerMustHoldAnonymousFactoryInstance()
        {
            var mockFactory = new Mock<IFactory>();
            var container = new ServiceContainer();

            // Give it a random service interface type
            Type serviceType = typeof (IDisposable);

            // Manually add the factory instance
            container.AddFactory(serviceType, mockFactory.Object);
            Assert.IsTrue(container.Contains(serviceType),
                          "The container needs to have a factory for service type '{0}'", serviceType);
        }
        public void ShouldReportThatServiceExistsForStronglyTypedFunctor()
        {
            var container = new ServiceContainer();

            Func<int, int, int> addOperation1 = (a, b) => a + b;
            container.AddService("Add", addOperation1);

            Assert.IsTrue(container.Contains("Add", typeof (int), 1, 1));
        }