internal static TImplementation GetInstance <TImplementation>( ScopedRegistration <TImplementation> registration, Scope scope) where TImplementation : class { if (scope == null) { return(GetScopelessInstance(registration)); } return(scope.GetInstanceInternal(registration)); }
private static object GetScopelessInstance(ScopedRegistration registration) { if (registration.Container.IsVerifying) { return(registration.Container.VerificationScope !.GetInstanceInternal(registration)); } throw new ActivationException( StringResources.TheServiceIsRequestedOutsideTheContextOfAScopedLifestyle( registration.ImplementationType, registration.Lifestyle)); }
private static TImplementation GetScopelessInstance <TImplementation>( ScopedRegistration <TImplementation> registration) where TImplementation : class { if (registration.Container.IsVerifying) { return(registration.Container.VerificationScope.GetInstanceInternal(registration)); } throw new ActivationException( StringResources.TheServiceIsRequestedOutsideTheContextOfAScopedLifestyle( typeof(TImplementation), registration.Lifestyle)); }
private TImplementation CreateAndCacheInstance <TImplementation>( ScopedRegistration <TImplementation> registration) where TImplementation : class { TImplementation instance = registration.InstanceCreator.Invoke(); this.cachedInstances[registration] = instance; if (instance is IDisposable disposable) { this.RegisterForDisposalInternal(disposable); } return(instance); }
internal static TService GetInstance <TService, TImplementation>( ScopedRegistration <TService, TImplementation> registration, Scope scope) where TImplementation : class, TService where TService : class { if (scope == null) { return(GetScopelessInstance(registration)); } lock (scope.syncRoot) { return(scope.GetInstance(registration)); } }
private TImplementation CreateAndCacheInstance <TImplementation>( ScopedRegistration <TImplementation> registration, Dictionary <Registration, object> cache) where TImplementation : class { // registration.BuildExpression has been called, and InstanceCreate thus been initialized. Func <TImplementation> instanceCreator = registration.InstanceCreator !; TImplementation instance = instanceCreator.Invoke(); cache[registration] = instance; if (instance is IDisposable disposable && !registration.SuppressDisposal) { this.RegisterForDisposalInternal(disposable); } return(instance); }
private object GetInstanceInternal(ScopedRegistration registration) { lock (this.syncRoot) { this.RequiresInstanceNotDisposed(); bool cacheIsEmpty = this.cachedInstances is null; if (this.cachedInstances is null) { this.cachedInstances = new Dictionary <Registration, object>(ReferenceEqualityComparer <Registration> .Instance); } return(!cacheIsEmpty && this.cachedInstances.TryGetValue(registration, out object?instance) ? instance : this.CreateAndCacheInstance(registration, this.cachedInstances)); } }
private TService CreateAndCacheInstance <TService, TImplementation>( ScopedRegistration <TService, TImplementation> registration) where TService : class where TImplementation : class, TService { TService service = registration.InstanceCreator.Invoke(); this.cachedInstances[registration] = service; if (registration.RegisterForDisposal) { var disposable = service as IDisposable; if (disposable != null) { this.RegisterForDisposalInternal(disposable); } } return(service); }
private TService GetInstance <TService, TImplementation>( ScopedRegistration <TService, TImplementation> registration) where TService : class where TImplementation : class, TService { this.RequiresInstanceNotDisposed(); bool cacheIsEmpty = this.cachedInstances == null; if (this.cachedInstances == null) { this.cachedInstances = new Dictionary <Registration, object>(ReferenceEqualityComparer <Registration> .Instance); } object instance; return(!cacheIsEmpty && this.cachedInstances.TryGetValue(registration, out instance) ? (TService)instance : this.CreateAndCacheInstance(registration)); }
private TImplementation GetInstanceInternal <TImplementation>( ScopedRegistration <TImplementation> registration) where TImplementation : class { lock (this.syncRoot) { this.RequiresInstanceNotDisposed(); bool cacheIsEmpty = this.cachedInstances == null; if (this.cachedInstances == null) { this.cachedInstances = new Dictionary <Registration, object>(ReferenceEqualityComparer <Registration> .Instance); } return(!cacheIsEmpty && this.cachedInstances.TryGetValue(registration, out object?instance) ? (TImplementation)instance : this.CreateAndCacheInstance(registration, this.cachedInstances)); } }
private object CreateAndCacheInstance( ScopedRegistration registration, Dictionary <Registration, object> cache) { // registration.BuildExpression has been called, and InstanceCreate thus been initialized. Func <object> instanceCreator = registration.InstanceCreator !; object instance = instanceCreator.Invoke(); cache[registration] = instance; if (!registration.SuppressDisposal) { #if NETSTANDARD1_0 || NETSTANDARD1_3 || NET45 if (instance is IDisposable) #else if (instance is IDisposable || instance is IAsyncDisposable) #endif { this.RegisterForDisposalInternal(instance); } } return(instance); }