/// <summary>
        /// Create a new delegate for a given type
        /// </summary>
        /// <param name="scope">scope to use for creating</param>
        /// <param name="locateType">type to inject</param>
        /// <param name="request"></param>
        /// <param name="objectParameter"></param>
        /// <returns></returns>
        public IActivationExpressionResult CreateInjectionDelegate(IInjectionScope scope, Type locateType, IActivationExpressionRequest request, ParameterExpression objectParameter)
        {
            var strategy = new InjectionStrategy(locateType, scope);

            var result = scope.StrategyCompiler.CreateNewResult(request);

            var instanceValue = Expression.Variable(locateType);

            result.AddExtraParameter(instanceValue);
            result.AddExtraExpression(Expression.Assign(instanceValue, Expression.Convert(objectParameter, locateType)));

            var injectPropertyExpressions = CreatePropertyInjectionExpressions(scope, locateType, instanceValue, strategy, request);

            var injectMethodExpressions = CreateMethodInjectionExpressions(scope, locateType, instanceValue, strategy, request);

            foreach (var activationExpressionResult in injectPropertyExpressions)
            {
                result.AddExpressionResult(activationExpressionResult);
            }

            foreach (var activationExpressionResult in injectMethodExpressions)
            {
                result.AddExpressionResult(activationExpressionResult);
            }

            return(result);
        }
        private GameObject InjectDependencies([NotNull] GameObject obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            InjectionStrategy.InjectDependencies <MonoBehaviour>(InjecteeLocator <MonoBehaviour> .Create(obj), ResolverService);

            return(obj);
        }
        public GameObject Create([NotNull] GameObject prefab, Vector3 position, Quaternion rotation)
        {
            if (prefab == null)
            {
                throw new ArgumentNullException(nameof(prefab));
            }

            GameObject obj = GameObject.Instantiate(prefab, position, rotation) as GameObject;

            //Decorate the current resolver with one that uses the contextual services
            IResolver resolver = new ContextualDependencyResolverDecorator(ResolverService, ServiceMap);

            //Inject using the decorated resolver
            InjectionStrategy.InjectDependencies <MonoBehaviour>(InjecteeLocator <MonoBehaviour> .Create(obj), resolver);

            return(obj);
        }
        public TComponentType AddTo <TComponentType>([NotNull] GameObject gameObject)
            where TComponentType : MonoBehaviour
        {
            if (gameObject == null)
            {
                throw new ArgumentNullException(nameof(gameObject));
            }

            TComponentType component = gameObject.AddComponent <TComponentType>();

            //Decorate the current resolver with one that uses the contextual services
            IResolver resolver = new ContextualDependencyResolverDecorator(this.ResolverService, ServiceMap);

            //Inject using the decorated resolver
            InjectionStrategy.InjectDependencies <MonoBehaviour>(component, resolver);

            return(component);
        }
 internal void SetInjectionStrategy(Type type, InjectionStrategy strategy)
 {
     injectionStrategyMap[type] = strategy;
 }
示例#6
0
 /// <summary>
 ///     Sets the injection strategy for the aggregation function factory
 /// </summary>
 /// <param name="strategy">strategy</param>
 /// <returns>itself</returns>
 public AggregationFunctionModeManaged SetInjectionStrategyAggregationFunctionFactory(InjectionStrategy strategy)
 {
     InjectionStrategyAggregationFunctionFactory = strategy;
     return this;
 }
 public AggregationMultiFunctionAgentModeManaged(InjectionStrategy injectionStrategyAggregationAgentFactory)
 {
     _injectionStrategyAggregationAgentFactory = injectionStrategyAggregationAgentFactory;
 }
