Exemplo n.º 1
0
        /// <inheritdoc />
        public IEnumerable <IToken> Apply(IMutableContainer container)
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }
            yield return(container.Register(ctx => new Lazy <TT>(() => ctx.Container.Inject <TT>(ctx.Key.Tag), true), null, AnyTag));

            yield return(container.Register(ctx => ctx.Container.TryInjectValue <TTS>(ctx.Key.Tag), null, AnyTag));

            yield return(container.Register(ctx => new ThreadLocal <TT>(() => ctx.Container.Inject <TT>(ctx.Key.Tag)), null, AnyTag));
        }
Exemplo n.º 2
0
        /// <inheritdoc />
        public IEnumerable <IToken> Apply(IMutableContainer container)
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }
            yield return(container.Register(new[] { typeof(IEnumerable <TT>) }, new EnumerableDependency(), null, new[] { Key.AnyTag }));

            yield return(container.Register(new[] { typeof(TT[]) }, new ArrayDependency(), null, new [] { Key.AnyTag }));

            yield return(container.Register <List <TT>, IList <TT>, ICollection <TT> >(ctx => new List <TT>(ctx.Container.Inject <TT[]>(ctx.Key)), null, new[] { Key.AnyTag }));

            yield return(container.Register <HashSet <TT>, ISet <TT> >(ctx => new HashSet <TT>(ctx.Container.Inject <TT[]>(ctx.Key)), null, new[] { Key.AnyTag }));

            yield return(container.Register <IObservable <TT> >(ctx => new Observable <TT>(ctx.Container.Inject <IEnumerable <TT> >(ctx.Key)), null, new[] { Key.AnyTag }));

#if !NET40
            yield return(container.Register <ReadOnlyCollection <TT>, IReadOnlyList <TT>, IReadOnlyCollection <TT> >(ctx => new ReadOnlyCollection <TT>(ctx.Container.Inject <TT[]>(ctx.Key)), null, new[] { Key.AnyTag }));
#endif
#if NET5_0_OR_GREATER || NETCOREAPP3_0 || NETCOREAPP3_1 || NETSTANDARD2_1
            yield return(container.Register <IAsyncEnumerable <TT> >(ctx => new AsyncEnumeration <TT>(ctx.Container.Inject <IEnumerable <TT> >(ctx.Key)), null, new[] { Key.AnyTag }));
#endif
        }
Exemplo n.º 3
0
        /// <inheritdoc />
        public IEnumerable <IToken> Apply(IMutableContainer container)
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }
            yield return(container.Register(ctx => new Tuple <TT>(ctx.Container.Inject <TT>(ctx.Key.Tag)), null, AnyTag));

            yield return(container.Register(ctx => new Tuple <TT1, TT2>(
                                                ctx.Container.Inject <TT1>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT2>(ctx.Key.Tag)), null, AnyTag));

            yield return(container.Register(ctx => new Tuple <TT1, TT2, TT3>(
                                                ctx.Container.Inject <TT1>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT2>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT3>(ctx.Key.Tag)), null, AnyTag));

            yield return(container.Register(ctx => new Tuple <TT1, TT2, TT3, TT4>(
                                                ctx.Container.Inject <TT1>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT2>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT3>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT4>(ctx.Key.Tag)), null, AnyTag));

            yield return(container.Register(ctx => new Tuple <TT1, TT2, TT3, TT4, TT5>(
                                                ctx.Container.Inject <TT1>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT2>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT3>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT4>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT5>(ctx.Key.Tag)), null, AnyTag));

            yield return(container.Register(ctx => new Tuple <TT1, TT2, TT3, TT4, TT5, TT6>(
                                                ctx.Container.Inject <TT1>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT2>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT3>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT4>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT5>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT6>(ctx.Key.Tag)), null, AnyTag));

            yield return(container.Register(ctx => new Tuple <TT1, TT2, TT3, TT4, TT5, TT6, TT7>(
                                                ctx.Container.Inject <TT1>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT2>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT3>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT4>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT5>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT6>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT7>(ctx.Key.Tag)), null, AnyTag));

            yield return(container.Register(ctx => new Tuple <TT1, TT2, TT3, TT4, TT5, TT6, TT7, TT8>(
                                                ctx.Container.Inject <TT1>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT2>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT3>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT4>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT5>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT6>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT7>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT8>(ctx.Key.Tag)), null, AnyTag));

