Esempio n. 1
0
        public bool TryCreateProxy(object source, PathToken token, IObjectSourceProxyFactory factory, out IObjectSourceProxy proxy)
        {
            proxy = null;
            if (source == null || token.HasNext() || !(token.Current is MemberNode))
            {
                return(false);
            }

            MemberInfo memberInfo = token.GetMemberInfo(source);

            if (memberInfo == null)
            {
                return(false);
            }

            var fieldInfo = memberInfo as FieldInfo;

            if (fieldInfo != null && typeof(IInteractionRequest).IsAssignableFrom(fieldInfo.FieldType))
            {
                proxy = new InteractionRequestFieldObjectSourceProxy(source, fieldInfo);
                return(true);
            }

            var propertyInfo = memberInfo as PropertyInfo;

            if (propertyInfo != null && typeof(IInteractionRequest).IsAssignableFrom(propertyInfo.PropertyType))
            {
                proxy = new InteractionRequestPropertyObjectSourceProxy(source, propertyInfo);
                return(true);
            }

            return(false);
        }
Esempio n. 2
0
        public virtual IObjectSourceProxy CreateStaticProxy(Type type, PathToken token, IObjectSourceProxyFactory factory)
        {
            var node  = token.Current;
            var proxy = token.HasNext() ? this.CreateStaticChainedProxy(type, node, token.NextToken(), factory) : this.CreateStaticLeafProxy(type, node);

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

            if (log.IsWarnEnabled)
            {
                log.WarnFormat("Unable to bind: Not found the \"{0}\" member on the \"{1}\" type.", (node as MemberNode).Name, type.Name);
            }

            return(null);
        }
Esempio n. 3
0
        public bool TryCreateProxy(object source, PathToken token, IObjectSourceProxyFactory factory, out IObjectSourceProxy proxy)
        {
            proxy = null;
            IPathNode node = token.Current;

            if (node is TypeNode)
            {
                TypeNode typeNode = (node as TypeNode);
                Type     type     = typeNode.Type;
                if (type == null)
                {
                    type = TypeFinderUtils.FindType(typeNode.Name);
                }

                if (type == null || !token.HasNext())
                {
                    if (log.IsWarnEnabled)
                    {
                        log.WarnFormat("Unable to bind: not found the \"{0}\" type.", typeNode.Name);
                    }

                    return(false);
                }

                proxy = CreateStaticProxy(type, token.NextToken(), factory);
                if (proxy != null)
                {
                    return(true);
                }
            }
            else
            {
                proxy = CreateProxy(source, token, factory);
                if (proxy != null)
                {
                    return(true);
                }
            }

            return(false);
        }
Esempio n. 4
0
        public virtual IObjectSourceProxy CreateProxy(object source, PathToken token, IObjectSourceProxyFactory factory)
        {
            if (source == null)
            {
                return(new EmptyObjectSourceProxy());
            }

            var node  = token.Current;
            var proxy = token.HasNext() ? this.CreateChainedProxy(source, node, token.NextToken(), factory) : this.CreateLeafProxy(source, node);

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

            if (log.IsWarnEnabled)
            {
                log.WarnFormat("Unable to bind: Not found the \"{0}\" member on the \"{1}\" type.", (node as MemberNode).Name, source.GetType().Name);
            }

            return(null);
        }
        void Bind(object source, PathToken token)
        {
            int          index = token.Index;
            ISourceProxy proxy = factory.Create(source, token);

            if (proxy == null)
            {
                var node = token.Current;
                if (node is MemberNode)
                {
                    var    memberNode = node as MemberNode;
                    string typeName   = source != null?source.GetType().Name : memberNode.Type.Name;

                    throw new Exception(string.Format("Not found the member named '{0}' in the '{1}' type", memberNode.Name, typeName));
                }
                throw new Exception("proxy is null.");
            }

            ProxyEntry entry = new ProxyEntry(proxy, token);

            proxies[index] = entry;

            if (token.HasNext())
            {
                if (proxy is INotifiable)
                {
                    entry.Handler = (sender, args) =>
                    {
                        lock (_lock)
                        {
                            try
                            {
                                var proxyEntry = proxies[index];
                                if (proxyEntry == null || sender != proxyEntry.Proxy)
                                {
                                    return;
                                }

                                Rebind(index);
                            }
                            catch (Exception e)
                            {
                                if (log.IsErrorEnabled)
                                {
                                    log.ErrorFormat("{0}", e);
                                }
                            }
                        }
                    };
                }

                var child = (proxy as IObtainable).GetValue();
                if (child != null)
                {
                    Bind(child, token.NextToken());
                }
                else
                {
                    this.RaiseValueChanged();
                }
            }
            else
            {
                if (proxy is INotifiable)
                {
                    entry.Handler = (sender, args) => { this.RaiseValueChanged(); }
                }
                ;
                this.RaiseValueChanged();
            }
        }

        void Rebind(int index)
        {
            for (int i = proxies.Length - 1; i > index; i--)
            {
                ProxyEntry proxyEntry = proxies[i];
                if (proxyEntry == null)
                {
                    continue;
                }

                var proxy = proxyEntry.Proxy;
                proxyEntry.Proxy = null;
                if (proxy != null)
                {
                    proxy.Dispose();
                }
            }

            ProxyEntry entry      = proxies[index];
            var        obtainable = entry.Proxy as IObtainable;

            if (obtainable == null)
            {
                this.RaiseValueChanged();
                return;
            }

            var source = obtainable.GetValue();

            if (source == null)
            {
                this.RaiseValueChanged();
                return;
            }

            Bind(source, entry.Token.NextToken());
        }

        void Unbind()
        {
            for (int i = proxies.Length - 1; i <= 0; i--)
            {
                ProxyEntry proxyEntry = proxies[i];
                if (proxyEntry == null)
                {
                    continue;
                }

                proxyEntry.Dispose();
                proxies[i] = null;
            }
        }