示例#8
0
 /// <summary>
 ///     Sets the injection strategy for the aggregation function factory
 /// </summary>
 /// <param name="strategy">strategy</param>
 /// <returns>itself</returns>
 public AggregationFunctionModeMultiParam SetInjectionStrategyAggregationFunctionFactory(
     InjectionStrategy strategy)
 {
     InjectionStrategyAggregationFunctionFactory = strategy;
     return this;
 }
 public AggregationMultiFunctionAggregationMethodModeManaged(InjectionStrategy injectionStrategyAggregationMethodFactory)
 {
     this._injectionStrategyAggregationMethodFactory = injectionStrategyAggregationMethodFactory;
 }
 public AggregationMultiFunctionStateModeManaged(InjectionStrategy injectionStrategyAggregationStateFactory)
 {
     InjectionStrategyAggregationStateFactory = injectionStrategyAggregationStateFactory;
 }
        /// <summary>
        ///
        /// </summary>
        /// <param name="scope"></param>
        /// <param name="locateType"></param>
        /// <param name="instanceValue"></param>
        /// <param name="strategy"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        protected virtual IEnumerable <IActivationExpressionResult> CreatePropertyInjectionExpressions(IInjectionScope scope, Type locateType, ParameterExpression instanceValue, InjectionStrategy strategy, IActivationExpressionRequest request)
        {
            var properties = new Dictionary <string, bool>();

            foreach (var property in locateType.GetRuntimeProperties())
            {
                if (!property.CanWrite || !property.SetMethod.IsPublic || property.SetMethod.IsStatic)
                {
                    continue;
                }

                var attribute = property.GetCustomAttributes(true).FirstOrDefault(a => a is IImportAttribute) as IImportAttribute;

                var importInfo = attribute?.ProvideImportInfo(property.PropertyType, property.Name);

                if (importInfo == null)
                {
                    continue;
                }

                yield return(CreatePropertyImportStatement(scope, locateType, instanceValue, strategy, request, property, importInfo));

                properties[property.Name] = true;
            }

            var currentScope = scope;

            while (currentScope != null)
            {
                foreach (var selector in currentScope.MemberInjectionSelectors)
                {
                    foreach (var propertyMember in selector.GetPropertiesAndFields(locateType, scope, request))
                    {
                        if (properties.ContainsKey(propertyMember.MemberInfo.Name))
                        {
                            continue;
                        }

                        properties[propertyMember.MemberInfo.Name] = true;
                    }
                }

                currentScope = currentScope.Parent as IInjectionScope;
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="scope"></param>
        /// <param name="locateType"></param>
        /// <param name="instanceValue"></param>
        /// <param name="strategy"></param>
        /// <param name="request"></param>
        /// <param name="method"></param>
        /// <param name="importInfo"></param>
        /// <returns></returns>
        protected virtual IActivationExpressionResult CreateMethodInjectionExpression(IInjectionScope scope, Type locateType, ParameterExpression instanceValue, InjectionStrategy strategy, IActivationExpressionRequest request, MethodInfo method, ImportAttributeInfo importInfo)
        {
            var expressionResult = scope.StrategyCompiler.CreateNewResult(request);

            var list = new List <IActivationExpressionResult>();

            foreach (var parameter in method.GetParameters())
            {
                var parameterRequest = request.NewRequest(parameter.ParameterType, strategy, locateType,
                                                          RequestType.MethodParameter, parameter, false, true);

                if (scope.ScopeConfiguration.Behaviors.KeyedTypeSelector(parameter.ParameterType))
                {
                    parameterRequest.SetLocateKey(parameter.Name);
                }

                var result = request.Services.ExpressionBuilder.GetActivationExpression(scope, parameterRequest);

                list.Add(result);

                expressionResult.AddExpressionResult(result);
            }

            var callExpression = Expression.Call(instanceValue, method, list.Select(result => result.Expression));

            expressionResult.AddExtraExpression(callExpression);

            return(expressionResult);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="scope"></param>
        /// <param name="locateType"></param>
        /// <param name="instanceValue"></param>
        /// <param name="strategy"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        protected virtual IEnumerable <IActivationExpressionResult> CreateMethodInjectionExpressions(IInjectionScope scope, Type locateType, ParameterExpression instanceValue, InjectionStrategy strategy, IActivationExpressionRequest request)
        {
            foreach (var method in locateType.GetRuntimeMethods())
            {
                if (!method.IsPublic || method.IsStatic)
                {
                    continue;
                }

                var importAttribute = method.GetCustomAttributes(true).FirstOrDefault(a => a is IImportAttribute) as IImportAttribute;

                var importInfo = importAttribute?.ProvideImportInfo(null, method.Name);

                if (importInfo != null)
                {
                    yield return
                        (CreateMethodInjectionExpression(scope, locateType, instanceValue, strategy, request, method, importInfo));
                }
            }
        }
        private static IActivationExpressionResult CreatePropertyImportStatement(IInjectionScope scope, Type locateType,
                                                                                 ParameterExpression instanceValue, InjectionStrategy strategy, IActivationExpressionRequest request,
                                                                                 PropertyInfo property, ImportAttributeInfo importInfo)
        {
            var propertyRequest = request.NewRequest(property.PropertyType, strategy, locateType,
                                                     RequestType.Member, property, false, true);

            propertyRequest.SetIsRequired(importInfo.IsRequired);
            propertyRequest.SetEnumerableComparer(importInfo.Comparer);

            if (importInfo.ImportKey != null)
            {
                propertyRequest.SetLocateKey(importInfo.ImportKey);
            }
            else if (scope.ScopeConfiguration.Behaviors.KeyedTypeSelector(property.PropertyType))
            {
                propertyRequest.SetLocateKey(property.Name);
            }

            var result = request.Services.ExpressionBuilder.GetActivationExpression(scope, propertyRequest);

            var setExpression = Expression.Assign(Expression.Property(instanceValue, property.SetMethod),
                                                  result.Expression);

            result.AddExtraExpression(setExpression);
            result.Expression = null;

            return(result);
        }
示例#15
0
 public AggregationFunctionModeManaged(InjectionStrategy injectionStrategyAggregationFunctionFactory)
 {
     InjectionStrategyAggregationFunctionFactory = injectionStrategyAggregationFunctionFactory;
 }
 public AggregationMultiFunctionAccessorModeManaged(InjectionStrategy injectionStrategyAggregationAccessorFactory) {
     this._injectionStrategyAggregationAccessorFactory = injectionStrategyAggregationAccessorFactory;
 }