#if !NET40 && !NET403 && !NET45 && !NET45 && !NET451 && !NET452 && !NET46 && !NET461 && !NET462 && !NETCOREAPP1_0 && !NETCOREAPP1_1 && !NETSTANDARD1_0 && !NETSTANDARD1_1 && !NETSTANDARD1_2 && !NETSTANDARD1_3 && !NETSTANDARD1_4 && !NETSTANDARD1_5 && !NETSTANDARD1_6 && !WINDOWS_UWP
            yield return(container.Register(ctx => CreateTuple(
                                                ctx.Container.Inject <TT1>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT2>(ctx.Key.Tag)), null, AnyTag));

            yield return(container.Register(ctx => CreateTuple(
                                                ctx.Container.Inject <TT1>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT2>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT3>(ctx.Key.Tag)), null, AnyTag));

            yield return(container.Register(ctx => CreateTuple(
                                                ctx.Container.Inject <TT1>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT2>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT3>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT4>(ctx.Key.Tag)), null, AnyTag));

            yield return(container.Bind <(TT1, TT2, TT3, TT4, TT5)>().AnyTag().To(ctx => CreateTuple(
                                                                                      ctx.Container.Inject <TT1>(ctx.Key.Tag),
                                                                                      ctx.Container.Inject <TT2>(ctx.Key.Tag),
                                                                                      ctx.Container.Inject <TT3>(ctx.Key.Tag),
                                                                                      ctx.Container.Inject <TT4>(ctx.Key.Tag),
                                                                                      ctx.Container.Inject <TT5>(ctx.Key.Tag))));

            yield return(container.Register(ctx => CreateTuple(
                                                ctx.Container.Inject <TT1>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT2>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT3>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT4>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT5>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT6>(ctx.Key.Tag)), null, AnyTag));

            yield return(container.Register(ctx => CreateTuple(
                                                ctx.Container.Inject <TT1>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT2>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT3>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT4>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT5>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT6>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT7>(ctx.Key.Tag)), null, AnyTag));

            yield return(container.Register(ctx => CreateTuple(
                                                ctx.Container.Inject <TT1>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT2>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT3>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT4>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT5>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT6>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT7>(ctx.Key.Tag),
                                                ctx.Container.Inject <TT8>(ctx.Key.Tag)), null, AnyTag));
#endif
        }
Exemplo n.º 4
0
        /// <inheritdoc />
        public IEnumerable <IToken> Apply(IMutableContainer container)
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }
            yield return(container.Register(ctx => FoundCyclicDependency.Shared));

            yield return(container.Register(ctx => CannotBuildExpression.Shared));

            yield return(container.Register(ctx => CannotRegister.Shared));

            yield return(container.Register(ctx => CannotResolveConstructor.Shared));

            yield return(container.Register(ctx => CannotResolveType.Shared));

            yield return(container.Register(ctx => CannotResolveGenericTypeArgument.Shared));

            yield return(container.Register(ctx => DefaultAutowiringStrategy.Shared));

            // Lifetimes
            yield return(container.Register <ILifetime>(ctx => new SingletonLifetime(true), null, new object[] { Lifetime.Singleton }));

            yield return(container.Register <ILifetime>(ctx => new ContainerSingletonLifetime(), null, new object[] { Lifetime.ContainerSingleton }));

            yield return(container.Register <ILifetime>(ctx => new ScopeSingletonLifetime(), null, new object[] { Lifetime.ScopeSingleton }));

            yield return(container.Register <ILifetime>(ctx => new ScopeTransientLifetime(), null, new object[] { Lifetime.ScopeTransient }));

            yield return(container.Register <ILifetime>(ctx => new ScopeRootLifetime(), null, new object[] { Lifetime.ScopeRoot }));

            yield return(container.Register <ILifetime>(ctx => new DisposingLifetime(), null, new object[] { Lifetime.Disposing }));

            // Scope
            yield return(container.Register <IScopeToken>(ctx => Scope.Current));

            yield return(container.Register <IScope>(ctx => new Scope(ctx.Container.Inject <ILockObject>(), false)));

            // Current container
            yield return(container.Register <IContainer, IObservable <ContainerEvent> >(ctx => ctx.Container));

            // New child container
            yield return(container.Register <IMutableContainer>(
                             ctx => new Container(
                                 ctx.Args.Length == 1 ? Container.CreateContainerName(ctx.Args[0] as string) : Container.CreateContainerName(string.Empty),
                                 ctx.Container,
                                 ctx.Container.Inject <ILockObject>())));

            yield return(container.Register <Func <IMutableContainer> >(ctx => () => ctx.Container.Inject <IMutableContainer>()));

            yield return(container.Register <Func <string, IMutableContainer> >(ctx => name => ctx.Container.Resolve <IMutableContainer>(name)));

            // Metadata
            yield return(container.Register(ctx => ctx.Container.Inject <IBuildContext>().Parent.Key));

            yield return(container.Register(ctx => ctx.Container.Inject <IBuildContext>().Parent.Key.Tag.AsTag()));

            yield return(container.Register(ctx => ContainerEventToStringConverter.Shared));

            yield return(container.Register(ctx => TypeToStringConverter.Shared));

            // Core features
            yield return(container.Apply <ResolveUnboundFeature>());
        }
 public static IToken Register <T, T1, T2, T3>([NotNull] this IMutableContainer container, [CanBeNull] ILifetime lifetime = null, [CanBeNull] object[] tags = null)
     where T : T1, T2, T3
 => container.Register(new[] { typeof(T1), typeof(T2), typeof(T3) }, new AutowiringDependency(typeof(T)), lifetime, tags);
 public static IToken Register <T, T1, T2>([NotNull] this IMutableContainer container, Expression <Func <Context, T> > factory, [CanBeNull] ILifetime lifetime = null, [CanBeNull] object[] tags = null, [NotNull][ItemNotNull] params Expression <Action <Context <T> > >[] statements)
     where T : T1, T2
 // ReSharper disable once CoVariantArrayConversion
 => container.Register(new[] { typeof(T), typeof(T1), typeof(T2) }, new ExpressionDependency(factory, null, statements), lifetime, tags);
Exemplo n.º 7
0
 public static IToken Register <T>([NotNull] this IMutableContainer container, Expression <Func <Context, T> > factory, [CanBeNull] ILifetime lifetime = null, [CanBeNull] object[] tags = null, [IoC.NotNull][ItemNotNull] params Expression <Action <Context <T> > >[] statements)
 => container.Register(new[] { typeof(T) }, new ExpressionDependency(factory, null, statements), lifetime, tags);