Example #1
0
 public void DispatchPostMappingChangeEvent(MappingId mappingId, InjectionMapping injectionMapping)
 {
     if (_postMappingChange != null)
     {
         _postMappingChange(mappingId, injectionMapping);
     }
 }
        public void InjectionMappingAsSingletonMethodCreatesSingletonProvider()
        {
            InjectionMapping config = new InjectionMapping(injector, new MappingId(typeof(Clazz), null));

            config.AsSingleton();
            Assert.IsInstanceOf <SingletonProvider>(config.GetProvider());
        }
Example #3
0
 public void DispatchMappingOverrideEvent(MappingId mappingId, InjectionMapping injectionMapping)
 {
     if (_mappingOverride != null)
     {
         _mappingOverride(mappingId, injectionMapping);
     }
 }
        public void SealingAMappingMakesItSealed()
        {
            InjectionMapping config = new InjectionMapping(injector, new MappingId(typeof(Interface), null));

            config.Seal();
            Assert.True(config.isSealed);
        }
Example #5
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 GetMappingReturnsMappingsOfTheSameKey()
        {
            InjectionMapping injectionMapping1 = injector.Map(typeof(Interface));

            injectionMapping1.ToValue(null);

            InjectionMapping injectionMapping2 = injector.GetMapping(typeof(Interface));

            Assert.That(injectionMapping1, Is.EqualTo(injectionMapping2));
        }
Example #7
0
        public void MappingToProviderUsesProvidersResponse()
        {
            InjectionMapping otherConfig = new InjectionMapping(injector, new MappingId(typeof(ClazzExtension), null));

            otherConfig.ToProvider(new TypeProvider(typeof(ClazzExtension)));
            OtherMappingProvider otherMappingProvider = new OtherMappingProvider(otherConfig);
            object returnedResponse = otherMappingProvider.Apply(null, injector, null);

            Assert.IsInstanceOf <Clazz>(returnedResponse);
            Assert.IsInstanceOf <ClazzExtension>(returnedResponse);
        }
        public void SetProviderChangesTheProvider()
        {
            SingletonProvider provider1 = new SingletonProvider(typeof(Clazz), injector);
            TypeProvider      provider2 = new TypeProvider(typeof(Clazz));
            InjectionMapping  config    = new InjectionMapping(injector, new MappingId(typeof(Clazz), null));

            config.ToProvider(provider1);
            Assert.AreEqual(config.GetProvider(), provider1);
            config.ToProvider(null);
            config.ToProvider(provider2);
            Assert.AreEqual(config.GetProvider(), provider2);
        }
Example #9
0
        /*============================================================================*/
        /* Private Functions                                                          */
        /*============================================================================*/

        private void PostMappingChange(MappingId mappingId, InjectionMapping mapping)
        {
            DependencyProvider dp = mapping.GetProvider();

            if (dp is SingletonProvider)
            {
                dp.PostApply += HandlePostApply;
                _dependencyMappingIds [dp] = mappingId;
            }
            else if (dp is ValueProvider)
            {
                AddSingleton(mappingId, _injector.GetInstance(mappingId.type, mappingId.key));
            }
        }
Example #10
0
        private void SendMessageToPlugins(EventType eventType, InjectionMapping mapping,
                                          Exception exceptionInjection, string message)
        {
            if (Plugins == null || PluginList == null || PluginList.Count == 0)
            {
                return;
            }

            try
            {
                PluginList.ForEach(x =>
                {
                    try
                    {
                        x.OnMessageReceived(eventType, new PluginInterface.Message
                        {
                            Target    = mapping == null ? string.Empty : mapping.Method.Name,
                            Injector  = mapping == null ? string.Empty : mapping.Injector.Name,
                            Error     = exceptionInjection,
                            Result    = message,
                            TimeStamp = DateTime.Now
                        });
                    }
                    catch (Exception ex)
                    {
                        try
                        {
                            x.HandleError(ex);
                        }
                        catch
                        {
                            // Ignore error caused due to plugin, later should be given back to IPlugin through
                        }
                    }
                });
            }
            catch { }
        }
        public void InjectorUsesChildInjectorForSpecifiedMapping()
        {
            injector.Map(typeof(RobotFoot));

            InjectionMapping leftFootMapping   = injector.Map(typeof(RobotLeg), "leftLeg");
            Injector         leftChildInjector = injector.CreateChildInjector();

            leftChildInjector.Map(typeof(RobotAnkle));
            leftChildInjector.Map(typeof(RobotFoot)).ToType(typeof(LeftRobotFoot));

            leftFootMapping.SetInjector(leftChildInjector);
            InjectionMapping rightFootMapping   = injector.Map(typeof(RobotLeg), "rightLeg");
            Injector         rightChildInjector = injector.CreateChildInjector();

            rightChildInjector.Map(typeof(RobotAnkle));
            rightChildInjector.Map(typeof(RobotFoot)).ToType(typeof(RightRobotFoot));
            rightFootMapping.SetInjector(rightChildInjector);

            RobotBody robotBody = injector.InstantiateUnmapped <RobotBody>();

            Assert.IsInstanceOf <RightRobotFoot>(robotBody.rightLeg.ankle.foot, "Right RobotLeg should have a RightRobotFoot");
            Assert.IsInstanceOf <LeftRobotFoot>(robotBody.leftLeg.ankle.foot, "Left RobotLeg should have a LeftRobotFoot");
        }
