Пример #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));
            }
        }
Пример #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);
        }
Пример #3
0
        public ServiceFamily Build(Type type, ServiceGraph serviceGraph)
        {
            if (!type.IsGenericTypeDefinition)
            {
                return(new ServiceFamily(type, new IDecoratorPolicy[0], new Instance[0]));
            }

            return(null);
        }
Пример #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);
        }
Пример #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();
        }
Пример #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)));
        }
Пример #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();
            }
        }
Пример #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);
        }
Пример #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);
        }
Пример #10
0
 private Container(ServiceGraph serviceGraph, Container container) : base(serviceGraph, container)
 {
 }