internal override bool TryGetAtIndex(int index, out ILifetime cached) { if (index < cache.Count) { cached = cache[index]; return(true); } else { var indexForSublist = index - cache.Count; for (int i = 0; i < sublists.Count; i++) { if (indexForSublist < sublists[i].cache.Count) { cached = sublists[i].cache[indexForSublist]; return(true); } else { indexForSublist -= sublists[i].cache.Count; } } } cached = default(ILifetime); return(false); }
public bool TryBuildExpression(IBuildContext buildContext, ILifetime lifetime, out Expression expression, out Exception error) { var type = buildContext.Key.Type.Descriptor(); var keyComparer = buildContext.Key.Tag as IComparer <Key>; var elementType = type.GetElementType(); if (elementType == null) { throw new BuildExpressionException($"Unsupported array type {type}.", null); } var allKeys = GetKeys(buildContext.Container, elementType); if (keyComparer != null) { allKeys = allKeys.OrderBy(i => i, keyComparer); } var keys = allKeys.ToArray(); var expressions = new Expression[keys.Length]; for (var i = 0; i < keys.Length; i++) { var context = buildContext.CreateChild(keys[i], buildContext.Container); expressions[i] = context.CreateExpression(); } expression = Expression.NewArrayInit(elementType, expressions); error = default(Exception); return(true); }
public TypeRegistration(TypeRegistrationKey key, Type mapTo, ILifetime lifetime, Func <ISimpleContainer, object> factory) { this.Key = key; this.MapTo = mapTo; this.Lifetime = lifetime; this.Factory = factory; }
public override void AddObserver(ILifetime instance, object objObserver, bool forceCachedEvents) { var observer = (ILifetimeObserver <T>)objObserver; if (observers.TryGetValue(instance, out var list)) { if (!list.Contains(observer)) { list.Add(observer); } } else { observers.Add(instance, new List <ILifetimeObserver <T> > { observer }); } if (forceCachedEvents) { if (instance.IsLifetimeInitialized) { observer.OnInitialized((T)instance); } } }
public AdapterLifetime(ILifetime lifetime) { if (lifetime == null) throw new ArgumentNullException("lifetime"); this.lifetime = lifetime; }
private static ILifetime ResolveLifetime(ServiceDescriptor serviceDescriptor) { if (serviceDescriptor.ImplementationInstance != null) { return(null); } ILifetime lifetime = null; switch (serviceDescriptor.Lifetime) { case ServiceLifetime.Scoped: lifetime = new PerScopeLifetime(); break; case ServiceLifetime.Singleton: lifetime = new PerContainerLifetime(); break; case ServiceLifetime.Transient: lifetime = new PerRequestLifeTime(); break; } return(lifetime); }
private static void ScopeToLifetime(ConnectionScope scope, ILifetime lifetime) { switch (scope) { case ConnectionScope.ByRequest: lifetime.Request(); break; case ConnectionScope.ByThread: lifetime.Thread(); break; case ConnectionScope.BySession: lifetime.Session(); break; case ConnectionScope.KeepAlive: lifetime.Permanent(); break; case ConnectionScope.AlwaysNew: lifetime.AlwaysNew(); break; default: throw new ArgumentOutOfRangeException("scope"); } }
public static void AddInitItem(ILifetime item) { if (item != null) { InitList.Add(item); } }
public void Registration(IEnumerable <RegistrationDefinition> definitions) { foreach (var definition in definitions) { ILifetime lifetime = null; switch (definition.RegistrationLifestyle) { case RegistrationLifestyle.Singleton: lifetime = new PerContainerLifetime(); break; case RegistrationLifestyle.SingletonPerScope: lifetime = new PerScopeLifetime(); break; } if (definition.RegistrationMode == RegistrationMode.Single) { _container.Register(definition.ExportType, definition.ActivationType, lifetime); } else { _container.Register(definition.ExportType, definition.ActivationType, definition.ActivationType.Name, lifetime); } } }
public static ILifetime[] GetInitItems(int min, int max) { ILifetime[] found = null; var list = _InitList; if (list != null) { int start = -1, cnt = 0; list.Sort((ia, ib) => ia.Order - ib.Order); for (int i = 0; i < list.Count; ++i) { var order = list[i].Order; if (order >= min && order <= max) { if (start < 0) { start = i; } ++cnt; } } if (start >= 0) { found = new ILifetime[cnt]; list.CopyTo(start, found, 0, cnt); } } return(found ?? new ILifetime[0]); }
public static void OnDestroyed(ILifetime lifetime) { if (IsRunning) { Instance.OnDestroyedInternal(lifetime); } }
public static void OnInitialized(ILifetime lifetime) { if (IsRunning) { Instance.OnInitializedInternal(lifetime); } }
/// <inheritdoc /> public bool TryBuildExpression(IBuildContext buildContext, ILifetime lifetime, out Expression expression, out Exception error) { if (buildContext == null) { throw new ArgumentNullException(nameof(buildContext)); } var typesMap = new Dictionary <Type, Type>(); try { if (_instanceExpression.Type.Descriptor().IsGenericTypeDefinition()) { TypeMapper.Shared.Map(_instanceExpression.Type, buildContext.Key.Type, typesMap); foreach (var mapping in typesMap) { buildContext.MapType(mapping.Key, mapping.Value); } } } catch (BuildExpressionException ex) { error = ex; expression = default(Expression); return(false); } return (new BaseDependency( _instanceExpression.Body, _initializeInstanceExpressions.Select(i => i.Body), typesMap, _autowiringStrategy) .TryBuildExpression(buildContext, lifetime, out expression, out error)); }
/// <inheritdoc /> public bool TryBuildExpression(IBuildContext buildContext, ILifetime lifetime, out Expression expression, out Exception error) { if (buildContext == null) { throw new ArgumentNullException(nameof(buildContext)); } var typesMap = new Dictionary <Type, Type>(_typesMap); NewExpression newExpression; try { var autoWiringStrategy = _autowiringStrategy ?? buildContext.AutowiringStrategy; var instanceType = ResolveInstanceType(buildContext, autoWiringStrategy); var typeDescriptor = CreateTypeDescriptor(buildContext, instanceType, typesMap); var ctor = SelectConstructor(buildContext, typeDescriptor, autoWiringStrategy); newExpression = Expression.New(ctor.Info, ctor.GetParametersExpressions(buildContext)); } catch (BuildExpressionException ex) { error = ex; expression = default(Expression); return(false); } return (new BaseDependency( newExpression, _initializeInstanceExpressions.Select(i => i.Body), typesMap, _autowiringStrategy) .TryBuildExpression(buildContext, lifetime, out expression, out error)); }
public override void InvokeDisposed(ILifetime lifetime) { var lifetimeInstance = (T)lifetime; lifetimeList.Remove(lifetimeInstance); for (int i = 0; i < perceivers.Count; i++) { perceivers[i].OnDisposed(lifetimeInstance); } disposedDelegate?.DynamicInvoke(lifetime); if (observers.TryGetValue(lifetime, out var list)) { for (int i = 0; i < list.Count; i++) { list[i].OnDisposed(lifetimeInstance); } } if (disposedInstancesDelegate.TryGetValue(lifetime, out var @delegate)) { @delegate?.DynamicInvoke(lifetime); } #if UNITY_EDITOR if (childsInvoking == null) { Debug.LogError($"Invocation tree for {lifetime} is not ready", lifetime as UnityEngine.Object); } #endif for (int i = 0; i < childsInvoking.Count; i++) { childsInvoking[i].InvokeDisposedSilent(lifetime); childsInvoking[i].disposedDelegate?.DynamicInvoke(lifetime); } var parentTree = parent; while (parentTree != null) { parentTree.InvokeDisposedSilent(lifetime); parentTree.disposedDelegate?.DynamicInvoke(lifetime); parentTree = parentTree.parent; } for (int i = 0; i < interfaces.Count; i++) { interfaces[i].InvokeDisposedSilent(lifetime); interfaces[i].disposedDelegate?.DynamicInvoke(lifetime); } for (int i = 0; i < autoPerceive.Count; i++) { autoPerceive[i].RemovePerceiver(lifetime); } for (int i = 0; i < observableInstancesTrees.Count; i++) { observableInstancesTrees[i].RemoveObserver(lifetime); } }
public override void Dispose() { if (_lifetime != null) { _lifetime.Dispose(); _lifetime = null; } }
private static string GetLifetimeName(ILifetime lifetime) { if (lifetime == null) { return("Transient"); } return(lifetime.GetType().Name); }
public AdapterLifetime(ILifetime lifetime) { if (lifetime == null) { throw new ArgumentNullException("lifetime"); } this.lifetime = lifetime; }
public ExternTestTool(string apiKey, IResponseCache cache, ILifetime lifetime, ILog log) { this.apiKey = apiKey; this.cache = cache; this.lifetime = lifetime; httpClient = new HttpClientImplementation("https://extern-api.testkontur.ru/test-tools/v1/", apiKey, lifetime, log); driveCertificatesReader = new DriveCertificatesReader(lifetime); }
/// <summary> /// Constructs a <see cref="ServiceRegistration"/> /// </summary> /// <param name="lifetimeManager">The lifetime manager.</param> /// <param name="objectBuilder">THe object builder.</param> /// <param name="attributeConditions">The attribute conditions.</param> /// <param name="targetTypeCondition">The target type condition.</param> /// <param name="resolutionCondition">The resolution condition.</param> public ServiceRegistration(ILifetime lifetimeManager, IObjectBuilder objectBuilder, HashSet<Type> attributeConditions = null, Type targetTypeCondition = null, Func<TypeInformation, bool> resolutionCondition = null) { this.lifetimeManager = lifetimeManager; this.objectBuilder = objectBuilder; this.attributeConditions = attributeConditions; this.targetTypeCondition = targetTypeCondition; this.resolutionCondition = resolutionCondition; }
public LifetimeController( ILogger <LifetimeController> logger, ILifetime lifetime1, ILifetime lifetime2) { this._logger = logger; this._lifetime1 = lifetime1; this._lifetime2 = lifetime2; }
public HttpClientImplementation(string baseUrl, string apiKey, ILifetime lifetime, ILog log) { this.log = log; httpClient = lifetime.Add(new HttpClient { BaseAddress = new Uri(baseUrl, UriKind.Absolute) }); httpClient.DefaultRequestHeaders.Add("X-Kontur-Apikey", apiKey); }
private static bool LifetimeHandler(YangStatement statement, ILifetime partial) { if (statement.Keyword != "status") { return(false); } partial.Status = statement.Argument; return(true); }
public override void InvokeDestroyed(ILifetime lifetime) { if (lifetime.IsLifetimeInitialized) { throw new Exception(); } observers.Remove(lifetime); initializedInstancesDelegate.Remove(lifetime); disposedInstancesDelegate.Remove(lifetime); }
protected LifetimeRegistrationBase(ILifetime lifetime, object key = null) : base(key) { if (lifetime == null) { throw new ArgumentNullException("lifetime"); } Lifetime = lifetime; }
public IExpressionRegistration Register <TType, TImpl>(object key = null, ILifetime lifetime = null, CompileMode?compileMode = null, bool includeInternalCtor = false, Func <ConstructorInfo[], ConstructorInfo> selector = null) where TType : class where TImpl : class, TType { // Why create a Func<IResolver, T> Expression here when it is always turned into a Func<IResolver, object> later // Only to keep the constraints and work when calling Register<T> ? var exp = ExpressionHelper.CreateExpression <TType, TImpl>(includeInternalCtor: includeInternalCtor, selector: selector); return(Register <TType>(exp, key, lifetime, compileMode)); }
public Binding([NotNull] IBinding <T> binding, [NotNull] ILifetime lifetime) { if (binding == null) { throw new ArgumentNullException(nameof(binding)); } Container = binding.Container; Types = binding.Types; Lifetime = lifetime ?? throw new ArgumentNullException(nameof(lifetime)); Tags = binding.Tags; }
public Page( [NotNull] string name, [NotNull] ILifetime lifetime, [NotNull] IFactory <string, ICell> cellFactory) { Console.Out.WriteLine($"Page '{name}' created"); _name = name; _cells = Enumerable.Range(0, 5) .Select(n => cellFactory.Create(lifetime, $"Cell #{n} from page '{name}'")) .ToArray(); }
public override void RemoveObserver(ILifetime instance, object observer, bool forceCachedEvents) { if (observers.TryGetValue(instance, out var list)) { var lifetimeObserver = (ILifetimeObserver <T>)observer; if (list.RemoveSwapBack(lifetimeObserver) && forceCachedEvents && instance.IsLifetimeInitialized) { lifetimeObserver.OnDisposed((T)instance); } } }
private LightInject.ILifetime AdaptLifetime(ILifetime lifetime) { if (lifetime == null) { return(new LightInject.PerContainerLifetime()); } else { return(new AdapterLifetime(lifetime)); } }
private static ILifetime CreateLifeTimeBasedOnExistingServiceRegistration(ServiceRegistration serviceRegistration) { ILifetime lifeTime = null; if (serviceRegistration.Lifetime != null) { lifeTime = (ILifetime)Activator.CreateInstance(serviceRegistration.Lifetime.GetType()); } return(lifeTime); }
/// <inheritdoc /> public bool TryGetDependency(FullKey key, out IDependency dependency, out ILifetime lifetime) { if (!TryGetDependency(key, key.GetHashCode(), out var dependencyEntry)) { return(_parent.TryGetDependency(key, out dependency, out lifetime)); } dependency = dependencyEntry.Dependency; lifetime = dependencyEntry.GetLifetime(key.Type); return(true); }
public RegistrationEntry(Type type, string name, ILifetime lifetime, IFactory factory) { if (name == null) { throw new ArgumentNullException("name"); } this.type = type; this.name = name; this.lifetime = lifetime; this.factory = factory; }
public ExpressionRegistration(Type type, Expression<Func<IResolver, object>> expression, ILifetime lifetime, object key = null, CompileMode compileMode = CompileMode.Dynamic) { if (type == null) throw new ArgumentNullException("type"); if (expression == null) throw new ArgumentNullException("expression"); _type = type; _expression = expression; SetFactory(expression); SetLifetime(lifetime); _key = key; _compileMode = compileMode; }
public RegistrationEntry(Type type, string name, ILifetime lifetime) : this(type, name, lifetime, new DefaultFactory(type)) { }
public RegistrationEntry(Type type, ILifetime lifetime) : this(type, string.Empty, lifetime) { }
public InstanceBuilder(IInstanceFactory instanceFactory) { _instanceFactory = instanceFactory; _interceptors = new List<IInterceptor>(); _lifetime = TransientLifetime.Instance; }
public void SetLifetime(ILifetime lifetime) { _lifetime = lifetime; }
public IConfigurableRegistration SetLifetime(ILifetime lifetime) { // Currently allows changing lifetime no matter if compiled or not and/or used in other compiled registrations // The change wont be reflected if changed after it have been compiled (and not re-compiled) which is a problem. // Throw an exception ? or just dont care ? or recompile (if already compiled - requires all registrations to be recompiled as they might refer to this registration) if (lifetime == null) throw new ArgumentNullException("lifetime"); // Init new lifetime lifetime.Init(this); // If _lifetime is already set dispose it first ? or call Clear/Reset() if supported ? //if (_lifetime != null) // _lifetime.Dispose(); _lifetime = lifetime; return this; }
/// <summary> /// Creates a new instance of the /// <see cref="RemoteObjectProxyTypeBuilder"/> class. /// </summary> /// <param name="lifetime"> /// The lifetime properties to be applied to the target object. /// </param> public RemoteObjectProxyTypeBuilder(ILifetime lifetime) { this.lifetime = lifetime; Name = "RemoteObjectProxy"; }
/// <summary> /// Create a new instance of the RemoteFactory. /// </summary> public CaoRemoteFactory(ILifetime lifetime, string targetName, string[] interfaces, AbstractObjectFactory objectFactory) { this.targetName = targetName; this.objectFactory = objectFactory; this.remoteObjectFactory = new RemoteObjectFactory(); this.remoteObjectFactory.BaseType = typeof(BaseCao); this.remoteObjectFactory.Interfaces = interfaces; this.remoteObjectFactory.Infinite = lifetime.Infinite; this.remoteObjectFactory.InitialLeaseTime = lifetime.InitialLeaseTime; this.remoteObjectFactory.RenewOnCallTime = lifetime.RenewOnCallTime; this.remoteObjectFactory.SponsorshipTimeout = lifetime.SponsorshipTimeout; }
public void SetLifetime(ILifetime lifetime) { throw new NotSupportedException(); }