コード例 #1
0
ファイル: FluentBind.cs プロジェクト: sishui198/IoCContainer
 public static IDisposable To <T>([NotNull] this IBinding <T> binding, [CanBeNull] IAutowiringStrategy autoWiringStrategy = null)
 {
     if (binding == null)
     {
         throw new ArgumentNullException(nameof(binding));
     }
     return(new DependencyToken(binding.Container, CreateDependency(binding, new FullAutoWiringDependency(TypeDescriptor <T> .Type, autoWiringStrategy))));
 }
コード例 #2
0
 public ExpressionDependency(
     [NotNull] LambdaExpression instanceExpression,
     [CanBeNull] IAutowiringStrategy autowiringStrategy = default(IAutowiringStrategy),
     [NotNull][ItemNotNull] params LambdaExpression[] initializeInstanceExpressions)
 {
     _instanceExpression            = instanceExpression ?? throw new ArgumentNullException(nameof(instanceExpression));
     _autowiringStrategy            = autowiringStrategy;
     _initializeInstanceExpressions = initializeInstanceExpressions ?? throw new ArgumentNullException(nameof(initializeInstanceExpressions));
 }
コード例 #3
0
 public BaseDependency(
     [NotNull] Expression instanceExpression,
     [NotNull][ItemNotNull] IEnumerable <Expression> initializeInstanceExpressions,
     [NotNull] IDictionary <Type, Type> typesMap,
     [CanBeNull] IAutowiringStrategy autowiringStrategy = null)
 {
     _createInstanceExpression      = instanceExpression ?? throw new ArgumentNullException(nameof(instanceExpression));
     _initializeInstanceExpressions = initializeInstanceExpressions ?? throw new ArgumentNullException(nameof(initializeInstanceExpressions));
     _typesMap           = typesMap ?? throw new ArgumentNullException(nameof(typesMap));
     _autowiringStrategy = autowiringStrategy;
 }
コード例 #4
0
 private AspectOrientedMetadata(
     [NotNull] IDictionary <Type, Func <Attribute, Type> > typeSelectors,
     [NotNull] IDictionary <Type, Func <Attribute, IComparable> > orderSelectors,
     [NotNull] IDictionary <Type, Func <Attribute, object> > tagSelectors)
 {
     _lockObject         = new LockObject();
     _typeSelectors      = typeSelectors;
     _orderSelectors     = orderSelectors;
     _tagSelectors       = tagSelectors;
     _autowiringStrategy = default(IAutowiringStrategy);
 }
コード例 #5
0
ファイル: Binding.cs プロジェクト: cig-dev/IoCContainer
 public Binding([NotNull] IBinding <T> binding, [NotNull] IAutowiringStrategy autowiringStrategy)
 {
     if (binding == null)
     {
         throw new ArgumentNullException(nameof(binding));
     }
     Container          = binding.Container;
     Tokens             = binding.Tokens;
     Types              = binding.Types;
     Lifetime           = binding.Lifetime;
     Tags               = binding.Tags;
     AutowiringStrategy = autowiringStrategy;
 }
コード例 #6
0
        private Type ResolveInstanceType(IBuildContext buildContext, IAutowiringStrategy autoWiringStrategy)
        {
            if (autoWiringStrategy.TryResolveType(buildContext.Container, _implementationType, buildContext.Key.Type, out var instanceType))
            {
                return(instanceType);
            }

            if (_hasGenericParamsWithConstraints)
            {
                return(GetInstanceTypeBasedOnTargetGenericConstrains(buildContext.Key.Type) ?? buildContext.Container.Resolve <ICannotResolveType>().Resolve(buildContext, _implementationType, buildContext.Key.Type));
            }

            return(_implementationType);
        }
コード例 #7
0
 internal BuildContext(
     Key key,
     [NotNull] IContainer resolvingContainer,
     [NotNull] ICollection <IDisposable> resources,
     [NotNull] ICollection <IBuilder> builders,
     [NotNull] IAutowiringStrategy defaultAutowiringStrategy,
     int depth = 0)
 {
     Key                = key;
     Container          = resolvingContainer ?? throw new ArgumentNullException(nameof(resolvingContainer));
     _resources         = resources ?? throw new ArgumentNullException(nameof(resources));
     _builders          = builders ?? throw new ArgumentNullException(nameof(builders));
     AutowiringStrategy = defaultAutowiringStrategy ?? throw new ArgumentNullException(nameof(defaultAutowiringStrategy));
     Depth              = depth;
 }
コード例 #8
0
        public bool TryCreateResolver(
            Key key,
            [NotNull] IContainer resolvingContainer,
            [NotNull] ICollection <IBuilder> builders,
            [NotNull] IAutowiringStrategy defaultAutowiringStrategy,
            out Delegate resolver,
            out Exception error)
        {
            var typeDescriptor = key.Type.Descriptor();
            var buildContext   = new BuildContext(key, resolvingContainer, _resources, builders, defaultAutowiringStrategy);

            if (!Dependency.TryBuildExpression(buildContext, GetLifetime(typeDescriptor), out var expression, out error))
            {
                resolver = default(Delegate);
                return(false);
            }

            var resolverExpression = Expression.Lambda(buildContext.Key.Type.ToResolverType(), expression, false, WellknownExpressions.ResolverParameters);

            resolver = ExpressionCompiler.Shared.Compile(resolverExpression);
            error    = default(Exception);
            return(true);
        }
