protected override object GetDependency(IAutowireContext context, Node node)
        {
            Ensure.Any.IsNotNull(context, nameof(context));

            Ensure.Any.IsNotNull(node, nameof(node),
                                 opts => opts.WithMessage(
                                     "[Ancestor] attribute is only supported on members of a Node type class."));

            Ensure.Bool.IsFalse(Enumerable, nameof(node),
                                opts => opts.WithMessage(
                                    "[Ancestor] attribute can't be used on an IEnumerable<T> type field or property."));

            var ancestor = node;

            while ((ancestor = ancestor.GetParent()) != null)
            {
                if (!DependencyType.IsInstanceOfType(ancestor))
                {
                    continue;
                }

                return(ancestor);
            }

            return(null);
        }
Exemplo n.º 2
0
        protected override IEnumerable GetDependencies(IAutowireContext context, Node node)
        {
            IEnumerable enumerable = new ServiceDependencyCollector(
                context, node, Enumerable ? TargetType : DependencyType);

            if (Attribute.Local)
            {
                enumerable = enumerable.Cast <object>().Take(1);
            }

            if (Enumerable)
            {
                enumerable = enumerable
                             .OfType <IEnumerable>()
                             .Fold(EnumerableHelper.Empty(DependencyType),
                                   (e1, e2) => EnumerableHelper.Concat(e1, e2, DependencyType));

                enumerable = EnumerableHelper.Cast(enumerable, DependencyType);
            }
            else
            {
                enumerable = EnumerableHelper.OfType(enumerable, DependencyType);
            }

            return(enumerable);
        }
Exemplo n.º 3
0
        public override void Process(IAutowireContext context, Node node)
        {
            object dependency;

            var enumerable = GetDependencies(context, node);

            bool hasValue;

            if (Enumerable)
            {
                dependency = enumerable;
                hasValue   = enumerable.OfType <object>().Any();
            }
            else if (Optional)
            {
                var option = OptionalHelper.HeadOrNone(enumerable, DependencyType);

                dependency = option;
                hasValue   = option.IsSome;
            }
            else
            {
                dependency = enumerable.OfType <object>().FirstOrDefault();
                hasValue   = dependency != null;
            }

            if (Attribute.Required && !hasValue)
            {
                throw new InvalidOperationException(
                          $"Cannot resolve required dependency for {node.GetPath()}.{Member.Name}.");
            }

            TargetSetter(node, dependency);
        }
Exemplo n.º 4
0
        private Option <object> FindService(IAutowireContext context)
        {
            Debug.Assert(context != null, "context != null");

            var targetType = _targetType;
            var targetNode = _targetNode;

            return(context.FindService(targetType).BiBind(
                       Some,
                       () =>
            {
                var factoryType = typeof(IServiceFactory <>).MakeGenericType(targetType);
                var factory = context.FindService(factoryType);

                return factory.Bind(f =>
                {
                    var method = typeof(IServiceFactory <>)
                                 .MakeGenericType(targetType)
                                 .GetMethod("Create", new[] { typeof(IAutowireContext), typeof(object) });

                    Debug.Assert(method != null, "method != null");

                    var service = method.Invoke(f, new object[] { context, targetNode });

                    return Optional(service);
                });
            }
                       ));
        }
Exemplo n.º 5
0
        protected override object GetDependency(IAutowireContext context, Node node)
        {
            Ensure.Any.IsNotNull(context, nameof(context));

            Ensure.Any.IsNotNull(node, nameof(node),
                                 opts => opts.WithMessage(
                                     "[Node] attribute is only supported on members of a Node type class."));

            var hasPath = !string.IsNullOrWhiteSpace(NodePath);

            object dependency;

            if (Enumerable)
            {
                var parent = hasPath ? node.GetNode(NodePath) : node;
                var list   = parent?.GetChildren().Where(DependencyType.IsInstanceOfType).ToList();

                dependency = EnumerableHelper.Cast(list, DependencyType);
            }
            else
            {
                var path = hasPath ? NodePath : NormalizeMemberName(Member.Name);

                dependency = node.GetNode(path);
            }

            return(dependency);
        }
Exemplo n.º 6
0
        public ILogger Create(IAutowireContext context, object service)
        {
            Ensure.Any.IsNotNull(context, nameof(context));
            Ensure.Any.IsNotNull(service, nameof(service));

            return(LoggerFactory?.CreateLogger(service.GetType().FullName));
        }
Exemplo n.º 7
0
        public ILogger Create(IAutowireContext context, object service)
        {
            Ensure.That(service, nameof(service)).IsNotNull();

            var factory = this.GetRootContext().FindService <ILoggerFactory>();

            return(factory.Map(f => f.CreateLogger(service.GetType())).Head());
        }
