Ejemplo n.º 1
0
        public void ShouldLoadAssemblyIntoLoaderAtRuntime()
        {
            var path = Path.Combine(@"..\..\..\SampleFileWatcherLibrary\bin\Debug",
                                    AppDomain.CurrentDomain.BaseDirectory);
            var targetFile     = "SampleFileWatcherLibrary.dll";
            var 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.False(container.Contains(typeof(ISampleService)));

            // Copy the assembly to the target directory
            // and watch for changes
            var 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.True(container.Contains(typeof(ISampleService)));

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

            Assert.NotNull(instance);

            var typeName = instance.GetType().Name;

            Assert.Equal("SampleFileWatcherServiceClassAddedAtRuntime", typeName);
        }
Ejemplo n.º 2
0
        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 Element Parse(XElement element)
        {
            Node node = new Node
            {
                Name        = element.Attribute("name").Value,
                ID          = element.Attribute("id").Value,
                Cooperation = Convert.ToInt32(element.Attribute("cooperation").Value)
            };

            string category = element.Attribute("category").Value;

            node.NodeType = Utils.Convert(category);

            if (element.HasElements)
            {
                List <Element> nodes = new List <Element>();
                element.Elements().ToList().ForEach(entry =>
                {
                    string nodeName = entry.Name.LocalName;
                    if (ServiceContainer.Contains(nodeName))
                    {
                        IWorkflowParse parseService = (ServiceContainer.Resolve(nodeName) as IWorkflowParse);
                        nodes.Add(parseService.Parse(entry));
                    }
                });

                node.Transitions.AddRange(nodes.Where(transition => (transition is Transition)).Cast <Transition>());
                node.Actions.AddRange(nodes.Where(action => (action is Action)).Cast <Action>());
                node.Groups.AddRange(nodes.Where(group => (group is Group)).Cast <Group>());
                node.Actors.AddRange(nodes.Where(actor => (actor is Actor)).Cast <Actor>());
                node.Command = nodes.Where(command => (command is Command)).Cast <Command>().FirstOrDefault();
            }
            return(node);
        }
Ejemplo n.º 4
0
        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)));
        }
Ejemplo n.º 5
0
        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));
        }
Ejemplo n.º 6
0
        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> >());
        }
Ejemplo n.º 7
0
        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);
        }
Ejemplo n.º 8
0
        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 Element Parse(XElement element)
        {
            Node node = new Node
            {
                Name        = element.Attribute("name").Value,
                ID          = element.Attribute("id").Value,
                Cooperation = element.Attribute("cooperation").Value,
                Assistant   = element.Attribute("assistant").Value
            };

            XAttribute back = element.Attribute("back");
            XAttribute url  = element.Attribute("url");
            XAttribute attr = element.Attribute("veto");

            node.Veto = attr != null && attr.Value == "0" ? String.Empty : "Smartflow.Core.Components.VetoService";
            node.Back = back == null ? String.Empty : back.Value;
            node.Url  = url == null ? String.Empty : url.Value;

            string category = element.Attribute("category").Value;

            node.NodeType = Internals.Utils.Convert(category);
            if (element.HasElements)
            {
                List <Element> nodes = new List <Element>();
                element.Elements().ToList().ForEach(entry =>
                {
                    string nodeName = entry.Name.LocalName;
                    if (ServiceContainer.Contains(nodeName))
                    {
                        IWorkflowParse parseService = (ServiceContainer.Resolve(nodeName) as IWorkflowParse);
                        nodes.Add(parseService.Parse(entry));
                    }
                });

                node.Transitions.AddRange(nodes.Where(transition => (transition is Transition)).Cast <Transition>());
                node.Actions.AddRange(nodes.Where(action => (action is Action)).Cast <Action>());
                node.Groups.AddRange(nodes.Where(group => (group is Group)).Cast <Group>());
                node.Actors.AddRange(nodes.Where(actor => (actor is Actor)).Cast <Actor>());
                node.Rules.AddRange(nodes.Where(rule => (rule is Elements.Rule)).Cast <Elements.Rule>());
                node.Carbons.AddRange(nodes.Where(carbon => (carbon is Elements.Carbon)).Cast <Elements.Carbon>());
                node.Organizations.ToList().AddRange(nodes.Where(org => (org is Elements.Organization)).Cast <Elements.Organization>());
                node.Command = nodes.Where(command => (command is Command)).Cast <Command>().FirstOrDefault();

                if (node.Command != null)
                {
                    node.Command.Node = node;
                }
            }
            return(node);
        }
Ejemplo n.º 10
0
        public BasePager(IReportModel reportModel)
        {
            if (reportModel == null)
            {
                throw new ArgumentNullException("reportModel");
            }

            this.ReportModel = reportModel;

            if (!ServiceContainer.Contains(typeof(ILayouter)))
            {
                ServiceContainer.AddService <ILayouter>(new Layouter());
            }

            this.Graphics = CreateGraphicObject.FromSize(this.ReportModel.ReportSettings.PageSize);
        }
Ejemplo n.º 11
0
        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>));
        }
Ejemplo n.º 12
0
        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)));
        }
Ejemplo n.º 13
0
        public void FactoryAttributeLoaderMustInjectStronglyTypedFactoryIntoContainer()
        {
            var  container   = new ServiceContainer();
            Type serviceType = typeof(ISampleService);

            ITypeLoader loader = new FactoryAttributeLoader();
            IEnumerable <Action <IServiceContainer> > actions = loader.Load(typeof(SampleStronglyTypedFactory));

            // The factory loader should return a set of actions
            // that will inject that custom factory into the container
            // itself
            foreach (var action in actions)
            {
                action(container);
            }

            Assert.IsTrue(container.Contains(serviceType));
        }
Ejemplo n.º 14
0
        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));
        }
Ejemplo n.º 15
0
        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);
        }
Ejemplo n.º 16
0
        public void ContainsService()
        {
            bool contains = ServiceContainer.Contains(typeof(ITestInterface));

            Assert.That(contains, Is.True);
        }
Ejemplo n.º 17
0
        public void DosNotContainsService()
        {
            bool contains = ServiceContainer.Contains(typeof(IServiceProvider));

            Assert.That(contains, Is.False);
        }