public void Injector_Get_ShouldThrowOnNonInterfaceKey() { using (IInjector injector = Container.CreateInjector()) { Assert.Throws <ArgumentException>(() => injector.Get <Object>(), string.Format(Resources.PARAMETER_NOT_AN_INTERFACE, "iface")); Assert.Throws <ArgumentException>(() => injector.Get(typeof(Object)), string.Format(Resources.PARAMETER_NOT_AN_INTERFACE, "iface")); } }
public void Lifetime_InheritedTransientService_ShouldBeInstantiatedOnEveryRequest() { Container.Service <IInterface_1, Implementation_1_No_Dep>(Lifetime.Transient); using (IInjector injector = Container.CreateChild().CreateInjector()) { Assert.AreNotSame(injector.Get <IInterface_1>(), injector.Get <IInterface_1>()); } }
public FileHandlerActor(IInjector injector) { _fileReader = injector.Get <IDbFileReaderService>(); _googleService = injector.Get <IGDriveService>(); _zipService = injector.Get <IZipService>(); _cluster = Cluster.Get(Context.System); ReceiveAsync <ImportFile>(ImportDriveFile); Receive <IActorRef>(e => _eventStoreActor = e); }
/// <summary> /// Gets an instance of the specified service. /// </summary> /// <param name="service">The specified service type.</param> /// <param name="name">The specified binding name.</param> /// <param name="parameters">The specified parameters.</param> /// <returns>An instance of the service.</returns> public object Get(Type service, string name = null, params IIocParameter[] parameters) { this.NotBeDisposed(); Should.NotBeNull(service, "service"); if (name == null) { return(_injector.Get(service, ConvertParameters(parameters))); } return(_injector.Get(service, name, null, ConvertParameters(parameters))); }
public void Injector_UnderlyingContainer_ShouldServeTheInjector() { Container.Service <IInterface_1, Implementation_1_No_Dep>(Lifetime.Transient); using (IInjector injector = Container.CreateInjector()) { Assert.That(injector.UnderlyingContainer.Parent, Is.SameAs(Container)); Assert.That(injector.Get <IInterface_1>(), Is.TypeOf <Implementation_1_No_Dep>()); injector.UnderlyingContainer.Proxy <IInterface_1>((i, c) => new Implementation_1_Non_Interface_Dep(0)); Assert.That(injector.Get <IInterface_1>(), Is.TypeOf <Implementation_1_Non_Interface_Dep>()); } }
public void Lifetime_ScopedService_ShouldBeInstantiatedOnlyOncePerInjector() { Container.Service <IInterface_1, Implementation_1_No_Dep>(Lifetime.Scoped); using (IInjector injector1 = Container.CreateInjector()) { Assert.AreSame(injector1.Get <IInterface_1>(), injector1.Get <IInterface_1>()); using (IInjector injector2 = Container.CreateInjector()) { Assert.AreSame(injector2.Get <IInterface_1>(), injector2.Get <IInterface_1>()); Assert.AreNotSame(injector1.Get <IInterface_1>(), injector2.Get <IInterface_1>()); } } }
public void Lifetime_SingletonService_MayHaveScopedDependency() { Config.Value.Injector.StrictDI = false; Disposable instance; using (IServiceContainer child = Container.CreateChild()) { child .Service <IDisposable, Disposable>(Lifetime.Scoped) .Service <IInterface_7 <IDisposable>, Implementation_7_TInterface_Dependant <IDisposable> >(Lifetime.Singleton); using (IInjector injector = child.CreateInjector()) { injector.Get <IInterface_7 <IDisposable> >(); } using (IInjector injector = child.CreateInjector()) { instance = (Disposable)injector.Get <IInterface_7 <IDisposable> >().Interface; Assert.That(instance.Disposed, Is.False); } } Assert.That(instance.Disposed); }
public void Parallelism_DependencyResolutionTest( [ValueSource(nameof(Lifetimes))] Lifetime l1, [ValueSource(nameof(Lifetimes))] Lifetime l2, [ValueSource(nameof(Lifetimes))] Lifetime l3, [Values(true, false)] bool createChildContainer) { Container .Service(typeof(IList <>), typeof(MyList <>), l1) .Service <IInterface_7 <IList <object> >, Implementation_7_TInterface_Dependant <IList <object> > >(l2) .Service <IInterface_7 <IInterface_7 <IList <object> > >, Implementation_7_TInterface_Dependant <IInterface_7 <IList <object> > > >(l3); IServiceContainer container = createChildContainer ? Container.CreateChild() : Container; Assert.DoesNotThrow(() => Task.WaitAll ( Enumerable.Repeat(0, TASK_COUNT).Select(_ => Task.Run(Worker)).ToArray() )); void Worker() { using (IInjector injector = container.CreateInjector()) { for (int i = 0; i < 50; i++) { injector.Get <IInterface_7 <IInterface_7 <IList <object> > > >(); } } } }
public void Injector_Misc_RequestedServiceMayStoreItsDependencies( [Values(true, false)] bool useChildContainer, [ValueSource(nameof(Lifetimes))] Lifetime dependant) { Config.Value.Injector.StrictDI = false; Container.Instance <IDisposableEx>(new Disposable()); IServiceContainer sourceContainer = useChildContainer ? Container.CreateChild() : Container; sourceContainer.Service <IInterface_7 <IDisposableEx>, Implementation_7_TInterface_Dependant <IDisposableEx> >(dependant); // // Ket kulonallo injectort hozzunk letre. // for (int i = 0; i < 2; i++) { using (IInjector injector = sourceContainer.CreateInjector()) { IInterface_7 <IDisposableEx> svc = injector.Get <IInterface_7 <IDisposableEx> >(); Assert.That(svc.Interface.Disposed, Is.False); } } }
public void Injector_Misc_RequestedServiceMayStoreTheParentInjector( [Values(true, false)] bool useChildContainer, [ValueSource(nameof(Lifetimes))] Lifetime lifetime) { IServiceContainer sourceContainer = useChildContainer ? Container.CreateChild() : Container; sourceContainer.Service <IInterface_7 <IInjector>, Implementation_7_TInterface_Dependant <IInjector> >(lifetime); // // Ket kulonallo injectort hozzunk letre. // for (int i = 0; i < 2; i++) { using (IInjector injector = sourceContainer.CreateInjector()) { IInterface_7 <IInjector> svc = injector.Get <IInterface_7 <IInjector> >(); // // Mivel az IInjector nem IDisposableEx leszarmazott ezert ugy ellenorzom h dispose-olt e // hogy meghivom rajt a Get()-et. // Assert.DoesNotThrow(() => svc.Interface.Get <IInjector>()); } } }
public void Injector_ShouldResolveItself() { using (IInjector injector = Container.CreateChild().CreateInjector()) { Assert.AreSame(injector, injector.Get <IInjector>()); } }
public object Get(Type type) { object instance; _instanceCache.TryGetValue(type, out instance); if (instance == null) { Type value; if (_typeMap.TryGetValue(type, out value)) { instance = Activator.CreateInstance(value); Inject(instance); if (_instanceCache.ContainsKey(type)) { _instanceCache[type] = instance; } } else if (_parent != null) { instance = _parent.Get(type); } } if (instance == null) { throw new Exception("no instance for " + type); } return(instance); }
public void Injector_Get_ShouldThrowOnNull() { using (IInjector injector = Container.CreateInjector()) { Assert.Throws <ArgumentNullException>(() => injector.Get(null)); } }
public async Task Lifetime_PooledService_ShouldHaveItsOwnInjector() { Container.Service <IInterface_7 <IInjector>, Implementation_7_TInterface_Dependant <IInjector> >(Lifetime.Pooled.WithCapacity(2)); using (IInjector injector1 = Container.CreateInjector()) { IInterface_7 <IInjector> svc1 = injector1.Get <IInterface_7 <IInjector> >(); Assert.That(svc1.Interface, Is.Not.SameAs(injector1)); Assert.That(svc1.Interface.UnderlyingContainer.Parent, Is.SameAs(Container)); await Task.Run(() => { using (IInjector injector2 = Container.CreateInjector()) { IInterface_7 <IInjector> svc2 = injector2.Get <IInterface_7 <IInjector> >(); Assert.That(svc2.Interface, Is.Not.SameAs(injector2)); Assert.That(svc2.Interface.UnderlyingContainer.Parent, Is.SameAs(Container)); Assert.AreNotSame(svc1.Interface, svc2.Interface); } }); } }
public EsEntryPointActor(IInjector injector) { _injector = injector; _elastic = injector.Get <IElasticService>(); Receive <IActorRef>(e => _indexAlbum = e); AwaitElastic(); }
private void OnEvent(T eventData) { foreach (var type in _commandTypes) { var command = (ICommand <T>)Injector.Get(type); command.Execute(eventData); } }
/// <summary> /// Gets the option associated with the given <paramref name="optionName"/>. /// </summary> /// <remarks>Options can be set via the <see cref="IScopeFactory.CreateScope(IReadOnlyDictionary{string, object}?)"/> method.</remarks> public static T?GetOption <T>(this IInjector self, string optionName) { Ensure.Parameter.IsNotNull(self, nameof(self)); return(self .Get <IReadOnlyDictionary <string, object> >($"{ServiceContainer.INTERNAL_SERVICE_NAME_PREFIX}options") .GetValueOrDefault <T>(optionName)); }
public void Lifetime_InheritedScopedService_ShouldBeInstantiatedOnlyOncePerInjector() { IServiceContainer childContainer = Container // nem muszaj dispose-olni Container felszabaditasakor ugy is dispose-olva lesz .Service <IInterface_1, Implementation_1_No_Dep>(Lifetime.Scoped) .CreateChild(); using (IInjector injector1 = childContainer.CreateInjector()) { Assert.AreSame(injector1.Get <IInterface_1>(), injector1.Get <IInterface_1>()); using (IInjector injector2 = childContainer.CreateInjector()) { Assert.AreSame(injector2.Get <IInterface_1>(), injector2.Get <IInterface_1>()); Assert.AreNotSame(injector1.Get <IInterface_1>(), injector2.Get <IInterface_1>()); } } }
public void Lifetime_SingletonService_ShouldBeInstantiatedOnlyOncePerDeclaringContainer() { Container.Service <IInterface_1, Implementation_1_No_Dep>(Lifetime.Singleton); using (IInjector injector1 = Container.CreateInjector()) { using (IInjector injector2 = Container.CreateChild().CreateInjector()) { using (IInjector injector3 = Container.CreateChild().CreateChild().CreateInjector()) { Assert.AreSame(injector1.Get <IInterface_1>(), injector3.Get <IInterface_1>()); Assert.AreSame(injector2.Get <IInterface_1>(), injector3.Get <IInterface_1>()); Assert.AreSame(injector3.Get <IInterface_1>(), injector3.Get <IInterface_1>()); } } } }
public void Injector_Get_ShouldThrowOnOpenGenericType(Lifetime lifetime) { Container.Service(typeof(IInterface_3 <>), typeof(Implementation_3_IInterface_1_Dependant <>), lifetime); using (IInjector injector = Container.CreateInjector()) { Assert.Throws <ArgumentException>(() => injector.Get(typeof(IInterface_3 <>)), Resources.PARAMETER_IS_GENERIC); } }
public void DirectInvocation() { using IInjector injector = Container.CreateInjector(); for (int i = 0; i < OperationsPerInvoke; i++) { injector.Get <IModule>().Foo(); } }
public void Injector_ShouldPassItselfToItsFactories() { Container.Factory <IInterface_7 <IInjector> >(i => new Implementation_7_TInterface_Dependant <IInjector>(i), Lifetime.Transient); using (IInjector i = Container.CreateChild().CreateInjector()) { Assert.AreSame(i, i.Get <IInterface_7 <IInjector> >().Interface); } }
public void Lifetime_SingletonService_ShouldResolveDependencyFromTheDeclaringContainer_DecorationTest() { Config.Value.Injector.StrictDI = false; Container .Service <IInterface_1, Implementation_1_No_Dep>(Lifetime.Transient) .Service <IInterface_2, Implementation_2_IInterface_1_Dependant>(Lifetime.Singleton); IServiceContainer child = Container.CreateChild(); child.Proxy <IInterface_1>((i, curr) => new DecoratedImplementation_1()); using (IInjector injector = child.CreateInjector()) { Assert.That(injector.Get <IInterface_2>().Interface1, Is.InstanceOf <Implementation_1_No_Dep>()); Assert.That(injector.Get <IInterface_1>(), Is.InstanceOf <DecoratedImplementation_1>()); } }
/// <summary> /// Gets the service instance associated with the given interface and (optional) name. /// </summary> /// <typeparam name="TInterface">The "id" of the service to be resolved. It must be an interface.</typeparam> /// <param name="self">The injector itself.</param> /// <param name="name">The (optional) name of the service.</param> /// <returns>The resolved service.</returns> /// <exception cref="ServiceNotFoundException">The service could not be found.</exception> public static TInterface Get <TInterface>(this IInjector self, string?name = null) where TInterface : class { if (self is null) { throw new ArgumentNullException(nameof(self)); } return((TInterface)self.Get(typeof(TInterface), name)); }
public void Injector_Get_ShouldNotThrowOnNonRegisteredDependencyInCaseOfEnumerables() { using (IInjector injector = Container.CreateInjector()) { var enumerable = injector.Get <IEnumerable <IInterface_1> >(); Assert.That(enumerable.Any(), Is.False); } }
public void Lazy() { using (IInjector injector = CreateInjector()) { for (int i = 0; i < OperationsPerInvoke; i++) { injector.Get <IDependantLazy>(name: null); } } }
public void PersistedData_ShouldBeAccessibleAcrossInstances() { Assert.That(Cache !.Add(key, val, TimeSpan.FromDays(1))); using IInjector injector = Container !.CreateInjector(); ICache second = injector.Get <ICache>(); Assert.That(second.TryGetValue(key, out int result)); Assert.That(result, Is.EqualTo(val)); }
public void Enumerable() { using (IInjector injector = CreateInjector()) { for (int i = 0; i < OperationsPerInvoke; i++) { injector.Get <IEnumerable <IDependant> >(name: null); } } }
public void Lifetime_PooledService_ShouldBeInstantiatedUpToNTimesPerDeclaringContainer([Values(1, 2, 3)] int times) { Container.Service <IInterface_1, Implementation_1_No_Dep>(Lifetime.Pooled.WithCapacity(times)); ManualResetEventSlim stop = new ManualResetEventSlim(); Task[] holders = Enumerable.Repeat(0, times).Select(_ => Task.Run(() => { using (IInjector injector = Container.CreateInjector()) { Assert.AreSame(injector.Get <IInterface_1>(), injector.Get <IInterface_1>()); stop.Wait(); } })).ToArray(); Thread.Sleep(100); Assert.False ( Task.Run(() => { using (IInjector injector = Container.CreateInjector()) { injector.Get <IInterface_1>(); } }).Wait(10) ); stop.Set(); Task.WaitAll(holders); Assert.True ( Task.Run(() => { using (IInjector injector = Container.CreateInjector()) { injector.Get <IInterface_1>(); } }).Wait(10) ); }
void InitMediator(Type mediatorType, IMediatedView view) { IMediator mediator = injector.Get <IMediator>(mediatorType); _mediators.Add(mediator); mediator.mediate += Mediate; mediator.onRemove += OnRemove; mediator.view = view; mediator.OnRegister(); view.OnMediate(); }