public void CanBindAndGet() { const string customSingletonId = "customSingleton"; const string customPrototypeId = "customPrototype"; TestEmptyClass defaultSingletonEmptyObj = new TestEmptyClass(); TestEmptyClass customSingletonEmptyObj = new TestEmptyClass(); TestEmptyClass customPrototypeEmptyObj = new TestEmptyClass(); Injector injector = new Injector(); injector.Bind <TestEmptyClass>(defaultSingletonEmptyObj); injector.Bind <TestEmptyClass>(customSingletonEmptyObj, customSingletonId); injector.Bind <TestEmptyClass>(customPrototypeEmptyObj, InstantiationType.PROTOTYPE, customPrototypeId); // Default singleton TestEmptyClass resultDefaultSingletonObj = injector.Get <TestEmptyClass>(); Assert.AreSame(defaultSingletonEmptyObj, resultDefaultSingletonObj); // custom singleton TestEmptyClass resultCustomSingletonObj = injector.Get <TestEmptyClass>(customSingletonId); Assert.AreSame(customSingletonEmptyObj, resultCustomSingletonObj); Assert.AreNotSame(defaultSingletonEmptyObj, resultCustomSingletonObj); // custom prototype TestEmptyClass resultCustomPrototypeObj = injector.Get <TestEmptyClass>(customPrototypeId); Assert.AreNotSame(customPrototypeEmptyObj, resultCustomPrototypeObj); Assert.AreEqual(typeof(TestEmptyClass), resultCustomPrototypeObj.GetType()); }
static void Main(string[] args) { var injector = new Injector(); injector.Bind<IClock, SystemClock>(); injector.Bind<DateTimeZone>(DateTimeZone.Utc); injector.Bind<Instant>(Instant.FromUtc(2000, 1, 1, 0, 0, 0)); injector.Bind<CalendarSystem>(CalendarSystem.Iso); }
public void Should_ReturnConcreteObject_When_RegisteredWithInstance() { _injector.Bind(new StubClass()); var registeredObject = _injector.Resolve(typeof(StubClass)); registeredObject.Should().BeOfType <StubClass>(); }
public void Main_With_Generic_Injector() { var injector = new Injector(); injector.Bind<IClock, SystemClock>(); injector.Bind<DateTimeZone>(DateTimeZone.GetSystemDefault()); injector.Bind<Instant>(Instant.FromUtc(2000, 1,1,0,0,0)); injector.Bind<CalendarSystem>(CalendarSystem.Iso); var presenter = injector.Resolve<DiaryPresenter>(); presenter.Start(); }
public void CanGetIsManagedType() { Injector injector = new Injector(); injector.Bind <TestEmptyClass>(new TestEmptyClass()); injector.Bind <int>(568); Assert.IsTrue(injector.IsManagedType <TestEmptyClass>()); Assert.IsTrue(injector.IsManagedType <int>()); Assert.IsFalse(injector.IsManagedType <float>()); Assert.IsFalse(injector.IsManagedType <TestClassWithConstructor>()); }
static void Main() { Injector injector = new Injector(); injector.Bind <IClock, SystemClock>().InSingletonScope(); injector.Bind <DateTimeZone>(DateTimeZone.GetSystemDefault()); injector.Bind <Instant>(Instant.FromUtc(2000, 1, 1, 0, 0, 0)); injector.Bind <CalendarSystem>(CalendarSystem.Iso); var presenter = injector.CreateDiaryPresenter() presenter.Start(); }
public void AddPropertyInjectorTest() { var binding = injector.Bind <TestClassAddPropertyInjectorTest> (); binding.AddPropertyInjector((TestClassAddPropertyInjectorTest v) => v.Other); binding.AddPropertyInjector((TestClassAddPropertyInjectorTest v) => v.MyStringProperty, () => "Goldfinger"); var result = injector.Resolve <TestClassAddPropertyInjectorTest>(); Assert.AreEqual("Goldfinger", result.MyStringProperty); Assert.AreEqual(7, result.Other.Id); }
protected void Start() { _injector = new Injector(); _injector.Bind <InjectableA>(new InjectableA()); _injector.Bind <InjectableB>(new InjectableB()); var injected = new Injected(); _injector.Inject(injected); injected.PrintMessages(); }
public void CanNewInstance() { const string customId = "custom"; const float defaultTestFloat = 0.6f; const float customTestFloat = 1.14f; TestStruct testStruct; testStruct.idx = 8; testStruct.value = 99; const int defaultTestInt = 6; const int customTestInt = 88; const long defaultTestLong = 8864; const long customTestLong = 7143; const short defaultTestShort = 3; const short customTestShort = 16; Injector injector = new Injector(); // Dependency cannot be found Assert.Throws <IoCConstructorException>(() => injector.NewInstance <TestClassWithConstructor>()); injector.Bind <float>(defaultTestFloat); injector.Bind <float>(customTestFloat, customId); injector.Bind <TestStruct>(testStruct); injector.Bind <int>(defaultTestInt); injector.Bind <int>(customTestInt, customId); injector.Bind <TestEmptyClass>(injector.NewInstance <TestEmptyClass>()); injector.Bind <long>(defaultTestLong); injector.Bind <long>(customTestLong, customId); injector.Bind <short>(defaultTestShort); injector.Bind <short>(customTestShort, customId); // Dependecy can be found. // Prototype Assert.DoesNotThrow(() => injector.NewInstance <TestClassWithConstructor>()); //Singleton TestClassWithConstructorInjection obj = injector.NewInstance <TestClassWithConstructorInjection>(); Assert.NotNull(obj); Assert.AreEqual(injector.Get <int>(customId), obj.IntValue); Assert.AreEqual(injector.Get <float>(customId), obj.FloatValue); Assert.AreEqual(injector.Get <TestStruct>(), obj.TestStruct); Assert.AreEqual(injector.Get <long>(), obj.LongValue); Assert.AreEqual(injector.Get <short>(customId), obj.ShortValue); }
static void Main(string[] args) { log.Write("beginnign program"); Injector injector = new Injector(); injector.Bind <IHelper, HelperImplementerClass>(); injector.Bind <IMessager, MessageService>(); var presenter = injector.Resolve <Presenter>(); presenter.present(); Console.ReadLine(); }
public void TestPostConstruct() { var injector = new Injector(); var testClass = new TestClass(); var otherTestClass = new TestClassFromInterface(); injector.Bind <TestClass>(testClass); injector.Bind <ITestInterface>(otherTestClass); var injectingClass = new TestInjectingClass(); injector.Bind <TestInjectingClass>(injectingClass); injector.PostBindings(); Assert.IsTrue(injectingClass.PostConstructExecuted, "PostConstruct method should be executed"); }
public void TestInject() { var injector = new Injector(); var testClass = new TestClass(); var otherTestClass = new TestClassFromInterface(); injector.Bind <TestClass>(testClass); injector.Bind <ITestInterface>(otherTestClass); var injectingClass = new TestInjectingClass(); injector.Inject(injectingClass); Assert.AreEqual(testClass, injectingClass.GetTestClass(), "instance should be injected"); Assert.AreEqual(otherTestClass, injectingClass.GetOtherTestClass(), "instance should be injected"); }
public void CanNewInstanceFail() { Injector injector = new Injector(); Assert.DoesNotThrow(() => injector.Bind(new TestEmptyClass())); Assert.Throws <IoCConstructorException>(() => injector.NewInstance <TestClassWithConstructorInjectionFail>()); }
public void ResolveConcreteTypeSame() { Injector injector = Injector.NewInstance(); injector.Bind <MyTestClass, MyTestClass>(); var result = injector.Resolve <MyTestClass>(); Assert.IsInstanceOf <MyTestClass>(result); }
public void TestPostBindings() { var injector = new Injector(); var testClass = new TestClass(); var otherTestClass = new TestClassFromInterface(); injector.Bind <TestClass>(testClass); injector.Bind <ITestInterface>(otherTestClass); var injectingClass = new TestInjectingClass(); injector.Bind <TestInjectingClass>(injectingClass); injector.PostBindings(); Assert.AreEqual(injectingClass, injector.GetInstance <TestInjectingClass>(), "should return bound instance"); Assert.AreEqual(testClass, injectingClass.GetTestClass(), "instance should be injected"); Assert.AreEqual(otherTestClass, injectingClass.GetOtherTestClass(), "instance should be injected"); }
public void ResolveInterface() { Injector injector = Injector.NewInstance(); injector.Bind <MyInterface, MyTestClass>(); var result = injector.Resolve <MyInterface>(); Assert.IsInstanceOf <MyTestClass>(result); }
public void TestBindClassGetInterface() { var injector = new Injector(); var testClass = new TestClassFromInterface(); injector.Bind <TestClassFromInterface>(testClass); Assert.Throws <InjectorException>(() => { injector.GetInstance <ITestInterface>(); }, "injector should throw on get interface if just the class was bound."); }
public void CanGetContainsCustomId() { string defaultId = "default"; string customId1 = "custom1"; string customId2 = "custom2"; Injector injector = new Injector(); injector.Bind <TestEmptyClass>(new TestEmptyClass(), customId1); injector.Bind <TestEmptyClass>(new TestEmptyClass(), customId2); injector.Bind <int>(662, customId1); injector.Bind <int>(477); Assert.IsTrue(injector.ContainsCustomId <TestEmptyClass>(customId1)); Assert.IsTrue(injector.ContainsCustomId <TestEmptyClass>(customId2)); Assert.IsFalse(injector.ContainsCustomId <TestEmptyClass>(defaultId)); Assert.IsTrue(injector.ContainsCustomId <int>(customId1)); Assert.IsTrue(injector.ContainsCustomId <int>(defaultId)); Assert.IsFalse(injector.ContainsCustomId <int>(customId2)); }
public void TestBindClass() { var injector = new Injector(); var testClass = new TestClass(); injector.Bind <TestClass>(testClass); Assert.DoesNotThrow(() => { injector.GetInstance <TestClass>(); }, "Injector should have instance."); Assert.AreEqual(testClass, injector.GetInstance <TestClass>(), "instance should be the same"); }
public void SetUp() { injector = new Injector(); injector.Activate = false; injector.Bind("public field", "public-field"); injector.Bind("protected field", "protected-field"); injector.Bind("private field", "private-field"); injector.Bind("public property", "public-property"); injector.Bind("protected property", "protected-property"); injector.Bind("private property", "private-property"); injector.Bind("parameter", "parameter"); injector.Bind("bound-string"); }
public override void Load() { PrepareForLoad(); var singletonTypes = Singletons.SelectMany(singleton => singleton.Item1); var types = GetType().GetTypeInfo().Assembly.GetTypes().Except(singletonTypes); ApplyConventions(types); foreach (var singleton in Singletons) { Injector.Bind(singleton.Item1.ToArray()).To(singleton.Item2).InSingletonScope(); } }
public void TestGetFromParent() { var parentInjector = new Injector(); var injector = new Injector(parentInjector); var testClass = new TestClass(); parentInjector.Bind(testClass); Assert.DoesNotThrow(() => { injector.GetInstance <TestClass>(); }, "Injector should have instance from parent."); Assert.AreEqual(testClass, injector.GetInstance <TestClass>(), "instance from parent should be the same"); }
public void TestBindInterface() { var injector = new Injector(); var testClass = new TestClassFromInterface(); injector.Bind <ITestInterface>(testClass); Assert.Throws <InjectorException>(() => { injector.GetInstance <TestClassFromInterface>(); }, "getting class should throw if only interface bound."); Assert.DoesNotThrow(() => { injector.GetInstance <ITestInterface>(); }, "Injector should have instance."); Assert.AreEqual(testClass, injector.GetInstance <ITestInterface>(), "instance should be the same"); }
public static void InstancedInstantiation() { const string legendId = "legend"; Injector injector = new Injector(); injector.BindNewInstance <Weapon, HeroDefaultWeapon>(); injector.BindNewInstance <Armor, HeroDefaultArmor>(); injector.BindNewInstance <Character, Hero>(); Weapon excalibur = new Weapon(8); injector.Bind(excalibur, "excalibur"); Armor genjiArmor = new Armor(8); injector.Bind(genjiArmor, "genjiArmor"); injector.Bind(new Hero(excalibur, genjiArmor), legendId); Hero legend = injector.Get <Hero>(legendId); Console.WriteLine(string.Format("This legend has weapon with atk = {0} and armor with def = {1}.", legend.Weapon.Atk, legend.Armor.Def)); }
public void TestOverrideParent() { var parentInjector = new Injector(); var injector = new Injector(parentInjector); var testClassInParent = new TestClass(); parentInjector.Bind(testClassInParent); var testClassInChild = new TestClass(); injector.Bind(testClassInChild); Assert.DoesNotThrow(() => { injector.GetInstance <TestClass>(); }, "Injector should have instance."); Assert.AreEqual(testClassInChild, injector.GetInstance <TestClass>(), "instance from parent should be overridden"); }
protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); // create the dependency injections Injector.Bind(GetType(), typeof(ConfigurationService)); Injector.Bind <SingleTaskScheduler>(); // create the main window var mainWindow = Injector.Get <MainWindow>(); // assign the view model to the window var mainWindowViewModel = Injector.Get <MainWindowViewModel>(); mainWindow.DataContext = mainWindowViewModel; // show the window mainWindow.Show(); }
public void TestGetFromAncestor() { var parentInjector = new Injector(); var testClass = new TestClass(); parentInjector.Bind(testClass); Injector injector = null; for (var i = 0; i < 10; i++) { injector = new Injector(parentInjector); parentInjector = injector; } Assert.DoesNotThrow(() => { injector.GetInstance <TestClass>(); }, "Injector should have instance from ancestor."); Assert.AreEqual(testClass, injector.GetInstance <TestClass>(), "instance from ancestor should be the same"); }
public static void TestNew1() { injector.Bind <MyTestInterface, MyTestClass>(); var result1 = injector.Resolve <MyTestInterface>(); var stopwatch = new Stopwatch(); stopwatch.Start(); for (int i = 0; i < 1000000; i++) { var result = injector.Resolve <MyTestInterface>(); } Console.WriteLine("IfFastInjector Resolve<T>(1000000) INSTANCE - NO properties: " + stopwatch.ElapsedTicks.ToString()); var typeT = typeof(MyTestInterface); stopwatch.Restart(); for (int i = 0; i < 1000000; i++) { var result = injector.Resolve(typeT); } Console.WriteLine("IFastInjector Resolve(<T>)(1000000) INSTANCE - NO properties: " + stopwatch.ElapsedTicks.ToString()); }
public override void CreateBinding(Type type) { var targetType = GetTargetType(type); Injector.Bind(type).To(targetType).InSingletonScope(); }
public BindingAttributeTest() { injector.Bind <Inner> ().AsSingleton(); injector.Bind <Outer> ().AsSingleton(); }
public static void MakeBind() { Injector.Bind <GUI.ViewModel.ExpertAnswers.ExpertAnswersViewModel>().To <GUI.View.ExpertAnswersView>().WithFlag(ViewBindFlag); Injector.Bind <GUI.ViewModel.SetValueViewModel>().To <GUI.View.SetValueView>().WithFlag(ViewBindFlag); Injector.Bind <GUI.ViewModel.SelectItemViewModel <GUI.Entity.User> >().To <GUI.View.SelectItemView>().WithFlag(ViewBindFlag); Injector.Bind <GUI.ViewModel.SelectItemViewModel <GUI.Entity.Questionnaire> >().To <GUI.View.SelectItemView>().WithFlag(ViewBindFlag); Injector.Bind <GUI.ViewModel.SelectItemViewModel <GUI.Entity.WorkQuestionnaire> >().To <GUI.View.WorkQuestionnaireSelectView>().WithFlag(ViewBindFlag); Injector.Bind <GUI.ViewModel.SelectItemViewModel <GUI.Entity.ExpertGroup> >().To <GUI.View.SelectItemView>().WithFlag(ViewBindFlag); Injector.Bind <GUI.ViewModel.UserViewModel>().To <GUI.View.UserEditView>().WithFlag(ViewBindFlag); Injector.Bind <GUI.ViewModel.UsersViewModel>().To <GUI.View.UsersEditView>().WithFlag(ViewBindFlag); Injector.Bind <GUI.ViewModel.ExpertGroupViewModel>().To <GUI.View.ExpertGroupView>().WithFlag(ViewBindFlag); Injector.Bind <GUI.ViewModel.ExpertGroupsViewModel>().To <GUI.View.ExpertGroupsEditView>().WithFlag(ViewBindFlag); Injector.Bind <GUI.ViewModel.QuestionnaireViewModel>().To <GUI.View.QuestionnaireEditView>().WithFlag(ViewBindFlag); Injector.Bind <GUI.ViewModel.QuestionnairesViewModel>().To <GUI.View.QuestionnairesEditView>().WithFlag(ViewBindFlag); Injector.Bind <GUI.ViewModel.AdminViewModel>().To <GUI.View.AdminView>().WithFlag(ViewBindFlag); Injector.Bind <GUI.ViewModel.StartViewModel>().To <GUI.View.StartView>().WithFlag(ViewBindFlag); Injector.Bind <GUI.ViewModel.WorkQuestionnairesViewModel>().To <GUI.View.WorkQuestionnairesEditView>().WithFlag(ViewBindFlag); //Injector.Bind<GUI.ViewModel.WorkQuestionnairesViewModel>().To<GUI.View.WorkQuestionnairesView>(GUI.ViewManager.ReadOnlyFlag); Injector.Bind <GUI.ViewModel.WorkQuestionnaireViewModel>().To <GUI.View.WorkQuestionnaireEditView>().WithFlag(GUI.ViewManager.EditFlag); Injector.Bind <GUI.ViewModel.WorkQuestionnaireViewModel>().To <GUI.View.WorkQuestionnaireDoView>().WithFlag(GUI.ViewManager.ReadOnlyFlag); Injector.Bind <GUI.ViewModel.SetValueViewModel>().To <GUI.ViewModel.SetValueViewModel>() .NotSaveInstance(); Injector.Bind <GUI.ViewModel.ExpertAnswers.ExpertAnswersViewModel>().To <GUI.ViewModel.ExpertAnswers.ExpertAnswersViewModel>() .NotSaveInstance(); Injector.Bind <GUI.ViewModel.Controls.ScrollItemsViewModel>().To <GUI.ViewModel.Controls.ScrollItemsViewModel>() .NotSaveInstance(); Injector.Bind <GUI.ViewModel.SelectItemViewModel <GUI.Entity.ExpertGroup> >().To <GUI.ViewModel.SelectItemViewModel <GUI.Entity.ExpertGroup> >() .NotSaveInstance(); Injector.Bind <GUI.ViewModel.StartViewModel>().To <GUI.ViewModel.StartViewModel>() .NotSaveInstance(); Injector.Bind <GUI.ViewModel.AdminViewModel>().To <GUI.ViewModel.AdminViewModel>() .NotSaveInstance(); Injector.Bind <GUI.ViewModel.SelectItemViewModel <GUI.Entity.User> >().To <GUI.ViewModel.SelectItemViewModel <GUI.Entity.User> >() .NotSaveInstance(); Injector.Bind <GUI.ViewModel.SelectItemViewModel <GUI.Entity.Questionnaire> >().To <GUI.ViewModel.SelectItemViewModel <GUI.Entity.Questionnaire> >() .NotSaveInstance(); Injector.Bind <GUI.ViewModel.SelectItemViewModel <GUI.Entity.WorkQuestionnaire> >().To <GUI.ViewModel.SelectItemViewModel <GUI.Entity.WorkQuestionnaire> >() .NotSaveInstance(); Injector.Bind <GUI.ViewModel.ExpertGroupViewModel>().To <GUI.ViewModel.ExpertGroupViewModel>() .NotSaveInstance(); Injector.Bind <GUI.ViewModel.ExpertGroupsViewModel>().To <GUI.ViewModel.ExpertGroupsViewModel>() .NotSaveInstance(); Injector.Bind <GUI.ViewModel.QuestionnaireViewModel>().To <GUI.ViewModel.QuestionnaireViewModel>() .NotSaveInstance(); Injector.Bind <GUI.ViewModel.QuestionnairesViewModel>().To <GUI.ViewModel.QuestionnairesViewModel>() .NotSaveInstance(); Injector.Bind <GUI.ViewModel.UserViewModel>().To <GUI.ViewModel.UserViewModel>() .NotSaveInstance(); Injector.Bind <GUI.ViewModel.UsersViewModel>().To <GUI.ViewModel.UsersViewModel>() .NotSaveInstance(); Injector.Bind <GUI.ViewModel.WorkQuestionnairesViewModel>().To <GUI.ViewModel.WorkQuestionnairesViewModel>() .NotSaveInstance(); Injector.Bind <GUI.ViewModel.WorkQuestionnaireViewModel>().To <GUI.ViewModel.WorkQuestionnaireViewModel>() .NotSaveInstance(); Injector.Bind <Context.IContext>().To <Context.Context>() .NotSaveInstance(); Injector.Bind <GUI.Model.WorkQuestionnaireModel>().To <GUI.Model.WorkQuestionnaireModel>() .NotSaveInstance(); Injector.Bind <GUI.Model.AnswerModel>().To <GUI.Model.AnswerModel>() .NotSaveInstance(); Injector.Bind <GUI.Model.MainModel>().To <GUI.Model.MainModel>() .NotSaveInstance(); Injector.Bind <GUI.Model.QuestionModel>().To <GUI.Model.QuestionModel>() .NotSaveInstance(); Injector.Bind <GUI.Model.QuestionnaireModel>().To <GUI.Model.QuestionnaireModel>() .NotSaveInstance(); Injector.Bind <GUI.Model.ExpertGroupModel>().To <GUI.Model.ExpertGroupModel>() .NotSaveInstance(); Injector.Bind <GUI.Model.ExpertGroupUserModel>().To <GUI.Model.ExpertGroupUserModel>() .NotSaveInstance(); Injector.Bind <GUI.Model.UserModel>().To <GUI.Model.UserModel>() .NotSaveInstance(); Injector.Bind <Repository.UserRepository>().To <Repository.UserRepository>() .NotSaveInstance(); Injector.Bind <Repository.ExpertGroupUserRepository>().To <Repository.ExpertGroupUserRepository>() .NotSaveInstance(); Injector.Bind <Repository.ExpertGroupRepository>().To <Repository.ExpertGroupRepository>() .NotSaveInstance(); Injector.Bind <Repository.AnswerRepository>().To <Repository.AnswerRepository>() .NotSaveInstance(); Injector.Bind <Repository.QuestionnaireRepository>().To <Repository.QuestionnaireRepository>() .NotSaveInstance(); Injector.Bind <Repository.QuestionRepository>().To <Repository.QuestionRepository>() .NotSaveInstance(); Injector.Bind <Repository.WorkQuestionnaireRepository>().To <Repository.WorkQuestionnaireRepository>() .NotSaveInstance(); Injector.Bind <Repository.ExpertAnswerRepository>().To <Repository.ExpertAnswerRepository>() .NotSaveInstance(); }