Exemplo n.º 8
0
        public ILogger Create(IAutowireContext context, object service)
        {
            Ensure.Any.IsNotNull(context, nameof(context));
            Ensure.Any.IsNotNull(service, nameof(service));

            var factory = this.GetRootContext().GetService<ILoggerFactory>();

            return factory?.CreateLogger(service.GetType().FullName);
        }
Exemplo n.º 9
0
        protected override object GetDependency(IAutowireContext context, Node node)
        {
            Ensure.Any.IsNotNull(context, nameof(context));
            Ensure.Any.IsNotNull(node, nameof(node));

            var factoryType = typeof(IServiceFactory <>).MakeGenericType(TargetType);

            IEnumerable enumerable = null;

            var current = context;

            while (current != null)
            {
                var dependency = current.GetService(TargetType);

                if (dependency == null)
                {
                    var factory = current.GetService(factoryType);

                    if (factory != null)
                    {
                        var method = typeof(IServiceFactory <>)
                                     .MakeGenericType(TargetType)
                                     .GetMethod("Create", new[] { typeof(IAutowireContext), typeof(object) });

                        Debug.Assert(method != null, "method != null");

                        dependency = method.Invoke(factory, new object[] { context, node });
                    }
                }

                if (dependency != null)
                {
                    if (Enumerable)
                    {
                        if (enumerable == null)
                        {
                            enumerable = (IEnumerable)dependency;
                        }
                        else
                        {
                            enumerable = EnumerableHelper.Concat(enumerable, (IEnumerable)dependency, DependencyType);
                        }
                    }
                    else
                    {
                        return(dependency);
                    }
                }

                current = current.Parent;
            }

            return(enumerable);
        }
 public override void Process(IAutowireContext context, Node node)
 {
     try
     {
         Method.Invoke(node, new object[0]);
     }
     catch (TargetInvocationException e)
     {
         ExceptionDispatchInfo.Capture(e.InnerException ?? e).Throw();
     }
 }
Exemplo n.º 11
0
        private static void ProcessNode(IAutowireContext context, INodeProcessor processor, Node node)
        {
            try
            {
                context.LogDebug("Processing {} on '{}'.", processor, node);

                processor.Process(context, node);
            }
            catch (Exception e)
            {
                context.LogError(e, "Failed to process {} on '{}'.", processor, node);
            }
        }
Exemplo n.º 12
0
        public override void Process(IAutowireContext context, Node node)
        {
            Ensure.That(context, nameof(context)).IsNotNull();
            Ensure.That(node, nameof(node)).IsNotNull();

            var target = context.Node == node ? context.Parent : Some(context);

            Debug.Assert(target.IsSome, "target.IsSome");

            target
            .SelectMany(t => Provides, (t, tpe) => (t, tpe))
            .Iter(v => v.t.AddService(c => c.AddSingleton(v.tpe, node)));
        }
Exemplo n.º 13
0
        public static IAutowireContext GetRootContext([NotNull] this Node node)
        {
            Ensure.Any.IsNotNull(node, nameof(node));

            if (_rootContext != null)
            {
                return(_rootContext);
            }

            var viewport = node.GetTree().Root;

            return(_rootContext = GetLocalContext(viewport));
        }
        public override void Process(IAutowireContext context, Node node)
        {
            Ensure.Any.IsNotNull(context, nameof(context));
            Ensure.Any.IsNotNull(node, nameof(node));

            try
            {
                Method.Invoke(node, new object[0]);
            }
            catch (TargetInvocationException e)
            {
                ExceptionDispatchInfo.Capture(e.InnerException ?? e).Throw();
            }
        }
        public override void Process(IAutowireContext context, Node node)
        {
            Ensure.Any.IsNotNull(context, nameof(context));
            Ensure.Any.IsNotNull(node, nameof(node));

            var target = context.Node == node ? context.Parent : context;

            Debug.Assert(target != null, "context.Parent != null");

            foreach (var type in Attribute.Types)
            {
                target.AddService(c => c.AddSingleton(type, node));
            }
        }
Exemplo n.º 16
0
        public ServiceDependencyCollector(
            IAutowireContext context, Node targetNode, Type targetType)
        {
            Ensure.That(context, nameof(context)).IsNotNull();
            Ensure.That(targetNode, nameof(targetNode)).IsNotNull();
            Ensure.That(targetType, nameof(targetType)).IsNotNull();

            _context    = context;
            _targetNode = targetNode;
            _targetType = targetType;

            _current = Some(context);
            _value   = None;
        }
Exemplo n.º 17
0
        protected override IEnumerable GetDependencies(IAutowireContext context, Node node)
        {
            Ensure.That(node, nameof(node)).IsNotNull();

            var ancestors = node.GetAncestors().Bind(c =>
            {
                var a = c.FindDelegate().IfNone(c);

                return(DependencyType.IsInstanceOfType(a)
                    ? Some(a)
                    : Some(a).OfType <IGameNodeFactory>().Bind(f => f.Service.ToOption()));
            });

            return(EnumerableHelper.OfType(ancestors.Freeze(), DependencyType));
        }
