Ejemplo n.º 1
0
        public Expression GetExpression(
            IResolutionStrategy resolutionStrategy,
            TypeInformation typeInfo,
            ResolutionContext resolutionContext)
        {
            var args             = typeInfo.Type.GetGenericArguments();
            var tupleConstructor = typeInfo.Type.GetConstructor(args);
            var length           = args.Length;
            var expressions      = new Expression[length];

            for (var i = 0; i < length; i++)
            {
                var argumentInfo = typeInfo.CloneForType(args[i]);
                var expr         = resolutionStrategy.BuildExpressionForType(resolutionContext, argumentInfo);

                if (expr != null)
                {
                    expressions[i] = expr;
                }
                else
                {
                    return(null);
                }
            }

            return(tupleConstructor.MakeNew(expressions));
        }
Ejemplo n.º 2
0
        public Expression GetExpression(
            IResolutionStrategy resolutionStrategy,
            TypeInformation typeInfo,
            ResolutionContext resolutionContext)
        {
            var name         = typeInfo.DependencyName;
            var configurator = name != null
                ? context =>
            {
                context.WithName(name);
                resolutionContext.RequestInitiatorContainerContext.ContainerConfiguration.UnknownTypeConfigurator?.Invoke(context);
            }
            : resolutionContext.RequestInitiatorContainerContext.ContainerConfiguration.UnknownTypeConfigurator;

            var registrationConfigurator = new UnknownRegistrationConfigurator(typeInfo.Type, typeInfo.Type);

            configurator?.Invoke(registrationConfigurator);

            if (!registrationConfigurator.TypeMapIsValid(out _))
            {
                return(null);
            }

            var registration = this.registrationBuilder.BuildServiceRegistration(resolutionContext.RequestInitiatorContainerContext,
                                                                                 registrationConfigurator, false);

            this.serviceRegistrator.Register(resolutionContext.RequestInitiatorContainerContext, registration, typeInfo.Type);

            return(resolutionStrategy.BuildExpressionForRegistration(registration, resolutionContext.ShouldFallBackToRequestInitiatorContext
                ? resolutionContext.BeginCrossContainerContext(resolutionContext.RequestInitiatorContainerContext)
                : resolutionContext,
                                                                     typeInfo));
        }
Ejemplo n.º 3
0
 public IEnumerable <Expression> GetExpressionsForEnumerableRequest(
     IResolutionStrategy resolutionStrategy,
     TypeInformation typeInfo,
     ResolutionContext resolutionInfo)
 {
     return(new Expression[] { Expression.Constant(new Test1()) });
 }
Ejemplo n.º 4
0
 internal ContainerContext(IRegistrationRepository registrationRepository, IStashboxContainer container,
                           IResolutionStrategy resolutionStrategy, IContainerConfigurator containerConfigurator, IDecoratorRepository decoratorRepository)
 {
     this.ResolutionStrategy     = resolutionStrategy;
     this.RegistrationRepository = registrationRepository;
     this.Container             = container;
     this.ContainerConfigurator = containerConfigurator;
     this.DecoratorRepository   = decoratorRepository;
 }
