Exemplo n.º 1
0
        public ServiceFamily Build(Type type, ServiceGraph graph)
        {
            if (!type.IsGenericType)
            {
                return(null);
            }

            var basicType = type.GetGenericTypeDefinition();

            if (graph.HasFamily(basicType))
            {
                var basicFamily             = graph.ResolveFamily(basicType);
                var templatedParameterTypes = type.GetGenericArguments();

                return(basicFamily.CreateTemplatedClone(type, graph.DecoratorPolicies, templatedParameterTypes.ToArray()));
            }

            try
            {
                return(tryToConnect(type, graph));
            }
            catch (Exception)
            {
                return(tryToConnect(type, graph));
            }
        }
Exemplo n.º 2
0
        public ServiceFamily Build(Type type, ServiceGraph serviceGraph)
        {
            if (type.IsGenericTypeDefinition)
            {
                return(null);
            }
            if (!type.IsConcrete())
            {
                return(null);
            }


            if (!IsReallyPublic(type))
            {
                return(null);
            }

            if (serviceGraph.CouldBuild(type, out var message))
            {
                return(new ServiceFamily(type, serviceGraph.DecoratorPolicies, new ConstructorInstance(type, type, ServiceLifetime.Transient)));
            }
            else
            {
                var empty = new ServiceFamily(type, new IDecoratorPolicy[0], new Instance[0]);
                empty.CannotBeResolvedMessage = message;

                return(empty);
            }

            return(null);
        }
Exemplo n.º 3
0
        public ServiceFamily Build(Type type, ServiceGraph serviceGraph)
        {
            if (!type.IsGenericTypeDefinition)
            {
                return(new ServiceFamily(type, new IDecoratorPolicy[0], new Instance[0]));
            }

            return(null);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Add additional configurations to this container. NOT RECOMMENDED.
        /// </summary>
        /// <param name="services"></param>
        /// <exception cref="InvalidOperationException"></exception>
        public void Configure(IServiceCollection services)
        {
            if (services.Any(x => x.ServiceType == typeof(IFamilyPolicy)))
            {
                throw new InvalidOperationException("Cannot register any IFamilyPolicy objects in Configure()");
            }
            if (services.Any(x => x.ServiceType == typeof(IFamilyPolicy)))
            {
                throw new InvalidOperationException("Cannot register any IFamilyPolicy objects in Configure()");
            }

            ServiceGraph.AppendServices(services);
        }
Exemplo n.º 5
0
        public override void Dispose()
        {
            // Because a StackOverflowException when trying to cleanly shut down
            // an application is really no fun
            if (_isDisposing)
            {
                return;
            }

            _isDisposing = true;

            base.Dispose();
            ServiceGraph.Dispose();
        }
Exemplo n.º 6
0
        private ServiceFamily tryToConnect(Type type, ServiceGraph graph)
        {
            // RIGHT HERE: do the connections thing HERE!
            var connectingTypes = graph.Services.ConnectedConcretions().Where(x => x.CanBeCastTo(type)).ToArray();


            if (connectingTypes.Any())
            {
                var instances = connectingTypes.Select(x => new ConstructorInstance(type, x, ServiceLifetime.Transient)).ToArray();

                return(new ServiceFamily(type, new IDecoratorPolicy[0], instances));
            }

            // This is a problem right here. Need this to be exposed
            return(graph.Families.Values.ToArray()
                   .FirstOrDefault(x => type.IsAssignableFrom(x.ServiceType)));
        }
Exemplo n.º 7
0
        public override async ValueTask DisposeAsync()
        {
            // Because a StackOverflowException when trying to cleanly shut down
            // an application is really no fun
            if (_isDisposing)
            {
                return;
            }

            _isDisposing = true;

            await base.DisposeAsync();

            if (ReferenceEquals(Root, this))
            {
                await ServiceGraph.DisposeAsync();
            }
        }
Exemplo n.º 8
0
        public static async Task <Container> BuildAsync(IServiceCollection services, PerfTimer timer = null)
        {
            bool timedExternally = true;
            var  timerMarker     = "Bootstrapping Lamar Container";

            if (timer == null)
            {
                timedExternally = false;
                timer           = new PerfTimer();
                timer.Start(timerMarker);
            }
            else
            {
                timer.MarkStart(timerMarker);
            }



            var container = new Container();

            var graph = await ServiceGraph.BuildAsync(services, container);

            container.Root         = container;
            container.ServiceGraph = graph;

            container.Bootstrapping = timer;


            graph.Initialize(timer);

            if (timedExternally)
            {
                timer.MarkFinished(timerMarker);
            }
            else
            {
                timer.Stop();
            }

            return(container);
        }
Exemplo n.º 9
0
        public ServiceFamily Build(Type type, ServiceGraph serviceGraph)
        {
            if (type.IsGenericTypeDefinition)
            {
                return(null);
            }
            if (!type.IsConcrete())
            {
                return(null);
            }


            if (!IsReallyPublic(type))
            {
                return(null);
            }

            if (serviceGraph.CouldBuild(type))
            {
                return(new ServiceFamily(type, new ConstructorInstance(type, type, ServiceLifetime.Transient)));
            }

            return(null);
        }
Exemplo n.º 10
0
 private Container(ServiceGraph serviceGraph, Container container) : base(serviceGraph, container)
 {
 }