public static void CanChildRedefineRepositoryInChildWithoutAffectingParent() { /* * Define repository & BL in the parent kernel. * Redefine in the child. */ //Arrange var kernel = new StandardKernel(); kernel.Bind <IMyRepository>().To <MyRepository>().InTransientScope(); kernel.Bind <IMyBusinessLogic>().To <MyBusinessLogic>().InTransientScope(); var myRepository = new Mock <IMyRepository>(); myRepository.Setup(x => x.Get("Test")).Returns("I came from a mock"); var childKernel = new ChildKernel(kernel); childKernel.Bind <IMyRepository>().ToConstant(myRepository.Object).InTransientScope(); //Act var myBusinessLogicFromTheChild = childKernel.Get <IMyBusinessLogic>(); var myBusinessLogicFromTheParent = kernel.Get <IMyBusinessLogic>(); //Assert Assert.Equal("I came from a mock and then the business layer returned it.", myBusinessLogicFromTheChild.DoSomething("Test")); Assert.Equal("You got: Test from myRepository and then the business layer returned it.", myBusinessLogicFromTheParent.DoSomething("Test")); }
public static void CanChildSupplyRepositoryInstanceToParent() { /* * Repository in the child kernel. * Business Logic in the parent kernel. * We expect Ninject to construct the BL in the parent with the child kernels * repository */ //Arrange var kernel = new StandardKernel(); kernel.Bind <IMyBusinessLogic>().To <MyBusinessLogic>().InTransientScope(); var myRepository = new Mock <IMyRepository>(); myRepository.Setup(x => x.Get("Test")).Returns("I came from a mock"); var childKernel = new ChildKernel(kernel); childKernel.Bind <IMyRepository>().ToConstant(myRepository.Object).InSingletonScope(); //Act var myBusinessLogic = childKernel.Get <IMyBusinessLogic>(); //Assert Assert.Equal("I came from a mock and then the business layer returned it.", myBusinessLogic.DoSomething("Test")); }
internal static IUiElementInformation GetUiElementInformation(classic.AutomationElement.AutomationElementInformation information) { try { var singleInfo = new ConstructorArgument("information", information); // 20140122 // IUiElementInformation adapterInformation = Kernel.Get<IUiElementInformation>(singleInfo); IUiElementInformation adapterInformation; // if (null != ChildKernel) { // adapterInformation = ChildKernel.Get<IUiElementInformation>(singleInfo); // var childKernel = GetChildKernel(); // adapterInformation = childKernel.Get<IUiElementInformation>(singleInfo); adapterInformation = ChildKernel.Get <IUiElementInformation>(singleInfo); // childKernel.Dispose(); // } else { // adapterInformation = Kernel.Get<IUiElementInformation>(singleInfo); // } return(adapterInformation); } catch (Exception) { // TODO // write error to error object!!! // Console.WriteLine("Information"); // Console.WriteLine(eFailedToIssueInformation.Message); return(null); } }
public static void CanChildContainerRequestRepositoryFromParentContainer() { /* * Repository in the parent kernel. * Business Logic in the child kernel. * We expect Ninject to ask the parent kernel for the repository */ //Arrange var kernel = new StandardKernel(); var myRepository = new Mock <IMyRepository>(); myRepository.Setup(x => x.Get("Test")).Returns("I came from a mock"); kernel.Bind <IMyRepository>().ToConstant(myRepository.Object); var childKernel = new ChildKernel(kernel); kernel.Bind <IMyBusinessLogic>().To <MyBusinessLogic>(); //Act var myBusinessLogic = childKernel.Get <IMyBusinessLogic>(); //Assert Assert.Contains("I came from a mock and then the business layer returned it.", myBusinessLogic.DoSomething("Test")); }
internal static void InitializeChildKernel() { // if (null != ChildKernel) { // ChildKernel.Dispose(); // ChildKernel = null; // } // 20140129 if (null != ChildKernel && 5 == ChildKernelCreationCounter) { ChildKernel.Dispose(); ChildKernel = null; ChildKernelCreationCounter = 0; } // else { // ChildKernelCreationCounter++; // } // 20140125 if (null == ChildKernel) { ChildKernel = new ChildKernel(Kernel, new ChildKernelModule()); // 20140123 // ChildKernel.Settings.ActivationCacheDisabled = true; ChildKernel.Settings.ActivationCacheDisabled = false; // 20140129 ChildKernelCreationCounter++; } }
private IHandlerBase ResolveCommand(Command Command, CommandDispatcher dispatcher, Type type) { var context = new ChildKernel(_kernel); // long-lived context.BindInstance(dispatcher); context.BindInstance(dispatcher.Environments); context.BindInstance(dispatcher.State); context.BindInstance(dispatcher.Output); context.BindInstance(dispatcher.Parser); context.BindInstance(dispatcher.Handlers); // transient context.BindInstance(Command); context.BindInstance(Command.Arguments); if (dispatcher.Environments.Current != null) { context.Bind(dispatcher.Environments.Current.GetType()).ToConstant(dispatcher.Environments.Current); } var instance = context.Get(type); context.Dispose(); return(instance as IHandlerBase); }
// internal static IUiEltCollection GetUiEltCollectionViaChildKerne(AutomationElementCollection elements, IChildKernel childKernel) // { // if (null == elements) { // return null; // } // try { // var manyElements = new ConstructorArgument("elements", elements); // IUiEltCollection adapterCollection = childKernel.Get<IUiEltCollection>("AutomationElementCollection.NET", manyElements); // return adapterCollection; // } // catch (Exception eFailedToIssueCollection) { // // TODO // // write error to error object!!! // // Console.WriteLine("Collection 01 via ChildKernel"); // // Console.WriteLine(eFailedToIssueCollection.Message); // return null; // } // } internal static IUiEltCollection GetUiEltCollection(classic.AutomationElementCollection elements) { if (null == elements) { return(null); } try { var manyElements = new ConstructorArgument("elements", elements); // 20140122 IUiEltCollection adapterCollection; // = Kernel.Get<IUiEltCollection>("AutomationElementCollection.NET", manyElements); // if (null != ChildKernel) { //Console.WriteLine("child coll"); // adapterCollection = ChildKernel.Get<IUiEltCollection>("AutomationElementCollection.NET", manyElements); // var childKernel = GetChildKernel(); // adapterCollection = childKernel.Get<IUiEltCollection>("AutomationElementCollection.NET", manyElements); adapterCollection = ChildKernel.Get <IUiEltCollection>("AutomationElementCollection.NET", manyElements); // childKernel.Dispose(); // } else { // adapterCollection = Kernel.Get<IUiEltCollection>("AutomationElementCollection.NET", manyElements); // } return(adapterCollection); } catch (Exception) { // TODO // write error to error object!!! // Console.WriteLine("Collection 01"); // Console.WriteLine(eFailedToIssueCollection.Message); return(null); } }
public static void CanWhenTakePrecidenceForBusinessLogicWhenUsedWithChildKernel() { //Arrange var kernel = new StandardKernel(); kernel.Bind <IMyRepository>().To <MyRepository>().InTransientScope(); kernel.Bind <IMyBusinessLogic>().To <MyBusinessLogic>().InTransientScope(); var myRepository = new Mock <IMyRepository>(); myRepository.Setup(x => x.Get("Test")).Returns("I came from a mock"); var shouldReturnTestInstance = new ByValueBooleanWrapper(true); var childKernel = new ChildKernel(kernel); childKernel.Bind <IMyRepository>().ToConstant(myRepository.Object).When(x => shouldReturnTestInstance.Flag); //Act var myBusinessLogic = childKernel.Get <IMyBusinessLogic>(); //Assert Assert.Equal("I came from a mock and then the business layer returned it.", myBusinessLogic.DoSomething("Test")); shouldReturnTestInstance.Flag = false; myBusinessLogic = kernel.Get <IMyBusinessLogic>(); Assert.Equal("You got: Test from myRepository and then the business layer returned it.", myBusinessLogic.DoSomething("Test")); }
public static void Main(string[] args) { Console.WriteLine("Hello NInject!"); // TODO: Implement Functionality Here Kernel = new StandardKernel(new NjModule()); Kernel.Settings.ActivationCacheDisabled = true; var requester = Kernel.Get <Requester>(); for (int i = 0; i < 100000000; i++) { var childKernel = new ChildKernel(Kernel, new NjChildKernelModule()); childKernel.Settings.ActivationCacheDisabled = true; List <IMyObj> list = requester.RequestObjects(childKernel); foreach (MyObj listItem in list) { listItem.Dispose(); } list.Clear(); list = null; childKernel.Dispose(); } Console.Write("Press any key to continue . . . "); Console.ReadKey(true); }
public virtual IChildKernel GetChildKernel(IKernel parentKernel, IList <Type> excludedTypes = null) { var kernel = new ChildKernel(parentKernel); ApplyAutoBindings(kernel, excludedTypes); kernel.Rebind <IKernelFactory>().ToConstant(this); return(kernel); }
/// <summary> /// Create a child kernel - also registers modules in the child kernel to /// work around child container limitations. /// </summary> /// <returns>ChildKernel</returns> private IKernel GetChildKernel() { // TODO - add kernal to context so it's disposed? var child = new ChildKernel(_Kernel); RegisterModulesInternal(child, _ModuleRegistations); return(child); }
internal static IChildKernel GetChildKernel() { var childKernel = new ChildKernel(Kernel, new ChildKernelModule()); // 20140123 // childKernel.Settings.ActivationCacheDisabled = true; childKernel.Settings.ActivationCacheDisabled = false; return(childKernel); }
private static ChildKernel CreateAuthKernel(IResolutionRoot parent) { var kernel = new ChildKernel(parent, new ServerModule(), new AuthServerModule()); kernel.Rebind <IPacketCodeTable>().To <AuthPacketCodeTableV75>().InSingletonScope(); kernel.Rebind <IAuthenticator>().To <StubAuthenticator>().InSingletonScope(); kernel.Rebind <IAccountProvider>().To <StubAccountProvider>().InSingletonScope(); return(kernel); }
private IKernel CreateChildKernel() { var childKernel = new ChildKernel(_kernel); childKernel.Components.Add <IActivationStrategy, MyMonitorActivationStrategy>(); bindAction(childKernel); _childBindingAction(childKernel); return(childKernel); }
//Parameter module is not used here because we are creating a fake one instead. public ChildKernel CreateChildKernelWithModule(IKernel kernel, string module) { var testModule = new TestValidatorModule(); var childKernel = new ChildKernel(kernel); childKernel.Load(testModule); return(childKernel); }
private IChildKernel GetChildKernelWithBindings(IKernel kernel) { IChildKernel childKernel = new ChildKernel(kernel); childKernel.Bind <IVasaClient>().ToMethod(config => { return(new VasaClient()); }); return(childKernel); }
// public static IUiElement GetUiElementViaChildKernel(AutomationElement element, IChildKernel childKernel) // { // if (null == element) { // return null; // } // // try { // var singleElement = new ConstructorArgument("element", element); // IUiElement adapterElement = childKernel.Get<IUiElement>("AutomationElement.NET", singleElement); // // if (Preferences.UseElementsPatternObjectModel) { // // IUiElement proxiedTypedUiElement = // ConvertToProxiedElement( // adapterElement); // // proxiedTypedUiElement.SetSourceElement<AutomationElement>(element); // // return (IUiElement)proxiedTypedUiElement; // as IUiElement; // } else { // // adapterElement.SetSourceElement<AutomationElement>(element); // // return adapterElement; // } // // } // catch (Exception eFailedToIssueElement) { // // TODO // // write error to error object!!! // // Console.WriteLine("Element 01 via ChildKernel"); // // Console.WriteLine(eFailedToIssueElement.Message); // return null; // } // } public static IUiElement GetUiElement(classic.AutomationElement element) { if (null == element) { return(null); } try { var singleElement = new ConstructorArgument("element", element); // 20140122 IUiElement adapterElement; // = Kernel.Get<IUiElement>("AutomationElement.NET", singleElement); // if (null != ChildKernel) { //Console.WriteLine("child elt"); // adapterElement = ChildKernel.Get<IUiElement>("AutomationElement.NET", singleElement); // var childKernel = GetChildKernel(); // adapterElement = childKernel.Get<IUiElement>("AutomationElement.NET", singleElement); adapterElement = ChildKernel.Get <IUiElement>("AutomationElement.NET", singleElement); // childKernel.Dispose(); // (adapterElement as UiElement).ChildKernel = childKernel; // } else { // adapterElement = Kernel.Get<IUiElement>("AutomationElement.NET", singleElement); // } // 20140313 // if (Preferences.UseElementsPatternObjectModel) { // if (Preferences.UseElementsPatternObjectModel || Preferences.UseElementsSearchObjectModel || Preferences.UseElementsCached || Preferences.UseElementsCurrent) { // if (Preferences.UseElementsPatternObjectModel || Preferences.UseElementsCached || Preferences.UseElementsCurrent) { if (Preferences.UseProxy) { IUiElement proxiedTypedUiElement = ConvertToProxiedElement( adapterElement); proxiedTypedUiElement.SetSourceElement <classic.AutomationElement>(element); return((IUiElement)proxiedTypedUiElement); // as IUiElement; } else { adapterElement.SetSourceElement <classic.AutomationElement>(element); return(adapterElement); } } catch (Exception) { // TODO // write error to error object!!! // Console.WriteLine("Element 01"); // Console.WriteLine(eFailedToIssueElement.Message); return(null); } }
public void ChildKernel_IFooBoundInChildKernel_ParentKernelCannotResolveIFoo() { // Assemble var parentKernel = new StandardKernel(); var childKernel = new ChildKernel(parentKernel); childKernel.Bind <IFoo>().To <Foo>(); // Act Assert.Throws <ActivationException>(() => parentKernel.Get <IFoo>()); }
// internal static #endregion Common objects #region IUiElement // 20140210 internal static IExtendedModelHolder GetUiExtendedModelHolder(IUiElement parentElement, classic.TreeScope scope) // internal static IExtendedModelHolder GetUiExtendedModelHolder(IUiElement parentElement, TreeScope scope, int seconds) { if (null == parentElement) { return(null); } try { // 20140122 // IExtendedModelHolder holder = Kernel.Get<IExtendedModelHolder>(new IParameter[] {}); IExtendedModelHolder holder; // if (null != ChildKernel) { // holder = ChildKernel.Get<IExtendedModelHolder>(new IParameter[] {}); // var childKernel = GetChildKernel(); // if (null == (parentElement as UiElement).ChildKernel) { // (parentElement as UiElement).ChildKernel = GetChildKernel(); // } // holder = childKernel.Get<IExtendedModelHolder>(new IParameter[] {}); // holder = (parentElement as UiElement).ChildKernel.Get<IExtendedModelHolder>(new IParameter[] {}); // childKernel.Dispose(); // 20140210 holder = ChildKernel.Get <IExtendedModelHolder>(new IParameter[] {}); // var paramSeconds = new ConstructorArgument("seconds", seconds); // holder = ChildKernel.Get<IExtendedModelHolder>(paramSeconds); // (parentElement as UiElement).ChildKernel.Dispose(); // } else { // holder = Kernel.Get<IExtendedModelHolder>(new IParameter[] {}); // } var proxiedHolder = (IExtendedModelHolder)_generator.CreateClassProxy( typeof(UiExtendedModelHolder), new Type[] { typeof(IExtendedModel) }, new MethodSelectorAspect()); proxiedHolder.SetScope(scope); proxiedHolder.SetParentElement(parentElement); return(proxiedHolder); } catch (Exception) { // TODO // write error to error object!!! // Console.WriteLine("Holder"); // Console.WriteLine(eFailedToIssueHolder.Message); return(null); } }
public SimpleBootstrapper(IResolutionRoot resolutionRoot, ILogger logger) : base(resolutionRoot, logger) { var nexus = new ChildKernel(ResolutionRoot, new NexusServerModule()); _account = new ChildKernel(nexus, new AccountServerModule()); _auth = CreateAuthKernel(nexus); _world = new ChildKernel(nexus, new WorldServerModule()); _channel = new ChildKernel(_world, new ServerModule(), new ChannelServerModule()); // HACK :( nexus.Bind <IAccountService>().ToMethod(ctx => _account.Get <IAccountService>()); }
public void TestReferenceFromParent() { StandardKernel kernel = new StandardKernel(); ChildKernel child = new ChildKernel(kernel); kernel.Bind <IBaseClass, ClassA>().To <ClassA>(); child.Bind <IBaseClass, ClassD_A>().To <ClassD_A>(); var classD_A = child.Get <ClassD_A>(); Assert.IsNotNull(classD_A); Assert.AreEqual("Class A: Class D", classD_A.Message); }
public void Foo() { var kernel = new StandardKernel(); kernel.Bind <IServiceProvider>().ToMethod(GetServiceProvider); kernel.Bind <IService>().To <RootService>(); var childKernel = new ChildKernel(kernel); childKernel.Bind <IService>().To <ChildService>(); kernel.Get <IServiceProvider>().Provide().Should().BeOfType <RootService>(); childKernel.Get <IServiceProvider>().Provide().Should().BeOfType <ChildService>(); }
public ChildKernel CreateChildKernelWithModule(IKernel kernel, string module) { var childKernel = new ChildKernel(kernel); log.Debug("Attempting to load module {moduleName} in ChildKernelCreator", module); if (!PDFile.Exists(module)) { throw new Exception(string.Format(ValidatorModuleNotFound, module)); } childKernel.Load(module); return(childKernel); }
public void TestMultipleNestingChild() { StandardKernel kernel = new StandardKernel(); ChildKernel child1 = new ChildKernel(kernel); ChildKernel child2 = new ChildKernel(child1); kernel.Bind <ClassA>().ToSelf(); child2.Bind <ClassD_A>().ToSelf(); var classD_A = child2.Get <ClassD_A>(); Assert.IsNotNull(classD_A); Assert.AreEqual("Class A: Class D", classD_A.Message); }
public void ChildkernelChangedBinding() { var pluginAKernel = new ChildKernel(this.kernel); pluginAKernel.Load(new BarracksModule()); var pluginBKernel = new ChildKernel(this.kernel); pluginBKernel.Bind <IWeapon>().To <Dagger>().WhenInjectedExactlyInto <Samurai>(); var pluginASamurai = pluginAKernel.Get <Samurai>(); var pluginBSamurai = pluginBKernel.Get <Samurai>(); pluginASamurai.Weapon.Should().NotBeSameAs(pluginBSamurai.Weapon); }
private void ConfigureWebApi(IAppBuilder app, IResolutionRoot resolutionRoot) { var config = new HttpConfiguration(); var kernel = new ChildKernel(resolutionRoot); kernel.Rebind <HttpConfiguration>().ToConstant(config); kernel.Bind <DefaultModelValidatorProviders>().ToConstant(new DefaultModelValidatorProviders(config.Services.GetServices(typeof(ModelValidatorProvider)).Cast <ModelValidatorProvider>())); kernel.Bind <DefaultFilterProviders>().ToConstant(new DefaultFilterProviders(config.Services.GetServices(typeof(IFilterProvider)).Cast <IFilterProvider>())); var dependencyResolver = new NinjectDependencyResolver(kernel); config.DependencyResolver = dependencyResolver; config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new StringEnumConverter()); config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); config.Formatters.Remove(config.Formatters.XmlFormatter); config.EnableCors(new EnableCorsAttribute("http://localhost:8081", "*", "*", "Cookie") { SupportsCredentials = true }); var constraintResolver = new DefaultInlineConstraintResolver(); constraintResolver.ConstraintMap.Add("situation", typeof(SituationConstraint)); config.MapHttpAttributeRoutes(constraintResolver); config.Routes.MapHttpRoute("Default", "{controller}/{id}"); config.EnableSwagger(c => { var baseDirectory = AppDomain.CurrentDomain.BaseDirectory; var commentsFileName = Assembly.GetExecutingAssembly().GetName().Name + ".XML"; var commentsFile = Path.Combine(baseDirectory, "bin", commentsFileName); c.IncludeXmlComments(commentsFile); c.RootUrl(h => $"{h.RequestUri.Scheme}://{h.RequestUri.Host}:{h.RequestUri.Port}/api"); c.SingleApiVersion("v1", "Michael's Place SPA API."); }) .EnableSwaggerUi(); app.Map("/api", webApi => { webApi.UseWebApi(config); }); }
internal static IUiEltCollection GetUiEltCollection() { try { var boolArgument = new ConstructorArgument("fake", true); // IUiEltCollection adapterCollection = Kernel.Get<IUiEltCollection>("Empty", boolArgument); IUiEltCollection adapterCollection = ChildKernel.Get <IUiEltCollection>("Empty", boolArgument); return(adapterCollection); } catch (Exception) { // TODO // write error to error object!!! // Console.WriteLine("Collection 04"); // Console.WriteLine(eFailedToIssueCollection.Message); return(null); } }
public void TestOneChildGetParent() { StandardKernel kernel = new StandardKernel(); ChildKernel child = new ChildKernel(kernel); kernel.Bind <IBaseClass>().To <ClassA>(); child.Bind <IBaseClass>().To <ClassB>(); var classesFromParent = kernel.GetAll <IBaseClass>(); Assert.AreEqual(1, classesFromParent.Count()); var classesFromChild = child.GetAll <IBaseClass>(); Assert.AreEqual(1, classesFromChild.Count()); }
public void ChildKernel_IFooBoundInParentAndChildKernel_ParentCanResolveIFoo() { // Assemble var parentKernel = new StandardKernel(); parentKernel.Bind <IFoo>().To <Foo1>(); var childKernel = new ChildKernel(parentKernel); childKernel.Bind <IFoo>().To <Foo2>(); // Act var foo = parentKernel.Get <IFoo>(); // Act Assert.IsInstanceOf <Foo1>(foo); }
public static void CanSecondChildSupplyDifferentInstanceOfBusinessLogicWithCorrectRepository() { /* * Repository in the child kernel. * Business Logic in the parent kernel. * Dispose the child kernel and try it again! */ //Arrange var kernel = new StandardKernel(); kernel.Bind <IMyRepository>().To <MyRepository>(); kernel.Bind <IMyBusinessLogic>().To <MyBusinessLogic>().InTransientScope(); var myRepository1 = new Mock <IMyRepository>(); myRepository1.Setup(x => x.Get("Test")).Returns("I came from a mock"); var childKernel1 = new ChildKernel(kernel); childKernel1.Bind <IMyRepository>().ToConstant(myRepository1.Object).InSingletonScope(); var businessLogic1 = childKernel1.Get <IMyBusinessLogic>(); childKernel1.Dispose(); var myRepository2 = new Mock <IMyRepository>(); myRepository2.Setup(x => x.Get("Test")).Returns("I came from a mock2"); var childKernel2 = new ChildKernel(kernel); childKernel1.Bind <IMyRepository>().ToConstant(myRepository2.Object).InSingletonScope(); //Act var businessLogic2 = childKernel2.Get <IMyBusinessLogic>(); //Assert Assert.NotEqual(businessLogic1, businessLogic2); Assert.Equal("I came from a mock and then the business layer returned it.", businessLogic1.DoSomething("Test")); Assert.Equal("I came from a mock and then the business layer returned it.", businessLogic2.DoSomething("Test")); }