예제 #1
0
        /*============================================================================*/
        /* Private Functions                                                          */
        /*============================================================================*/

        private InjectionMapping CreateMapping(MappingId mappingId)
        {
            if (_mappingsInProcess.ContainsKey(mappingId))
            {
                throw new InjectorException("Can't change a mapping from inside a listener to it's creation event");
            }

            _mappingsInProcess [mappingId] = true;

            if (_preMappingCreate != null)
            {
                _preMappingCreate(mappingId);
            }

            InjectionMapping mapping = new InjectionMapping(this, mappingId);

            _mappings [mappingId] = mapping;

            object sealKey = mapping.Seal();

            if (_postMappingCreate != null)
            {
                _postMappingCreate(mappingId, mapping);
            }

            _mappingsInProcess.Remove(mappingId);
            mapping.Unseal(sealKey);
            return(mapping);
        }
        public void SealingAMappingMakesItSealed()
        {
            InjectionMapping config = new InjectionMapping(injector, new MappingId(typeof(Interface), null));

            config.Seal();
            Assert.True(config.isSealed);
        }
        public void SealingAMappingMakesItUnchangable()
        {
            InjectionMapping config = new InjectionMapping(injector, new MappingId(typeof(Interface), null));

            config.Seal();
            List <MethodInfo> testMethods         = new List <MethodInfo> ();
            List <object[]>   testMethodArguments = new List <object[]> ();

            testMethods.Add(config.GetType().GetMethod("AsSingleton"));
            testMethodArguments.Add(new object[1] {
                false
            });

            testMethods.Add(config.GetType().GetMethod("ToSingleton", new Type[] { typeof(Type), typeof(bool) }));
            testMethodArguments.Add(new object[2] {
                typeof(Clazz), false
            });

            testMethods.Add(config.GetType().GetMethod("ToType", new Type[] { typeof(Type) }));
            testMethodArguments.Add(new object[1] {
                typeof(Clazz)
            });

            testMethods.Add(config.GetType().GetMethod("ToValue"));
            testMethodArguments.Add(new object[3] {
                typeof(Clazz), false, false
            });

            testMethods.Add(config.GetType().GetMethod("ToProvider"));
            testMethodArguments.Add(new object[1] {
                null
            });

            testMethods.Add(config.GetType().GetMethod("Locally"));
            testMethodArguments.Add(null);

            testMethods.Add(config.GetType().GetMethod("Softly"));
            testMethodArguments.Add(null);

            List <MethodInfo> injectionExeptionMethods = new List <MethodInfo> ();

            for (int i = 0; i < testMethods.Count; i++)
            {
                MethodInfo methodInfo = testMethods [i];
                try
                {
                    methodInfo.Invoke(config, testMethodArguments [i]);
                }
                catch (Exception exception)
                {
                    if (exception.InnerException != null && exception.InnerException is InjectorException)
                    {
                        injectionExeptionMethods.Add(methodInfo);
                    }
                    else
                    {
                        throw exception;
                    }
                }
            }

            Assert.AreEqual(testMethods.Count, injectionExeptionMethods.Count);
            for (int i = 0; i < testMethods.Count; i++)
            {
                Assert.AreEqual(testMethods [i], injectionExeptionMethods [i]);
            }
        }
        public void SealReturnsAnUnsealingKeyObject()
        {
            InjectionMapping config = new InjectionMapping(injector, new MappingId(typeof(Interface), null));

            Assert.NotNull(config.Seal());
        }