Beispiel #1
0
        public IEnumerable <IToken> Apply(IMutableContainer container)
        {
            // Create ASP .NET core feature
            var aspNetCoreFeature = new AspNetCoreFeature();

            // Configure services
            aspNetCoreFeature
            .AddEntityFrameworkInMemoryDatabase()
            .AddDbContext <Db>(options => options.UseInMemoryDatabase("Sample DB"), ServiceLifetime.Singleton);

            // Id generator
            var id         = new Id();
            var generateId = new Func <Id>(() => id = new Id(id));

            yield return(container
                         // Apply ASP.NET core feature
                         .Apply(aspNetCoreFeature)
                         // Add required bindings
                         .Bind <Id>().To(ctx => generateId())
                         .Bind <Person>().To <Person>());
        }
Beispiel #2
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>());
        }