示例#1
0
文件: Scope.cs 项目: smcardle88/lamar
        public object TryGetInstance(Type serviceType, string name)
        {
            assertNotDisposed();
            var instance = ServiceGraph.FindInstance(serviceType, name);

            return(instance?.Resolve(this));
        }
示例#2
0
        public object QuickBuild(Type objectType)
        {
            assertNotDisposed();

            if (!objectType.IsConcrete())
            {
                throw new InvalidOperationException("Type must be concrete");
            }

            var ctor = new ConstructorInstance(objectType, objectType, ServiceLifetime.Transient).DetermineConstructor(ServiceGraph, out var message);

            if (ctor == null)
            {
                throw new InvalidOperationException(message);
            }

            var dependencies = ctor.GetParameters().Select(x =>
            {
                var instance = ServiceGraph.FindInstance(x);



                return(instance.QuickResolve(this));
            }).ToArray();

            return(ctor.Invoke(dependencies));
        }
        protected override IEnumerable<Instance> createPlan(ServiceGraph services)
        {
            _inner = services.FindInstance(ServiceType, _instanceKey);
            if (_inner == null) throw new InvalidOperationException($"Referenced instance of {ServiceType.FullNameInCode()} named '{_instanceKey}' does not exist");
            
            _inner.Parent = Parent;
            Lifetime = _inner.Lifetime;

            yield return _inner;
        }
示例#4
0
文件: Scope.cs 项目: smcardle88/lamar
        public object GetInstance(Type serviceType, string name)
        {
            assertNotDisposed();

            var instance = ServiceGraph.FindInstance(serviceType, name);

            if (instance == null)
            {
                throw new LamarMissingRegistrationException(serviceType, name);
            }

            return(instance.Resolve(this));
        }
示例#5
0
文件: Scope.cs 项目: smcardle88/lamar
        public object QuickBuild(Type objectType)
        {
            assertNotDisposed();

            if (!objectType.IsConcrete())
            {
                throw new InvalidOperationException("Type must be concrete");
            }

            var constructorInstance = new ConstructorInstance(objectType, objectType, ServiceLifetime.Transient);
            var ctor    = constructorInstance.DetermineConstructor(ServiceGraph, out var message);
            var setters = constructorInstance.FindSetters(ServiceGraph);


            if (ctor == null)
            {
                throw new InvalidOperationException(message);
            }

            var dependencies = ctor.GetParameters().Select(x =>
            {
                var instance = ServiceGraph.FindInstance(x);

                if (instance == null)
                {
                    throw new InvalidOperationException($"Cannot QuickBuild type {objectType.GetFullName()} because Lamar cannot determine how to build required dependency {x.ParameterType.FullNameInCode()}");
                }

                try
                {
                    return(instance.QuickResolve(this));
                }
                catch (Exception)
                {
                    // #sadtrombone, do it the heavy way instead
                    return(instance.Resolve(this));
                }
            }).ToArray();

            var service = ctor.Invoke(dependencies);

            foreach (var setter in setters)
            {
                setter.ApplyQuickBuildProperties(service, this);
            }

            return(service);
        }