Example #12
0
        private void openProjectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _project = new Project();
            _mapping = new List <InjectionMapping>();

            DialogResult dialogResult = openProjectFileDialog.ShowDialog();

            if (dialogResult == System.Windows.Forms.DialogResult.OK)
            {
                System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Project));
                TextReader reader = new StreamReader(openProjectFileDialog.FileName);

                _project = (Project)serializer.Deserialize(reader);

                _project.Injectors.ForEach(x => AddInjectionAssembly(x));
                _project.TargetAssemblies.ForEach(x => AddTargetAssembly(x));
                _project.Mapping.ForEach(x => _mapping.Add(InjectionMapping.FromProjectInjectionMapping(x)));

                grdCombination.DataSource = _mapping;

                reader.Close();
            }
        }
        public void childInjectorDoesntReturnToParentAfterUsingParentInjectorForMissingMappings()
        {
            injector.Map(typeof(RobotAnkle));
            injector.Map(typeof(RobotFoot));
            injector.Map(typeof(RobotToes));

            InjectionMapping leftFootMapping   = injector.Map(typeof(RobotLeg), "leftLeg");
            Injector         leftChildInjector = injector.CreateChildInjector();

            leftChildInjector.Map(typeof(RobotFoot)).ToType(typeof(LeftRobotFoot));
            leftFootMapping.SetInjector(leftChildInjector);

            InjectionMapping rightFootMapping   = injector.Map(typeof(RobotLeg), "rightLeg");
            Injector         rightChildInjector = injector.CreateChildInjector();

            rightChildInjector.Map(typeof(RobotFoot)).ToType(typeof(RightRobotFoot));
            rightFootMapping.SetInjector(rightChildInjector);

            RobotBody robotBody = injector.InstantiateUnmapped <RobotBody>();

            Assert.IsInstanceOf <RightRobotFoot>(robotBody.rightLeg.ankle.foot, "Right RobotFoot should have RightRobotFoot");
            Assert.IsInstanceOf <LeftRobotFoot>(robotBody.leftLeg.ankle.foot, "Left RobotFoot should have LeftRobotFoot");
        }
        public void ChildInjectorUsesParentForMissingMappings()
        {
            injector.Map(typeof(RobotFoot));
            injector.Map(typeof(RobotToes));

            InjectionMapping leftFootMapping   = injector.Map(typeof(RobotLeg), "leftLeg");
            Injector         leftChildInjector = injector.CreateChildInjector();

            leftChildInjector.Map(typeof(RobotAnkle));
            leftChildInjector.Map(typeof(RobotFoot)).ToType(typeof(LeftRobotFoot));
            leftFootMapping.SetInjector(leftChildInjector);

            InjectionMapping rightFootMapping   = injector.Map(typeof(RobotLeg), "rightLeg");
            Injector         rightChildInjector = injector.CreateChildInjector();

            rightChildInjector.Map(typeof(RobotAnkle));
            rightChildInjector.Map(typeof(RobotFoot)).ToType(typeof(RightRobotFoot));
            rightFootMapping.SetInjector(rightChildInjector);

            RobotBody robotBody = injector.InstantiateUnmapped <RobotBody>();

            Assert.IsInstanceOf <RobotToes>(robotBody.rightLeg.ankle.foot.toes, "Right RobotFoot should have toes");
            Assert.IsInstanceOf <RobotToes>(robotBody.leftLeg.ankle.foot.toes, "Right RobotFoot should have toes");
        }
        public void SealReturnsAnUnsealingKeyObject()
        {
            InjectionMapping config = new InjectionMapping(injector, new MappingId(typeof(Interface), null));

            Assert.NotNull(config.Seal());
        }
        public void ConfigIsInstantiated()
        {
            InjectionMapping config = new InjectionMapping(injector, new MappingId(typeof(Clazz), null));

            Assert.IsInstanceOf <InjectionMapping>(config);
        }
		private void OnPostMappingChange(MappingId mappingId, InjectionMapping instanceType)
		{
			_logger.Debug("Mapping event POST_MAPPING_CHANGE. Mapped type: {1}. Mapped name: {2}",
				mappingId.type, mappingId.key);
		}
		private void OnMappingOverride(MappingId mappingId, InjectionMapping instanceType)
		{
			_logger.Debug("Mapping event MAPPING_OVERRIDE. Mapped type: {1}. Mapped name: {2}",
				mappingId.type, mappingId.key);
		}
