Ejemplo n.º 1
0
        public IBindingDefinition CreateRealBindingDefinition(Type type)
        {
            IBindingDefinition definition;

            if (!bindingDefinitionByType.TryGetValue(type, out definition))
            {
                definition = BindingDefinitionFactory.CreateInstance(type, DefinitionDescription) ?? this;
                bindingDefinitionByType[type] = definition;
            }
            return(definition);
        }
        public static BindingDefinitionHierarchical CreateInstance(Type type, BindingDefinitionDescription definitionDescription)
        {
            try
            {
                if (string.IsNullOrEmpty(definitionDescription.Name))
                {
                    definitionDescription.Name = definitionDescription.BindingExpression.Replace('.', '_');
                }

                BindingDefinitionHierarchical definition = new BindingDefinitionHierarchical(definitionDescription);

                string realBindingDefinitionExpression = definitionDescription.BindingExpression.Split('.')[0];

                BindingDefinitionDescription realBindingDefinitionDescription = new BindingDefinitionDescription()
                {
                    BindingExpression = realBindingDefinitionExpression, IsReadOnly = definitionDescription.IsReadOnly
                };
                definition.realBindingDefinition = BindingDefinitionFactory.CreateInstance(type, realBindingDefinitionDescription);

                string childBindingExpression = definitionDescription.BindingExpression.Substring(realBindingDefinitionExpression.Length + 1);
                BindingDefinitionDescription childBindingDescription = new BindingDefinitionDescription()
                {
                    BindingExpression = childBindingExpression, IsReadOnly = definitionDescription.IsReadOnly
                };
                definition.childBindingDefinition = BindingDefinitionFactory.CreateInstance(definition.realBindingDefinition.BindingType, childBindingDescription);

                definition.CanNotify = definition.childBindingDefinition.CanNotify;

                definition.BindingType = definition.childBindingDefinition.BindingType; // type;
                return(definition);
            }
            catch (Exception ex)
            {
                throw new BindingTemplateException($"Cannot create the 'Hierarchical BindingDefinition' '{definitionDescription.BindingExpression}'. {ex.Message}");
            }
        }
        public static BindingDefinitionComposite CreateInstance(Type type, BindingDefinitionDescription definitionDescription)
        {
            try
            {
                if (string.IsNullOrEmpty(definitionDescription.Name))
                {
                    definitionDescription.Name = definitionDescription.BindingExpression.Replace('.', '_');
                    MatchCollection ret = ValidCharExtract.Matches(definitionDescription.Name);
                    StringBuilder   sb  = new StringBuilder();
                    foreach (Match m in ret)
                    {
                        sb.Append(m.Value);
                    }
                    definitionDescription.Name = sb.ToString();
                }

                definitionDescription.BindingExpression = definitionDescription.BindingExpression.Substring(1);
                definitionDescription.BindingExpression = definitionDescription.BindingExpression.Substring(0, definitionDescription.BindingExpression.Length - 1);

                BindingDefinitionComposite definition = null;
                string          bindingFormat         = definitionDescription.BindingExpression;
                List <string>   results = new List <string>();
                MatchCollection matches = Regex.Matches(bindingFormat, pattern);

                int cpt = -1;
                foreach (Match match in matches)
                {
                    string[] elements = match.Value.Split(new[] { "::" }, StringSplitOptions.None);
                    if (string.IsNullOrEmpty(elements[0]))
                    {
                        bindingFormat = bindingFormat.Replace($"{{{match.Value}}}", string.Empty);
                    }
                    else
                    {
                        int pos = results.FindIndex(s => s.Equals(elements[0]));
                        if (pos == -1)
                        {
                            results.Add(elements[0]);
                            pos = ++cpt;
                        }
                        else
                        {
                            pos = cpt;
                        }
                        string format = $"{{{match.Value}}}";
                        bindingFormat = bindingFormat.Replace(format, $"{{{pos}}}");
                    }
                }
                if (results.Count > 0)
                {
                    List <BindingDefinitionDescription> definitionDescriptions = new List <BindingDefinitionDescription>();
                    if (results.Count == 1)
                    {
                        definitionDescriptions.Add(new BindingDefinitionDescription()
                        {
                            BindingExpression = results[0], IsReadOnly = definitionDescription.IsReadOnly
                        });
                    }
                    else
                    {
                        definitionDescriptions.AddRange(results.Select(s => new BindingDefinitionDescription()
                        {
                            BindingExpression = s, IsReadOnly = true
                        }));
                    }

                    List <IBindingDefinition> nestedDefinitions = BindingDefinitionFactory.CreateInstances(type, definitionDescriptions);

                    if (nestedDefinitions.FirstOrDefault(nd => nd.IsACollection) != null)
                    {
                        throw new BindingTemplateException("The nested 'BindingDefinition' of a 'Composite BindingDefinition' cannot be a collection");
                    }

                    // If more than one nested definition, then force the Binding definition to be ReadOnly
                    definitionDescription.IsReadOnly = nestedDefinitions.Count > 1 || nestedDefinitions[0].IsReadOnly;
                    definition = new BindingDefinitionComposite(definitionDescription)
                    {
                        nestedDefinitions = nestedDefinitions, BindingFormat = bindingFormat, BindingType = typeof(string)
                    };
                    definition.canBeNotifiedNestedDefinitions = definition.nestedDefinitions.Where(d => d.CanNotify).ToList();
                    definition.CanNotify = definition.canBeNotifiedNestedDefinitions.Count > 0;
                }
                return(definition);
            }
            catch (Exception ex)
            {
                throw new BindingTemplateException($"Cannot create the 'Composite BindingDefinition' '{definitionDescription.BindingExpression}'. {ex.Message}");
            }
        }