Beispiel #1
0
 /// <summary>
 /// The method binds tags found in the template to property values of the dtoObject parameter.
 /// <para>In order to bind, tag names must match the property names of the dtoObject (case-insensitive)</para>
 /// <para>When a tag does not match any property in the dtoObject the missingPropertiesCallback Action</para>
 /// <para>delegate is called passing in the tag as a parameter so it can be handled in the callback</para>
 /// <para>This method handles resolving rooted path tags such as "@~/forums/@"</para>
 /// </summary>
 /// <param name="templateFile">The name of the Template File to Parse</param>
 /// <param name="writer">The TextWriter of the current View</param>
 /// <param name="dtoObject">An instance of a Dto Object/ViewModel object</param>
 /// <param name="missingPropertiesCallback">A callback Action delegate that will handle any tags that did not match property names of the dtoObject</param>
 protected void BindModelToTagsFromTemplate(string templateFile, TextWriter writer, object dtoObject, Action <string> missingPropertiesCallback = null)
 {
     ParseTemplate(templateFile, writer, tag =>
     {
         object propertyValueObj = DtoBinder.GetPropertyValue(dtoObject, tag);
         if (propertyValueObj != null)
         {
             writer.Write(propertyValueObj);
         }
         else if (missingPropertiesCallback != null)
         {
             missingPropertiesCallback(tag);
         }
     });
 }
Beispiel #2
0
        protected T Bind <T>() where T : class, new()
        {
            Type type       = typeof(T);
            var  nameValues = GetRequestNameValueCollection();

            if (RequestBinder.Contains(type))
            {
                return((T)RequestBinder.GetBinder(type).Bind(nameValues, type));
            }
            else if (!type.IsValueType)
            {
                return(DtoBinder.CreateInstance <T>(nameValues));
            }
            else
            {
                return(default(T));
            }
        }
Beispiel #3
0
        public static void Repeat <T>(BaseView view, TextWriter writer, string headerTemplate,
                                      string itemTemplate, string footerTemplate, IEnumerable <T> model,
                                      Action <string, T> missingPropertiesCallback = null)
        {
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }
            Dictionary <string, PropertyInfo> properties = null;

            if (model.First() != null)
            {
                properties = DtoBinder.GetPropertyInfos(model.First());
            }

            view.ParseTemplate(headerTemplate, writer, headerTag =>
            {
                if (String.CompareOrdinal(headerTag, ItemTemplateTag) == 0)
                {
                    foreach (var item in model)
                    {
                        view.ParseTemplate(itemTemplate, writer, itemTag =>
                        {
                            if (properties.ContainsKey(itemTag))
                            {
                                var propInfo = properties[itemTag];
                                if (propInfo != null)
                                {
                                    writer.Write(propInfo.GetValue(item, null));
                                }
                            }
                            else if (missingPropertiesCallback != null)
                            {
                                missingPropertiesCallback(itemTag, item);
                            }
                        });
                    }
                }
                else if (String.CompareOrdinal(headerTag, FooterTemplateTag) == 0)
                {
                    view.ParseTemplate(footerTemplate, writer, footerTag =>
                    {
                        object propertyValueObj = DtoBinder.GetPropertyValue(view, footerTag);
                        if (propertyValueObj != null)
                        {
                            writer.Write(propertyValueObj);
                        }
                        else if (missingPropertiesCallback != null)
                        {
                            missingPropertiesCallback(footerTag, default(T));
                        }
                    });
                }
                else
                {
                    object propertyValueObj = DtoBinder.GetPropertyValue(view, headerTag);
                    if (propertyValueObj != null)
                    {
                        writer.Write(propertyValueObj);
                    }
                    else if (missingPropertiesCallback != null)
                    {
                        missingPropertiesCallback(headerTag, default(T));
                    }
                }
            });
        }