Example #19
0
        private bool BindMappings()
        {
            List <Type>           selectedInjectors = _rootInjection.GetCheckedNodes <Type>();
            Func <BindItem, bool> predicate         = x => x.Method != null && x.Method.Body != null;
            List <BindItem>       selectedMethods   = _rootTarget.GetCheckedNodes <BindItem>(predicate);

            if (_mapping == null)
            {
                _mapping = new List <InjectionMapping>();
            }

            if (selectedInjectors.Count == 0 && _mapping.Count == 0)
            {
                MessageBox.Show(@"No code injectors selected!");
                return(false);
            }

            bool injectAllMethods = false;

            if (selectedMethods.Count == 0 && _mapping.Count == 0)
            {
                DialogResult result =
                    MessageBox.Show(
                        @"You have not selected any methods in the Target Assembly that need to be injected!",
                        Application.ProductName, MessageBoxButtons.OK);

                return(false);
            }

            if (_mapping.Count > 0)
            {
                DialogResult result =
                    MessageBox.Show(
                        @"You have already defined a mapping, do you want to add this to the mapping or override? Click Yes if you want to add, No if you want to override & Cancel otherwise",
                        Application.ProductName, MessageBoxButtons.YesNoCancel);

                if (result == System.Windows.Forms.DialogResult.No)
                {
                    _mapping = new List <InjectionMapping>();
                    lstOutput.Items.Clear();
                }
                else if (result == System.Windows.Forms.DialogResult.Cancel)
                {
                    return(false);
                }
            }

            btnRemove.Enabled = true;

            if (injectAllMethods)
            {
                selectedMethods = _rootTarget.GetNodes <BindItem>(predicate);
            }

            for (int methods = 0; methods < selectedMethods.Count; methods++)
            {
                for (int injector = 0; injector < selectedInjectors.Count; injector++)
                {
                    var newMapping = new InjectionMapping(selectedMethods[methods].Assembly,
                                                          selectedMethods[methods].Method,
                                                          selectedInjectors[injector]);

                    //selectedInjectors[injector]

                    if (_mapping.Count(x => x.GetHashCode() == newMapping.GetHashCode()) == 0)
                    {
                        _mapping.Add(newMapping);
                    }
                }
            }

            grdCombination.DataSource = null;
            grdCombination.DataSource = _mapping;
            grdCombination.Refresh();

            return(true);
        }
        public void MappingWithoutProviderEverSetUsesClassProvider()
        {
            InjectionMapping config = new InjectionMapping(injector, new MappingId(typeof(Clazz), null));

            Assert.IsInstanceOf <TypeProvider>(config.GetProvider());
        }
		/*============================================================================*/
		/* Private Functions                                                          */
		/*============================================================================*/

		void MappingOverrideHandler (MappingId mappingId, InjectionMapping instanceType)
		{
			throw new InjectorException("Injector mapping override for type " +
				mappingId.type + " with name " + mappingId.key);
		}
 private void OnPostMappingChange(MappingId mappingId, InjectionMapping instanceType)
 {
     _logger.Debug("Mapping event POST_MAPPING_CHANGE. Mapped type: {1}. Mapped name: {2}",
                   mappingId.type, mappingId.key);
 }
		/*============================================================================*/
		/* Private Functions                                                          */
		/*============================================================================*/
		
		private void PostMappingChange (MappingId mappingId, InjectionMapping mapping)
		{
			DependencyProvider dp = mapping.GetProvider ();
			if (dp is SingletonProvider) 
			{
				dp.PostApply += HandlePostApply;
				_dependencyMappingIds [dp] = mappingId;
			} 
			else if (dp is ValueProvider) 
			{
				AddSingleton(mappingId, _injector.GetInstance(mappingId.type, mappingId.key));
			}
		}
        /*============================================================================*/
        /* Private Functions                                                          */
        /*============================================================================*/

        void MappingOverrideHandler(MappingId mappingId, InjectionMapping instanceType)
        {
            throw new InjectorException("Injector mapping override for type " +
                                        mappingId.type + " with name " + mappingId.key);
        }
        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]);
            }
        }
 private void OnMappingOverride(MappingId mappingId, InjectionMapping instanceType)
 {
     _logger.Debug("Mapping event MAPPING_OVERRIDE. Mapped type: {1}. Mapped name: {2}",
                   mappingId.type, mappingId.key);
 }
 public OtherMappingProvider(InjectionMapping mapping)
 {
     _otherMapping = mapping;
 }