Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support interface IMetadataAware #15

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,133 +1,133 @@
#region Copyright
// Copyright (c) 2009 - 2011, Kazi Manzur Rashid <[email protected]>.
// This source is subject to the Microsoft Public License.
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
// All other rights reserved.
#endregion

namespace MvcExtensions
{
using System;
#region Copyright
// Copyright (c) 2009 - 2011, Kazi Manzur Rashid <[email protected]>.
// This source is subject to the Microsoft Public License.
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
// All other rights reserved.
#endregion
namespace MvcExtensions
{
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using JetBrains.Annotations;

/// <summary>
/// Extensions for <see cref="ModelMetadataItemBuilder{TValue}"/> which add AsDropDownList and AsListBox methods
/// <summary>
/// Extensions for <see cref="ModelMetadataItemBuilder{TValue}"/> which add AsDropDownList and AsListBox methods
/// </summary>
[TypeForwardedFrom(KnownAssembly.MvcExtensions)]
public static class HtmlSelectModelMetadataItemBuilderExtensions
{
/// <summary>
/// Marks the value to render as DropDownList element in edit mode.
/// </summary>
/// <param name="self">The instance.</param>
/// <param name="viewDataKey">The view data key, the value of the view data key must be a <seealso cref="IEnumerable{T}"/>.</param>
public static class HtmlSelectModelMetadataItemBuilderExtensions
{
/// <summary>
/// Marks the value to render as DropDownList element in edit mode.
/// </summary>
/// <param name="self">The instance.</param>
/// <param name="viewDataKey">The view data key, the value of the view data key must be a <seealso cref="IEnumerable{T}"/>.</param>
/// <returns></returns>
[NotNull]
public static ModelMetadataItemBuilder<TValue> AsDropDownList<TValue>([NotNull] this ModelMetadataItemBuilder<TValue> self, string viewDataKey)
{
return self.AsDropDownList(viewDataKey, (Func<string>)null);
}

/// <summary>
/// Marks the value to render as DropDownList element in edit mode.
/// </summary>
/// <param name="self">The instance.</param>
/// <param name="viewDataKey">The view data key, the value of the view data key must be a <seealso cref="IEnumerable{SelectListItem}"/>.</param>
/// <param name="optionLabel">The option label.</param>
public static ModelMetadataItemBuilder<TValue> AsDropDownList<TValue>([NotNull] this ModelMetadataItemBuilder<TValue> self, string viewDataKey)
{
return self.AsDropDownList(viewDataKey, (Func<string>)null);
}
/// <summary>
/// Marks the value to render as DropDownList element in edit mode.
/// </summary>
/// <param name="self">The instance.</param>
/// <param name="viewDataKey">The view data key, the value of the view data key must be a <seealso cref="IEnumerable{SelectListItem}"/>.</param>
/// <param name="optionLabel">The option label.</param>
/// <returns></returns>
[NotNull]
public static ModelMetadataItemBuilder<TValue> AsDropDownList<TValue>([NotNull] this ModelMetadataItemBuilder<TValue> self, string viewDataKey, string optionLabel)
{
return self.AsDropDownList(viewDataKey, () => optionLabel);
}

/// <summary>
/// Marks the value to render as DropDownList element in edit mode.
/// </summary>
/// <param name="self">The instance.</param>
/// <param name="viewDataKey">The view data key, the value of the view data key must be a <seealso cref="IEnumerable{SelectListItem}"/>.</param>
/// <param name="optionLabel">The option label.</param>
public static ModelMetadataItemBuilder<TValue> AsDropDownList<TValue>([NotNull] this ModelMetadataItemBuilder<TValue> self, string viewDataKey, string optionLabel)
{
return self.AsDropDownList(viewDataKey, () => optionLabel);
}
/// <summary>
/// Marks the value to render as DropDownList element in edit mode.
/// </summary>
/// <param name="self">The instance.</param>
/// <param name="viewDataKey">The view data key, the value of the view data key must be a <seealso cref="IEnumerable{SelectListItem}"/>.</param>
/// <param name="optionLabel">The option label.</param>
/// <returns></returns>
[NotNull]
public static ModelMetadataItemBuilder<TValue> AsDropDownList<TValue>([NotNull] this ModelMetadataItemBuilder<TValue> self, string viewDataKey, Func<string> optionLabel)
{
return self.AsDropDownList(viewDataKey, optionLabel, "DropDownList");
}

/// <summary>
/// Marks the value to render as DropDownList element in edit mode.
/// </summary>
/// <param name="self">The instance.</param>
/// <param name="viewDataKey">The view data key.</param>
/// <param name="optionLabel">The option label.</param>
/// <param name="template">The template.</param>
public static ModelMetadataItemBuilder<TValue> AsDropDownList<TValue>([NotNull] this ModelMetadataItemBuilder<TValue> self, string viewDataKey, Func<string> optionLabel)
{
return self.AsDropDownList(viewDataKey, optionLabel, "DropDownList");
}
/// <summary>
/// Marks the value to render as DropDownList element in edit mode.
/// </summary>
/// <param name="self">The instance.</param>
/// <param name="viewDataKey">The view data key.</param>
/// <param name="optionLabel">The option label.</param>
/// <param name="template">The template.</param>
/// <returns></returns>
[NotNull]
public static ModelMetadataItemBuilder<TValue> AsDropDownList<TValue>([NotNull] this ModelMetadataItemBuilder<TValue> self, string viewDataKey, string optionLabel, [AspMvcEditorTemplate, AspMvcDisplayTemplate]string template)
{
return self.AsDropDownList(viewDataKey, () => optionLabel, template);
}

/// <summary>
/// Marks the value to render as DropDownList element in edit mode.
/// </summary>
/// <param name="self">The instance.</param>
/// <param name="viewDataKey">The view data key.</param>
/// <param name="optionLabel">The option label.</param>
/// <param name="template">The template.</param>
public static ModelMetadataItemBuilder<TValue> AsDropDownList<TValue>([NotNull] this ModelMetadataItemBuilder<TValue> self, string viewDataKey, string optionLabel, [AspMvcEditorTemplate, AspMvcDisplayTemplate]string template)
{
return self.AsDropDownList(viewDataKey, () => optionLabel, template);
}
/// <summary>
/// Marks the value to render as DropDownList element in edit mode.
/// </summary>
/// <param name="self">The instance.</param>
/// <param name="viewDataKey">The view data key.</param>
/// <param name="optionLabel">The option label.</param>
/// <param name="template">The template.</param>
/// <returns></returns>
[NotNull]
public static ModelMetadataItemBuilder<TValue> AsDropDownList<TValue>([NotNull] this ModelMetadataItemBuilder<TValue> self, string viewDataKey, Func<string> optionLabel, [AspMvcEditorTemplate, AspMvcDisplayTemplate]string template)
{
return HtmlSelect(self, template, viewDataKey, optionLabel);
}

/// <summary>
/// Marks the value to render as ListBox in edit mode.
/// </summary>
/// <param name="self">The instance.</param>
/// <param name="viewDataKey">The view data key, the value of the view data key must be a <seealso cref="IEnumerable{SelectListItem}"/>.</param>
public static ModelMetadataItemBuilder<TValue> AsDropDownList<TValue>([NotNull] this ModelMetadataItemBuilder<TValue> self, string viewDataKey, Func<string> optionLabel, [AspMvcEditorTemplate, AspMvcDisplayTemplate]string template)
{
return HtmlSelect(self, template, viewDataKey, optionLabel);
}
/// <summary>
/// Marks the value to render as ListBox in edit mode.
/// </summary>
/// <param name="self">The instance.</param>
/// <param name="viewDataKey">The view data key, the value of the view data key must be a <seealso cref="IEnumerable{SelectListItem}"/>.</param>
/// <returns></returns>
[NotNull]
public static ModelMetadataItemBuilder<TValue> AsListBox<TValue>([NotNull] this ModelMetadataItemBuilder<TValue> self, string viewDataKey)
{
return self.AsListBox(viewDataKey, "ListBox");
}

/// <summary>
/// Marks the value to render as ListBox in edit mode.
/// </summary>
/// <param name="self">The instance.</param>
/// <param name="viewDataKey">The view data key.</param>
/// <param name="template">The template.</param>
public static ModelMetadataItemBuilder<TValue> AsListBox<TValue>([NotNull] this ModelMetadataItemBuilder<TValue> self, string viewDataKey)
{
return self.AsListBox(viewDataKey, "ListBox");
}
/// <summary>
/// Marks the value to render as ListBox in edit mode.
/// </summary>
/// <param name="self">The instance.</param>
/// <param name="viewDataKey">The view data key.</param>
/// <param name="template">The template.</param>
/// <returns></returns>
[NotNull]
public static ModelMetadataItemBuilder<TValue> AsListBox<TValue>([NotNull] this ModelMetadataItemBuilder<TValue> self, string viewDataKey, [AspMvcEditorTemplate, AspMvcDisplayTemplate]string template)
{
return HtmlSelect(self, template, viewDataKey, null);
public static ModelMetadataItemBuilder<TValue> AsListBox<TValue>([NotNull] this ModelMetadataItemBuilder<TValue> self, string viewDataKey, [AspMvcEditorTemplate, AspMvcDisplayTemplate]string template)
{
return HtmlSelect(self, template, viewDataKey, null);
}

[NotNull]
private static ModelMetadataItemBuilder<TValue> HtmlSelect<TValue>([NotNull] ModelMetadataItemBuilder<TValue> self, string templateName, string viewDataKey, Func<string> optionLabel)
{
self.AddAction(
m =>
{
var selectableElementSetting = m.GetAdditionalSettingOrCreateNew<ModelMetadataItemSelectableElementSetting>();

selectableElementSetting.ViewDataKey = viewDataKey;

if (optionLabel != null)
{
selectableElementSetting.OptionLabel = optionLabel;
}

m.TemplateName = templateName;
});

return self;
}
}
private static ModelMetadataItemBuilder<TValue> HtmlSelect<TValue>([NotNull] ModelMetadataItemBuilder<TValue> self, string templateName, string viewDataKey, Func<string> optionLabel)
{
self.AddAction(
m =>
{
var selectableElementSetting = m.GetAdditionalSettingOrCreateNew<ModelMetadataItemSelectableElementSetting>();
selectableElementSetting.ViewDataKey = viewDataKey;
if (optionLabel != null)
{
selectableElementSetting.OptionLabel = optionLabel;
}
m.TemplateName = templateName;
});
return self;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ namespace MvcExtensions
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using System.Web.Mvc;
using JetBrains.Annotations;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using System.Web.Mvc;
using JetBrains.Annotations;

/// <summary>
/// Defines a base class that is used to configure metadata of a model fluently.
/// </summary>
[TypeForwardedFrom(KnownAssembly.MvcExtensions)]
/// </summary>
[TypeForwardedFrom(KnownAssembly.MvcExtensions)]
public abstract class ModelMetadataConfiguration<TModel> : IModelMetadataConfiguration where TModel : class
{
private readonly IDictionary<string, IModelMetadataItemConfigurator> configurations = new Dictionary<string, IModelMetadataItemConfigurator>(StringComparer.OrdinalIgnoreCase);
Expand Down Expand Up @@ -52,57 +52,55 @@ public virtual IDictionary<string, IModelMetadataItemConfigurator> Configuration
/// </summary>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="expression">The expression.</param>
/// <returns></returns>
[NotNull]
/// <returns></returns>
[NotNull]
protected ModelMetadataItemBuilder<TValue> Configure<TValue>([NotNull] Expression<Func<TModel, TValue>> expression)
{
Invariant.IsNotNull(expression, "expression");

string property = ExpressionHelper.GetExpressionText(expression);
Invariant.IsNotNull(property, "property");

var builder = new ModelMetadataItemBuilder<TValue>(new ModelMetadataItem());
configurations[property] = builder;
return builder;
return GetOrCreate<TValue>(property);
}

/// <summary>
/// Configures the specified value.
/// </summary>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="property">The expression.</param>
/// <returns></returns>
[NotNull]
/// <returns></returns>
[NotNull]
protected ModelMetadataItemBuilder<TValue> Configure<TValue>([NotNull] string property)
{
Invariant.IsNotNull(property, "property");
return Create<TValue>(property);
Invariant.IsNotNull(property, "property");

return GetOrCreate<TValue>(property);
}

/// <summary>
/// Configures the specified value.
/// </summary>
/// <param name="property">The expression.</param>
/// <returns></returns>
[NotNull]
/// <returns></returns>
[NotNull]
protected ModelMetadataItemBuilder<object> Configure([NotNull] string property)
{
Invariant.IsNotNull(property, "property");
var item = new ModelMetadataItem();

var builder = new ModelMetadataItemBuilder<object>(item);
configurations[property] = builder;
return builder;
return GetOrCreate<object>(property);
}

private ModelMetadataItemBuilder<TValue> Create<TValue>(string property)
private ModelMetadataItemBuilder<TValue> GetOrCreate<TValue>(string property)
{
var builder = new ModelMetadataItemBuilder<TValue>(new ModelMetadataItem());

configurations[property] = builder;
if (!configurations.ContainsKey(property))
{
var builder = new ModelMetadataItemBuilder<TValue>(new ModelMetadataItem());

configurations[property] = builder;
return builder;
}

return builder;
return (ModelMetadataItemBuilder<TValue>)configurations[property];
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
// This source is subject to the Microsoft Public License.
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
// All other rights reserved.
#endregion

#endregion

using System.Web.Mvc;

namespace MvcExtensions
{
using System;
Expand All @@ -25,6 +27,7 @@ public ModelMetadataItem()
ShowForDisplay = true;
Validations = new List<IModelValidationMetadata>();
AdditionalSettings = new List<IModelMetadataAdditionalSetting>();
MetadataAwares = new List<IMetadataAware>();
}

/// <summary>
Expand Down Expand Up @@ -165,7 +168,12 @@ public IList<IModelMetadataAdditionalSetting> AdditionalSettings
{
get;
private set;
}
}

/// <summary>
/// Gets the MetadataAwares
/// </summary>
public IList<IMetadataAware> MetadataAwares { get; private set; }

/// <summary>
/// Gets or sets the order.
Expand Down
Loading