Ejemplo n.º 5
0
 public bool TryGet(IResolutionStrategy resolutionStrategy, out object resolution)
 {
     if (_history.ContainsKey(resolutionStrategy))
     {
         resolution = _history[resolutionStrategy];
         return true;
     }
     resolution = null;
     return false;
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Constructs a <see cref="ContainerContext"/>
 /// </summary>
 /// <param name="registrationRepository">The repository of the registrations.</param>
 /// <param name="container">The container itself.</param>
 /// <param name="resolutionStrategy">The resolution strategy.</param>
 /// <param name="metaInfoRepository">The meta information repository.</param>
 /// <param name="delegateRepository">Repository of the compiled delegates.</param>
 internal ContainerContext(IRegistrationRepository registrationRepository, IStashboxContainer container,
     IResolutionStrategy resolutionStrategy, ExtendedImmutableTree<MetaInfoCache> metaInfoRepository,
     ExtendedImmutableTree<Func<ResolutionInfo, object>> delegateRepository)
 {
     this.RegistrationRepository = registrationRepository;
     this.Container = container;
     this.ResolutionStrategy = resolutionStrategy;
     this.MetaInfoRepository = metaInfoRepository;
     this.DelegateRepository = delegateRepository;
     this.Bag = new ConcurrentKeyValueStore<object, object>();
 }
Ejemplo n.º 7
0
        public Expression GetExpression(
            IResolutionStrategy resolutionStrategy,
            TypeInformation typeInfo,
            ResolutionContext resolutionContext)
        {
            var newTypeInfo = typeInfo.CloneForType(typeInfo.Type.GetEnumerableType());
            var expressions = resolutionStrategy.BuildExpressionsForEnumerableRequest(resolutionContext, newTypeInfo);

            return(expressions == null?newTypeInfo.Type.InitNewArray() :
                       newTypeInfo.Type.InitNewArray(expressions));
        }
Ejemplo n.º 8
0
 internal ContainerContext(IRegistrationRepository registrationRepository, IDelegateRepository delegateRepository,
                           IStashboxContainer container, IResolutionStrategy resolutionStrategy, IContainerConfigurator containerConfigurator,
                           IDecoratorRepository decoratorRepository)
 {
     this.ResolutionStrategy     = resolutionStrategy;
     this.RegistrationRepository = registrationRepository;
     this.DelegateRepository     = delegateRepository;
     this.Container             = container;
     this.Bag                   = new ConcurrentKeyValueStore <object, object>();
     this.ContainerConfigurator = containerConfigurator;
     this.DecoratorRepository   = decoratorRepository;
 }
Ejemplo n.º 9
0
        public DnsResolver(
            IOptionsMonitor <ResolverOptions> options,
            IResolutionStrategy resolutionStrategy,
            IDnsMsgBinSerializer dnsSerializer)
        {
            this.options            = options ?? throw new ArgumentNullException(nameof(options));
            this.resolutionStrategy = resolutionStrategy ?? throw new ArgumentNullException(nameof(resolutionStrategy));
            this.dnsSerializer      = dnsSerializer ?? throw new ArgumentNullException(nameof(dnsSerializer));

            this.listenPort = (ushort)options.CurrentValue.ListenPort;
            this.listener   = new UdpListener(ProcessRawQuery, this.listenPort);
        }
Ejemplo n.º 10
0
        public IEnumerable <Expression> GetExpressionsForEnumerableRequest(
            IResolutionStrategy resolutionStrategy,
            TypeInformation typeInfo,
            ResolutionContext resolutionContext)
        {
            var lazyArgumentInfo = typeInfo.CloneForType(typeInfo.Type.GetGenericArguments()[0]);

            var ctorParamType   = Constants.FuncType.MakeGenericType(lazyArgumentInfo.Type);
            var lazyConstructor = typeInfo.Type.GetConstructor(ctorParamType);

            return(resolutionStrategy.BuildExpressionsForEnumerableRequest(resolutionContext, lazyArgumentInfo)
                   ?.Select(e => lazyConstructor.MakeNew(e.AsLambda())));
        }
Ejemplo n.º 11
0
        public Expression GetExpression(IResolutionStrategy resolutionStrategy, TypeInformation typeInfo,
                                        ResolutionContext resolutionContext)
        {
            var args             = typeInfo.Type.GetGenericArguments();
            var wrappedType      = args.LastElement();
            var funcArgumentInfo = typeInfo.CloneForType(wrappedType);

            var parameters = args.SelectButLast(a => a.AsParameter());
            var expression = resolutionStrategy.BuildExpressionForType(resolutionContext
                                                                       .BeginContextWithFunctionParameters(parameters), funcArgumentInfo);

            return(expression?.AsLambda(typeInfo.Type, parameters));
        }
Ejemplo n.º 12
0
        public Expression GetExpression(
            IResolutionStrategy resolutionStrategy,
            TypeInformation typeInfo,
            ResolutionContext resolutionContext)
        {
            var lazyArgumentInfo = typeInfo.CloneForType(typeInfo.Type.GetGenericArguments()[0]);

            var ctorParamType   = Constants.FuncType.MakeGenericType(lazyArgumentInfo.Type);
            var lazyConstructor = typeInfo.Type.GetConstructor(ctorParamType);
            var expression      = this.GetLazyFuncExpression(lazyArgumentInfo, resolutionContext, resolutionStrategy);

            return(expression == null ? null : lazyConstructor.MakeNew(expression.AsLambda()));
        }
Ejemplo n.º 13
0
        public IEnumerable <Expression> GetExpressionsForEnumerableRequest(IResolutionStrategy resolutionStrategy, TypeInformation typeInfo,
                                                                           ResolutionContext resolutionContext)
        {
            var type             = typeInfo.Type;
            var args             = type.GetGenericArguments();
            var wrappedType      = args.LastElement();
            var funcArgumentInfo = typeInfo.CloneForType(wrappedType);

            var parameters = args.SelectButLast(a => a.AsParameter());

            return(resolutionStrategy.BuildExpressionsForEnumerableRequest(resolutionContext
                                                                           .BeginContextWithFunctionParameters(parameters), funcArgumentInfo)?
                   .Select(e => e.AsLambda(type, parameters)));
        }
        private DependencyResolutionOptions SetResolutionStrategy(Type contractType, IResolutionStrategy resolutionStrategy)
        {
            object constructedObject;

            if (this.resolutionStrategies.ContainsKey(contractType))
            {
                if (this.resolutionStrategies[contractType].Equals(resolutionStrategy))
                {
                    return(this.resolutionOptions[contractType]);
                }
            }

            if (this.locallyCreatedDependencies.TryGetValue(contractType, out constructedObject) || this.injectedDependencies.TryGetValue(contractType, out constructedObject))
            {
                this.InvalidateConstructedObjects(constructedObject);
            }

            this.resolutionStrategies[contractType] = resolutionStrategy;
            var options = new DependencyResolutionOptions();

            this.resolutionOptions[contractType] = options;
            return(options);
        }
Ejemplo n.º 15
0
 public IEnumerable <Expression> GetExpressionsForEnumerableRequest(
     IResolutionStrategy resolutionStrategy,
     TypeInformation typeInfo,
     ResolutionContext resolutionContext) =>
 resolutionStrategy.BuildExpressionsForEnumerableRequest(resolutionContext.BeginCrossContainerContext(resolutionContext
                                                                                                      .CurrentContainerContext.ParentContext), typeInfo);
Ejemplo n.º 16
0
 public Expression GetExpression(
     IResolutionStrategy resolutionStrategy,
     TypeInformation typeInfo,
     ResolutionContext resolutionContext) =>
 resolutionStrategy.BuildExpressionForType(resolutionContext.BeginCrossContainerContext(resolutionContext
                                                                                        .CurrentContainerContext.ParentContext), typeInfo);
Ejemplo n.º 17
0
 public Expression GetExpression(IResolutionStrategy resolutionStrategy, TypeInformation typeInfo,
                                 ResolutionContext resolutionContext) => resolutionContext.CurrentScopeParameter;
Ejemplo n.º 18
0
 public Expression GetExpression(
     IResolutionStrategy resolutionStrategy,
     TypeInformation typeInfo,
     ResolutionContext resolutionContext) =>
 typeInfo.Type.AsDefault();
Ejemplo n.º 19
0
 public Expression GetExpression(IResolutionStrategy resolutionStrategy,
                                 TypeInformation typeInfo,
                                 ResolutionContext resolutionInfo)
 {
     return(Expression.Constant(new Test1()));
 }
Ejemplo n.º 20
0
        public static Expression BuildResolutionExpression(this ImmutableBucket <IResolver> resolvers,
                                                           TypeInformation typeInfo, ResolutionContext resolutionContext, IResolutionStrategy resolutionStrategy)
        {
            var length = resolvers.Length;

            for (var i = 0; i < length; i++)
            {
                var item = resolvers[i];
                if (item.CanUseForResolution(typeInfo, resolutionContext))
                {
                    return(item.GetExpression(resolutionStrategy, typeInfo, resolutionContext));
                }
            }

            return(null);
        }
Ejemplo n.º 21
0
 public Expression GetExpression(
     IResolutionStrategy resolutionStrategy,
     TypeInformation typeInfo,
     ResolutionContext resolutionContext) =>
 typeInfo.DefaultValue.AsConstant(typeInfo.Type);
Ejemplo n.º 22
0
 public CheckParentResolutionStrategyDecorator(IResolutionStrategy resolutionStrategy)
 {
     this.resolutionStrategy = resolutionStrategy;
 }
Ejemplo n.º 23
0
 private Expression GetLazyFuncExpression(TypeInformation argumentType,
                                          ResolutionContext resolutionContext, IResolutionStrategy resolutionStrategy) =>
 resolutionContext.CurrentContainerContext.ContainerConfiguration.CircularDependenciesWithLazyEnabled
         ? resolutionContext.CurrentScopeParameter
 .CallMethod(Constants.ResolveMethod, argumentType.Type.AsConstant(),
             argumentType.DependencyName.AsConstant(), false.AsConstant(),
             Constants.ObjectType.InitNewArray(resolutionContext.ParameterExpressions
                                               .SelectMany(x => x.Select(i => i.I2.ConvertTo(Constants.ObjectType)))))
 .ConvertTo(argumentType.Type)
         : resolutionStrategy.BuildExpressionForType(resolutionContext, argumentType);
Ejemplo n.º 24
0
 public void TryPut(IResolutionStrategy resolutionStrategy, object resolution)
 {
     if (resolutionStrategy.Lifestyle == Lifestyle.Singleton) _history[resolutionStrategy] = resolution;
 }
Ejemplo n.º 25
0
        public static IEnumerable <Expression> BuildAllResolutionExpressions(this ImmutableBucket <IResolver> resolvers,
                                                                             TypeInformation typeInfo, ResolutionContext resolutionContext, IResolutionStrategy resolutionStrategy)
        {
            var length = resolvers.Length;

            for (var i = 0; i < length; i++)
            {
                var item = resolvers[i];
                if (item is IEnumerableSupportedResolver resolver && item.CanUseForResolution(typeInfo, resolutionContext))
                {
                    return(resolver.GetExpressionsForEnumerableRequest(resolutionStrategy, typeInfo, resolutionContext));
                }
            }

            return(null);
        }