Exemplo n.º 18
0
        public override void Process(IAutowireContext context, Node node)
        {
            Ensure.Any.IsNotNull(context, nameof(context));
            Ensure.Any.IsNotNull(node, nameof(node));

            var dependency = GetDependency(context, node);

            if (Required && !HasValue(dependency, DependencyType))
            {
                var member = $"{Member.DeclaringType?.FullName}.{Member.Name}";

                throw new InvalidOperationException(
                          $"Cannot resolve required dependency for {member}.");
            }

            TargetSetter(node, dependency);
        }
Exemplo n.º 19
0
        public static void Autowire([NotNull] this Node node, IAutowireContext context = null)
        {
            Ensure.Any.IsNotNull(node, nameof(node));

            if (!((context ?? GetAutowireContext(node)) is AutowireContext target))
            {
                throw new InvalidOperationException(
                          $"No AutowireContext found for node: '{node.Name}'.");
            }

            target.Register(node);

            if (target.Node == node)
            {
                target.Initialize();
            }
        }
Exemplo n.º 20
0
        public override void Process(IAutowireContext context, Node node)
        {
            Ensure.Any.IsNotNull(context, nameof(context));

            if (node is IAutowireContext local)
            {
                if (node != context)
                {
                    local.Build();
                }
            }
            else
            {
                throw new ArgumentOutOfRangeException(
                          nameof(node),
                          "Node is not an instance of IAutowireContext.");
            }
        }
Exemplo n.º 21
0
        public override void Process(IAutowireContext context, Node node)
        {
            Ensure.That(context, nameof(context)).IsNotNull();

            if (node is IServiceDefinitionProvider provider)
            {
                var target = context.Node == node ? context.Parent : Some(context);

                Debug.Assert(target.IsSome, "target.IsSome");

                target.Iter(t => t.AddService(provider.AddServices));
            }
            else
            {
                throw new ArgumentOutOfRangeException(
                          nameof(node),
                          "Node is not an instance of IServiceDefinitionProvider.");
            }
        }
Exemplo n.º 22
0
        public override void Process(IAutowireContext context, Node node)
        {
            Ensure.Any.IsNotNull(context, nameof(context));

            if (node is IServiceDefinitionProvider provider)
            {
                var target = context.Node == node ? context.Parent : context;

                Debug.Assert(target != null, "context.Parent != null");

                target.AddService(provider.AddServices);
            }
            else
            {
                throw new ArgumentOutOfRangeException(
                          nameof(node),
                          "Node is not an instance of IServiceDefinitionProvider.");
            }
        }
Exemplo n.º 23
0
        public override void Process(IAutowireContext context, Node node)
        {
            Ensure.Any.IsNotNull(context, nameof(context));
            Ensure.Any.IsNotNull(node, nameof(node));

            try
            {
                Method.Invoke(node, new object[0]);
            }
            catch (TargetInvocationException e)
            {
                //TODO: Until godotengine/godot#16107 gets fixed.
                if (e.InnerException != null)
                {
                    GD.Print(e.InnerException.Message);
                    GD.Print(e.InnerException.StackTrace);
                }

                ExceptionDispatchInfo.Capture(e.InnerException ?? e).Throw();
            }
        }
Exemplo n.º 24
0
        protected override IEnumerable GetDependencies(IAutowireContext context, Node node)
        {
            var path = FindNodePath(node);

            IEnumerable dependency;

            if (Enumerable)
            {
                var p      = path.IfNone(NormalizeMemberName(Member.Name));
                var parent = node.FindComponent <Node>(p).IfNone(node);

                dependency = parent.GetChildren().Cast <Node>().Bind(c => c.OfType(DependencyType)).Freeze();
            }
            else
            {
                var targetPath = path.IfNone(() => NormalizeMemberName(Member.Name));

                dependency = node.FindComponent(targetPath, DependencyType).Freeze();
            }

            return(EnumerableHelper.Cast(dependency, DependencyType));
        }
Exemplo n.º 25
0
 public void Process(IAutowireContext context) => _processor.Invoke(context);
 protected abstract object GetDependency([NotNull] IAutowireContext context, [NotNull] Node node);
Exemplo n.º 27
0
 public abstract void Process(IAutowireContext context, Node node);
Exemplo n.º 28
0
 public void ProcessDeferred(IAutowireContext context) => _deferredProcessor?.Invoke(context);
Exemplo n.º 29
0
 protected abstract IEnumerable GetDependencies(IAutowireContext context, Node node);
Exemplo n.º 30
0
 public void ProcessDeferred(IAutowireContext context) => _deferredProcessor.Iter(p => p.Invoke(context));