Ejemplo n.º 1
0
        /// <summary>
        /// Adds registrations for <see cref="Collection{T}"/> and <see cref="ReadOnlyCollection{T}"/> to the <paramref name="targets"/> (including
        /// their primary interfaces) so long as none of the types are already registered.
        /// </summary>
        /// <param name="targets"></param>
        public override void Configure(IRootTargetContainer targets)
        {
            if (targets == null)
            {
                throw new ArgumentNullException(nameof(targets));
            }

            if (!targets.GetOption(Options.EnableCollectionInjection.Default))
            {
                return;
            }

            if (targets.Fetch(typeof(Collection <>)) != null ||
                targets.Fetch(typeof(ICollection <>)) != null ||
                targets.Fetch(typeof(ReadOnlyCollection <>)) != null ||
                targets.Fetch(typeof(IReadOnlyCollection <>)) != null)
            {
                return;
            }

            var collectionTarget = Target.ForConstructor(_collectionCtor);

            targets.Register(collectionTarget);
            targets.Register(collectionTarget, typeof(ICollection <>));

            // there's only one constructor for ReadOnlyCollection anyway, so no need to disambiguate
            var roCollectionTarget = Target.ForType(typeof(ReadOnlyCollection <>));

            targets.Register(roCollectionTarget);
            targets.Register(roCollectionTarget, typeof(IReadOnlyCollection <>));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Called to apply this configuration to the passed <paramref name="targets"/> target container.
        /// </summary>
        /// <param name="targets"></param>
        public override void Configure(IRootTargetContainer targets)
        {
            if (targets == null)
            {
                throw new ArgumentNullException(nameof(targets));
            }

            if (!targets.GetOption(EnableAutoFuncInjection.Default))
            {
                return;
            }

            bool enableEnumerables = targets.GetOption(EnableEnumerableInjection.Default);

            targets.TargetRegistered += (object sender, Events.TargetRegisteredEventArgs e) =>
            {
                if (e.Target is AutoFactoryTarget ||
                    (typeof(Delegate).IsAssignableFrom(e.Type) &&
                     e.Type.IsGenericType &&
                     ((e.Type.IsGenericTypeDefinition && FuncTypes.Contains(e.Type)) ||
                      FuncTypes.Contains(e.Type.GetGenericTypeDefinition()))))
                {
                    return;
                }

                IRootTargetContainer root = (IRootTargetContainer)sender;
                var funcType = typeof(Func <>).MakeGenericType(e.Type);
                var existing = root.Fetch(funcType);

                if (existing == null || existing.UseFallback)
                {
                    // you'd think we would bind to the target that was registered, but we don't because
                    // that would prevent auto IEnumerable<delegate_type> from working, and would also prevent
                    // decorators from working.
                    root.Register(new AutoFactoryTarget(funcType, e.Type, Type.EmptyTypes));
                }

                if (enableEnumerables)
                {
                    var enumerableType = typeof(IEnumerable <>).MakeGenericType(e.Type);
                    funcType = typeof(Func <>).MakeGenericType(enumerableType);
                    existing = root.Fetch(funcType);
                    if (existing == null || existing.UseFallback)
                    {
                        root.Register(new AutoFactoryTarget(funcType, enumerableType, Type.EmptyTypes));
                    }
                }
            };
        }
Ejemplo n.º 3
0
 private static void RegisterAutoFactoryInternal(IRootTargetContainer targets, Type delegateType, Type returnType, Type[] parameterTypes)
 {
     // create an unbound AutoFactoryTarget and register it.
     // this will also trigger a projection to be registered from IEnumerable<ReturnType> to IEnumerable<DelegateType>
     // since the newly created & registered target is unbound.
     targets.Register(new AutoFactoryTarget(delegateType, returnType, parameterTypes));
 }
        /// <summary>
        /// Attaches this behaviour to the target container, adding a registration to the <paramref name="targets"/>
        /// for the type <see cref="ResolveContext"/>.
        ///
        /// Note - if the <paramref name="targets"/> already has a registration for <see cref="ResolveContext"/>,
        /// then the behaviour DOES NOT overwrite it.
        /// </summary>
        /// <param name="targets"></param>
        public void Configure(IRootTargetContainer targets)
        {
            var existing = targets.Fetch(typeof(ResolveContext));

            if (existing == null || existing.UseFallback)
            {
                targets.Register(this._target);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Configures the passed <paramref name="targets"/> to enable auto injection of <see cref="List{T}"/>, <see cref="IList{T}"/>
        /// and <see cref="IReadOnlyList{T}"/> by registering a <see cref="Targets.GenericConstructorTarget"/> for
        /// <see cref="List{T}"/> for all three types - so long as none of them already have a registration.
        /// </summary>
        /// <param name="targets"></param>
        public override void Configure(IRootTargetContainer targets)
        {
            if (targets == null)
            {
                throw new ArgumentNullException(nameof(targets));
            }

            if (!targets.GetOption(Options.EnableListInjection.Default))
            {
                return;
            }

            if (targets.Fetch(typeof(List <>)) != null || targets.Fetch(typeof(IList <>)) != null || targets.Fetch(typeof(IReadOnlyList <>)) != null)
            {
                return;
            }

            var target = Target.ForConstructor(_listCtor);

            targets.Register(target);
            targets.Register(target, typeof(IList <>));
            // might be an argument here for a dedication implementation to prevent casting->modification
            targets.Register(target, typeof(IReadOnlyList <>));
        }