コード例 #9
0
 internal BuildContext(
     [CanBeNull] BuildContext parent,
     Key key,
     [NotNull] IContainer resolvingContainer,
     [NotNull] IEnumerable <ICompiler> compilers,
     [NotNull] IEnumerable <IBuilder> builders,
     [NotNull] IAutowiringStrategy defaultAutowiringStrategy,
     [NotNull] ParameterExpression argsParameter,
     [NotNull] ParameterExpression containerParameter,
     [NotNull] IObserver <ContainerEvent> eventObserver,
     int depth = 0)
 {
     Parent             = parent;
     Key                = key;
     Container          = resolvingContainer ?? throw new ArgumentNullException(nameof(resolvingContainer));
     Compilers          = compilers ?? throw new ArgumentNullException(nameof(compilers));
     _builders          = builders ?? throw new ArgumentNullException(nameof(builders));
     _eventObserver     = eventObserver ?? throw new ArgumentNullException(nameof(eventObserver));
     AutowiringStrategy = defaultAutowiringStrategy ?? throw new ArgumentNullException(nameof(defaultAutowiringStrategy));
     ArgsParameter      = argsParameter ?? throw new ArgumentNullException(nameof(argsParameter));
     ContainerParameter = containerParameter ?? throw new ArgumentNullException(nameof(containerParameter));
     Depth              = depth;
     _typesMap          = parent == null ? new Dictionary <Type, Type>() : new Dictionary <Type, Type>(parent._typesMap);
 }
コード例 #10
0
        private static IEnumerable <IMethod <MethodInfo> > GetInitializers(IContainer container, IAutowiringStrategy autoWiringStrategy, TypeDescriptor typeDescriptor)
        {
            var methods = typeDescriptor.GetDeclaredMethods().Select(info => new Method <MethodInfo>(info));

            if (autoWiringStrategy.TryResolveInitializers(container, methods, out var initializers))
            {
                return(initializers);
            }

            if (DefaultAutowiringStrategy.Shared == autoWiringStrategy || !DefaultAutowiringStrategy.Shared.TryResolveInitializers(container, methods, out initializers))
            {
                initializers = Enumerable.Empty <IMethod <MethodInfo> >();
            }

            return(initializers);
        }
コード例 #11
0
        /// <summary>
        /// Creates an instance of dependency.
        /// </summary>
        /// <param name="implementationType">The autowiring implementation type.</param>
        /// <param name="autowiringStrategy">The autowiring strategy.</param>
        /// <param name="initializeInstanceLambdaStatements">The statements to initialize an instance.</param>
        public AutowiringDependency(
            [NotNull] Type implementationType,
            [CanBeNull] IAutowiringStrategy autowiringStrategy = null,
            [NotNull][ItemNotNull] params LambdaExpression[] initializeInstanceLambdaStatements)
        {
            _implementationType            = implementationType ?? throw new ArgumentNullException(nameof(implementationType));
            _autowiringStrategy            = autowiringStrategy;
            _initializeInstanceExpressions = initializeInstanceLambdaStatements ?? throw new ArgumentNullException(nameof(initializeInstanceLambdaStatements));
            _typeDescriptor = implementationType.Descriptor();

            if (_typeDescriptor.IsInterface())
            {
                throw new ArgumentException($"Type \"{implementationType}\" should not be an interface.", nameof(implementationType));
            }

            if (_typeDescriptor.IsAbstract())
            {
                throw new ArgumentException($"Type \"{implementationType}\" should not be an abstract class.", nameof(implementationType));
            }

            if (!_typeDescriptor.IsGenericTypeDefinition())
            {
                return;
            }

            _genericTypeParameters = _typeDescriptor.GetGenericTypeParameters();
            if (_genericTypeParameters.Length > GenericTypeArguments.Arguments.Length)
            {
                throw new ArgumentException($"Too many generic type parameters in the type \"{implementationType}\".", nameof(implementationType));
            }

            _genericParamsWithConstraints = new List <GenericParamsWithConstraints>(_genericTypeParameters.Length);
            var genericTypePos = 0;

            for (var position = 0; position < _genericTypeParameters.Length; position++)
            {
                var genericType = _genericTypeParameters[position];
                if (!genericType.IsGenericParameter)
                {
                    continue;
                }

                var descriptor = genericType.Descriptor();
                if (descriptor.GetGenericParameterAttributes() == GenericParameterAttributes.None && !descriptor.GetGenericParameterConstraints().Any())
                {
                    if (!_typesMap.TryGetValue(genericType, out var curType))
                    {
                        try
                        {
                            curType = GenericTypeArguments.Arguments[genericTypePos++];
                            _typesMap[genericType] = curType;
                        }
                        catch (IndexOutOfRangeException ex)
                        {
                            throw new BuildExpressionException("Too many generic arguments.", ex);
                        }
                    }

                    _genericTypeParameters[position] = curType;
                }
                else
                {
                    _genericParamsWithConstraints.Add(new GenericParamsWithConstraints(descriptor, position));
                }
            }

            if (_genericParamsWithConstraints.Count == 0)
            {
                _implementationType = _typeDescriptor.MakeGenericType(_genericTypeParameters);
            }
            else
            {
                _hasGenericParamsWithConstraints = true;
            }
        }
