/// <summary>
        /// A singleton object cannot depend on a scoped object because it "captures" the scoped object and may access
        /// if after the scope closes and destroys the object.  To prevent this we eliminate the scope for a singleton
        /// evaluation and then put it back in case the singleton is part of a multiple object activation
        /// </summary>
        private IIocService ExchangeRequestScope(IBindingRequest request, IIocService newScope)
        {
            var ret = request.IocService;

            request.IocService = newScope;
            return(ret);
        }
Exemplo n.º 2
0
        public object?Create(IBindingRequest bindingRequest)
        {
            var values = new object[desiredType.GetGenericArguments().Length];
            var scope  = bindingRequest.IocService.CreateScope();

            values[0] = scope;
            scope.Fill(values.AsSpan <object?>()[1..], RequestsForInnerItems(bindingRequest));
        private async Task <T> CreateFromStaticImplementation(object[] parameters, IBindingRequest parentRequest)
        {
            request = parentRequest;
            await EnsureInitialized(parameters, StaticConstructor);

            return(datum !);
        }
Exemplo n.º 4
0
        private static object?RunStaticOrMemberMethod(IBindingRequest request, object?target, string methodName,
                                                      Type targetType, BindingFlags searchCriteria)
        {
            var methods = FindCallableTargetMethod(request, methodName, searchCriteria, targetType);

            return(methods.Invoke(target, ResolveParameters(request, methods)));
        }
        public IActivationStrategy?ApplyResolutionPolicy(IBindingRequest request)
        {
            var targetType = ApplyConvention(request.DesiredType);

            return(targetType == null ? null :
                   TypeActivatorFactory.CreateTypeActivator(targetType, ConstructorSelectors.MaximumArgumentCount));
        }
        private IBindingRequest InnerRequestForCanCreate(IBindingRequest bindingRequest)
        {
            var types = functionDelegateType.GetGenericArguments().ToList();

            return(bindingRequest.CreateSubRequest(types.Last(),
                                                   CreateFakeObjectsFromFunctionParameters(types).ToArray()));
        }
 public AsyncFunctionFactoryStub(IBindingRequest request, Type targetObjectType,
                                 ForwardFuncToMethodCall asyncInitializerFactory)
 {
     this.request                 = request;
     this.targetObjectType        = targetObjectType;
     this.asyncInitializerFactory = asyncInitializerFactory;
 }
Exemplo n.º 8
0
 private void TrySetScopeValue(IBindingRequest request)
 {
     if (IsAChildOfThisScope(request.IocService))
     {
         return;
     }
     request.IocService = this;
 }
Exemplo n.º 9
0
 public ClonedBindingRequest(IBindingRequest rootRequest)
 {
     this.rootRequest = rootRequest;
     IocService       = rootRequest.IocService;
     // copy the array because it gets destroyed
     ArgumentsFromParent = rootRequest.ArgumentsFromParent.ToArray();
     ArgumentsFormChild  = rootRequest.ArgumentsFormChild.ToArray();
 }
Exemplo n.º 10
0
 private static MethodInfo FindCallableTargetMethod(IBindingRequest request, string methodName,
                                                    BindingFlags searchCriteria, Type typeToSearch) =>
 typeToSearch
 .GetMethods(searchCriteria)
 .Where(i => i.Name.Equals(methodName, StringComparison.Ordinal))
 .OrderByDescending(i => i.GetParameters().Length)
 .FirstOrDefault(i => CanSupplyAllParameters(request, i))
 ?? throw new IocException("Could not find a method to call");
Exemplo n.º 11
0
 public object?Intercept(IBindingRequest request, object?source)
 {
     if (!(DestinationTypeFulfillsRequest(request) && source is TSource legalSource))
     {
         return(source);
     }
     return(DoInterception(request, legalSource));
 }
Exemplo n.º 12
0
 public IActivationStrategy?ApplyResolutionPolicy(IBindingRequest request)
 {
     if (!IsClosedEnumerable(request.DesiredType))
     {
         return(null);
     }
     return(new MultipleValueActivator(innerPolicy, request.DesiredType.GetGenericArguments()[0]));
 }
        private object?ComputeSingleValue(IBindingRequest bindingRequest)
        {
            var oldScope = ExchangeRequestScope(bindingRequest, bindingRequest.IocService.GlobalScope());
            var ret      = base.Create(bindingRequest);

            ExchangeRequestScope(bindingRequest, oldScope);
            return(ret);
        }
Exemplo n.º 14
0
 CreateNetworkVariableServer(IIocService ioc, IBindingRequest br)
 {
     return((r, w) =>
     {
         var dict = ioc.Get <BinaryObjectDictionary>();
         return new NetworkVariableServer(ioc.Get <IVariableCache>(),
                                          new BinaryObjectPipeReader(r, dict), new BinaryObjectPipeWriter(w, dict));
     });
 }
Exemplo n.º 15
0
        public object?Intercept(IBindingRequest request, object?source)
        {
            if (IsDisposableItem(source))
            {
                RegisterDisposal(source, request);
            }

            return(source);
        }
 public override object?Create(IBindingRequest bindingRequest)
 {
     // here we needed to copy the array anyway so that multiple invocations get their own set of
     // variables anyway.  We append our vars to the end of the array so any values provided by a
     // factory will get precedence;
     bindingRequest.ArgumentsFormChild =
         bindingRequest.ArgumentsFormChild.Concat(parameters).ToArray();
     return(base.Create(bindingRequest));
 }
 private T FetchDatumIfNeeded(IBindingRequest localRequest)
 {
     if (state != FactoryState.NotCreatedYet)
     {
         return(datum !);
     }
     state = FactoryState.StaticCreationDone;
     return((T)(localRequest.IocService.Get(localRequest) ??
                new InvalidOperationException("Failed to fetch datum")));
 }
        public override object?Create(IBindingRequest bindingRequest)
        {
            if ((!bindingRequest.IocService.ScopeList().OfType <IRegisterDispose>().Any()) ||
                forbidDisposeEvenIfInScope)
            {
                SetDisposalContextToAContextThatWillNeverGetDisposed(bindingRequest);
            }

            return(InnerActivationStrategy.Create(bindingRequest));
        }
Exemplo n.º 19
0
 BasisCall(Func <IBindingRequest, object?> action, IBindingRequest request)
 {
     try
     {
         return(action(request));
     }
     catch (IocException e)
     {
         throw e.CreateElaboratedException();
     }
 }
Exemplo n.º 20
0
        public IActivationStrategy?GetStrategy(IBindingRequest request, CachedResolutionPolicy cache)
        {
            var finalTargetType = genericTemplate.MakeGenericType(request.DesiredType.GetGenericArguments());
            var activator       = cache.Bind(request.DesiredType, true).DoBinding(
                TypeActivatorFactory.CreateTypeActivator(
                    finalTargetType, constructorSelector)
                );

            options?.Invoke(activator);
            return(activator.GetFinalFactory());
        }
Exemplo n.º 21
0
        public static object InterceptFromTypes(IBindingRequest bindingRequest, object item,
                                                IEnumerable <Type> interceptorTypes)
        {
            var interceptors = bindingRequest.IocService.Get(interceptorTypes
                                                             .Select(bindingRequest.CreateSubRequest).ToList())
                               .OfType <IInterceptor>()
                               .ToArray();

            return(ProxyGeneratorSource.CreateInterceptor(bindingRequest.IocService, bindingRequest.DesiredType,
                                                          item, interceptors));
        }
Exemplo n.º 22
0
        public static void AddBindingRequest(VisualElement ve, IBindingRequest req)
        {
            List <IBindingRequest> l = ve.GetProperty(s_BindingRequestObjectVEPropertyName) as List <IBindingRequest>;

            if (l == null)
            {
                l = RequestObjectListPool.Get();
                ve.SetProperty(s_BindingRequestObjectVEPropertyName, l);
            }

            l.Add(req);
            ve.IncrementVersion(VersionChangeType.Bindings);
        }
Exemplo n.º 23
0
 public static object?RecursiveCall(Func <IBindingRequest,
                                          object?> action, IBindingRequest request)
 {
     try
     {
         return(action(request));
     }
     catch (IocException e)
     {
         e.TypeStack.Add($"{RequestedTypeName(request)} -- {ScopeTag(request)}");
         throw;
     }
 }
Exemplo n.º 24
0
        private object FirstElligibleContainer(IIocService service, IBindingRequest bindingRequest)
        {
            IIocService?currentService = service;

            while (currentService != null && !bindingRequest.DesiredType.IsInstanceOfType(currentService))
            {
                currentService = currentService.ParentScope;
            }
            if (currentService == null)
            {
                throw new IocException("No valid service container found");
            }
            return(currentService);
        }
Exemplo n.º 25
0
        public IActivationStrategy?ApplyResolutionPolicy(IBindingRequest request)
        {
            var objects = request.ArgumentsFromParent;

            for (int i = 0; i < objects.Length; i++)
            {
                if (objects[i] is {} ret&& ObjectFillsRequest(ret, request))
                {
                    RemoveArgumentSoNooneElseCanUseIt(objects, i);
                    return(new ConstantActivationStrategy(ret));
                }
            }
            return(null);
        }
Exemplo n.º 26
0
        private static IBindingInfo Resolve(IBindingRequest request)
        {
            ICollection <IBindingInfo> bindings = Bindings[request.Service];

            if (string.IsNullOrWhiteSpace(request.Name))
            {
                if (DefaultBinding.ContainsKey(request.Service))
                {
                    request.Name = DefaultBinding[request.Service];
                }
            }

            return(bindings != null?bindings.FirstOrDefault(binding => ((IBindingMatcher)binding).Matches(request)) : null);
        }
Exemplo n.º 27
0
        public IActivationStrategy?ApplyResolutionPolicy(IBindingRequest request)
        {
            if (!request.DesiredType.IsGenericType)
            {
                return(null);
            }
            var generic = request.DesiredType.GetGenericTypeDefinition();

            if (registrations.TryGetValue(generic, out var activation))
            {
                return(activation.GetStrategy(request, cache));
            }
            return(null);
        }
Exemplo n.º 28
0
        public static void RemoveBindingRequest(VisualElement ve, IBindingRequest req)
        {
            List <IBindingRequest> l = ve.GetProperty(s_BindingRequestObjectVEPropertyName) as List <IBindingRequest>;

            if (l != null)
            {
                req.Release();
                l.Remove(req);
                if (l.Count == 0)
                {
                    RequestObjectListPool.Release(l);
                    ve.SetProperty(s_BindingRequestObjectVEPropertyName, null);
                }
            }
        }
 private void CreateValueExactlyOnceForAllThreads(IBindingRequest bindingRequest)
 {
     //the double check and lock pattern relies on value and valueExists being volitile fields
     if (!valueExists)
     {
         lock (this)
         {
             if (!valueExists)
             {
                 value       = ComputeSingleValue(bindingRequest);
                 valueExists = true;
             }
         }
     }
 }
Exemplo n.º 30
0
 public bool CanGet(IBindingRequest request)
 {
     try
     {
         var activator = TypeResolver.ApplyResolutionPolicy(request);
         if (activator == null)
         {
             return(false);
         }
         return(activator.CanCreate(request));
     }
     catch (Exception)
     {
         // if we run into a constructor we cannot automatically resolve, then we cannot get the type.
         return(false);
     }
 }