コード例 #12
0
        private static IMethod <ConstructorInfo> SelectConstructor(IBuildContext buildContext, TypeDescriptor typeDescriptor, IAutowiringStrategy autoWiringStrategy)
        {
            var constructors = (IEnumerable <IMethod <ConstructorInfo> >)typeDescriptor
                               .GetDeclaredConstructors()
                               .Where(method => !method.IsStatic && (method.IsAssembly || method.IsPublic))
                               .Select(info => new Method <ConstructorInfo>(info));

            if (autoWiringStrategy.TryResolveConstructor(buildContext.Container, constructors, out var ctor))
            {
                return(ctor);
            }

            if (DefaultAutowiringStrategy.Shared != autoWiringStrategy && DefaultAutowiringStrategy.Shared.TryResolveConstructor(buildContext.Container, constructors, out ctor))
            {
                return(ctor);
            }

            return(buildContext.Container.Resolve <ICannotResolveConstructor>().Resolve(buildContext, constructors));
        }
コード例 #13
0
 /// <summary>
 /// Specify a type selector for an aspect oriented autowiring strategy.
 /// </summary>
 /// <typeparam name="TTypeAttribute">The type metadata attribute.</typeparam>
 /// <param name="strategy">The base aspect oriented autowiring strategy.</param>
 /// <param name="typeSelector">The type selector.</param>
 /// <returns>The instance of aspect oriented autowiring strategy.</returns>
 public static IAutowiringStrategy Type <TTypeAttribute>(this IAutowiringStrategy strategy, [NotNull] Func <TTypeAttribute, Type> typeSelector)
     where TTypeAttribute : Attribute =>
 AspectOrientedMetadata.Type(
コード例 #14
0
 public CustomAutowiringStrategy(IContainer container, IAutowiringStrategy baseStrategy) =>
コード例 #15
0
        public FullAutoWiringDependency([NotNull] Type type, [CanBeNull] IAutowiringStrategy autoWiringStrategy = null)
        {
            _type = type ?? throw new ArgumentNullException(nameof(type));
            _autoWiringStrategy       = autoWiringStrategy;
            _registeredTypeDescriptor = type.Descriptor();
            if (_registeredTypeDescriptor.IsInterface())
            {
                throw new ArgumentException($"Type \"{type}\" should not be an interface.", nameof(type));
            }

            if (_registeredTypeDescriptor.IsAbstract())
            {
                throw new ArgumentException($"Type \"{type}\" should not be an abstract class.", nameof(type));
            }

            if (!_registeredTypeDescriptor.IsGenericTypeDefinition())
            {
                return;
            }

            _registeredGenericTypeParameters = _registeredTypeDescriptor.GetGenericTypeParameters();
            var genericTypePos = 0;
            var typesMap       = new Dictionary <Type, Type>();

            for (var position = 0; position < _registeredGenericTypeParameters.Length; position++)
            {
                var genericType = _registeredGenericTypeParameters[position];
                if (!genericType.IsGenericParameter)
                {
                    continue;
                }

                var descriptor = genericType.Descriptor();
                if (!descriptor.GetGenericParameterConstraints().Any())
                {
                    if (!typesMap.TryGetValue(genericType, out var curType))
                    {
                        try
                        {
                            curType = GenericTypeArguments[genericTypePos++];
                            typesMap[genericType] = curType;
                        }
                        catch (IndexOutOfRangeException ex)
                        {
                            throw new BuildExpressionException("Too many generic arguments.", ex);
                        }
                    }

                    _registeredGenericTypeParameters[position] = curType;
                }
                else
                {
                    _genericParamsWithConstraints[position] = descriptor;
                }
            }

            if (_genericParamsWithConstraints.Count == 0)
            {
                _type = _registeredTypeDescriptor.MakeGenericType(_registeredGenericTypeParameters);
            }
            else
            {
                _hasGenericParamsWithConstraints = true;
            }
        }
コード例 #16
0
 public static IBinding <T> Autowiring <T>([NotNull] this IBinding <T> binding, [NotNull] IAutowiringStrategy autowiringStrategy)
 {
     if (binding == null)
     {
         throw new ArgumentNullException(nameof(binding));
     }
     if (autowiringStrategy == null)
     {
         throw new ArgumentNullException(nameof(autowiringStrategy));
     }
     return(new Binding <T>(binding, autowiringStrategy));
 }