How to bind view model property with different name

戏子无情 提交于 2019-11-30 08:38:17
lorond

Actually, there is a way to do it.

In ASP.NET binding metadata gathered by TypeDescriptor, not by reflection directly. To be more precious, AssociatedMetadataTypeTypeDescriptionProvider is used, which, in turn, simply calls TypeDescriptor.GetProvider with our model type as parameter:

public AssociatedMetadataTypeTypeDescriptionProvider(Type type)
  : base(TypeDescriptor.GetProvider(type))
{
}

So, everything we need is to set our custom TypeDescriptionProvider for our model.

Let's implement our custom provider. First of all, let's define attribute for custom property name:

[AttributeUsage(AttributeTargets.Property)]
public class CustomBindingNameAttribute : Attribute
{
    public CustomBindingNameAttribute(string propertyName)
    {
        this.PropertyName = propertyName;
    }

    public string PropertyName { get; private set; }
}

If you already have attribute with desired name, you can reuse it. Attribute defined above is just an example. I prefer to use JsonPropertyAttribute because in most cases I work with json and Newtonsoft's library and want to define custom name only once.

The next step is to define custom type descriptor. We will not implement whole type descriptor logic and use default implementation. Only property accessing will be overridden:

public class MyTypeDescription : CustomTypeDescriptor
{
    public MyTypeDescription(ICustomTypeDescriptor parent)
        : base(parent)
    {
    }

    public override PropertyDescriptorCollection GetProperties()
    {
        return Wrap(base.GetProperties());
    }

    public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
    {
        return Wrap(base.GetProperties(attributes));
    }

    private static PropertyDescriptorCollection Wrap(PropertyDescriptorCollection src)
    {
        var wrapped = src.Cast<PropertyDescriptor>()
                         .Select(pd => (PropertyDescriptor)new MyPropertyDescriptor(pd))
                         .ToArray();

        return new PropertyDescriptorCollection(wrapped);
    }
}

Also custom property descriptor need to be implemented. Again, everything except property name will be handled by default descriptor. Note, NameHashCode for some reason is a separate property. As name changed, so it's hash code need to be changed too:

public class MyPropertyDescriptor : PropertyDescriptor
{
    private readonly PropertyDescriptor _descr;
    private readonly string _name;

    public MyPropertyDescriptor(PropertyDescriptor descr)
        : base(descr)
    {
        this._descr = descr;

        var customBindingName = this._descr.Attributes[typeof(CustomBindingNameAttribute)] as CustomBindingNameAttribute;
        this._name = customBindingName != null ? customBindingName.PropertyName : this._descr.Name;
    }

    public override string Name
    {
        get { return this._name; }
    }

    protected override int NameHashCode
    {
        get { return this.Name.GetHashCode(); }
    }

    public override bool CanResetValue(object component)
    {
        return this._descr.CanResetValue(component);
    }

    public override object GetValue(object component)
    {
        return this._descr.GetValue(component);
    }

    public override void ResetValue(object component)
    {
        this._descr.ResetValue(component);
    }

    public override void SetValue(object component, object value)
    {
        this._descr.SetValue(component, value);
    }

    public override bool ShouldSerializeValue(object component)
    {
        return this._descr.ShouldSerializeValue(component);
    }

    public override Type ComponentType
    {
        get { return this._descr.ComponentType; }
    }

    public override bool IsReadOnly
    {
        get { return this._descr.IsReadOnly; }
    }

    public override Type PropertyType
    {
        get { return this._descr.PropertyType; }
    }
}

Finally, we need our custom TypeDescriptionProvider and way to bind it to our model type. By default, TypeDescriptionProviderAttribute is designed to perform that binding. But in this case we will not able to get default provider that we want to use internally. In most cases, default provider will be ReflectTypeDescriptionProvider. But this is not guaranteed and this provider is inaccessible due to it's protection level - it's internal. But we do still want to fallback to default provider.

TypeDescriptor also allow to explicitly add provider for our type via AddProvider method. That what we will use. But firstly, let's define our custom provider itself:

public class MyTypeDescriptionProvider : TypeDescriptionProvider
{
    private readonly TypeDescriptionProvider _defaultProvider;

    public MyTypeDescriptionProvider(TypeDescriptionProvider defaultProvider)
    {
        this._defaultProvider = defaultProvider;
    }

    public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
    {
        return new MyTypeDescription(this._defaultProvider.GetTypeDescriptor(objectType, instance));
    }
}

The last step is to bind our provider to our model types. We can implement it in any way we want. For example, let's define some simple class, such as:

public static class TypeDescriptorsConfig
{
    public static void InitializeCustomTypeDescriptorProvider()
    {
        // Assume, this code and all models are in one assembly
        var types = Assembly.GetExecutingAssembly().GetTypes()
                            .Where(t => t.GetProperties().Any(p => p.IsDefined(typeof(CustomBindingNameAttribute))));

        foreach (var type in types)
        {
            var defaultProvider = TypeDescriptor.GetProvider(type);
            TypeDescriptor.AddProvider(new MyTypeDescriptionProvider(defaultProvider), type);
        }
    }
}

And either invoke that code via web activation:

[assembly: PreApplicationStartMethod(typeof(TypeDescriptorsConfig), "InitializeCustomTypeDescriptorProvider")]

Or simply call it in Application_Start method:

public class MvcApplication : HttpApplication
{
    protected void Application_Start()
    {
        TypeDescriptorsConfig.InitializeCustomTypeDescriptorProvider();

        // rest of init code ...
    }
}

But this is not the end of the story. :(

Consider following model:

public class TestModel
{
    [CustomBindingName("actual_name")]
    [DisplayName("Yay!")]
    public string TestProperty { get; set; }
}

If we try to write in .cshtml view something like:

@model Some.Namespace.TestModel
@Html.DisplayNameFor(x => x.TestProperty) @* fail *@

We will get ArgumentException:

An exception of type 'System.ArgumentException' occurred in System.Web.Mvc.dll but was not handled in user code

Additional information: The property Some.Namespace.TestModel.TestProperty could not be found.

That because all helpers soon or later invoke ModelMetadata.FromLambdaExpression method. And this method take expression we provided (x => x.TestProperty) and takes member name directly from member info and have no clue about any of our attributes, metadata (who cares, huh?):

internal static ModelMetadata FromLambdaExpression<TParameter, TValue>(/* ... */)
{
    // ...

        case ExpressionType.MemberAccess:
            MemberExpression memberExpression = (MemberExpression) expression.Body;
            propertyName = memberExpression.Member is PropertyInfo ? memberExpression.Member.Name : (string) null;
            //                                  I want to cry here - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    // ...
}

For x => x.TestProperty (where x is TestModel) this method will return TestProperty, not actual_name, but model metadata contains actual_name property, have no TestProperty. That is why the property could not be found error thrown.

This is a design failure.

However despite this little inconvenience there are several workarounds, such as:

  1. The easiest way is to access our members by theirs redefined names:

    @model Some.Namespace.TestModel
    @Html.DisplayName("actual_name") @* this will render "Yay!" *@
    

    This is not good. No intellisense at all and as our model change we will have no any compilation errors. On any change anything can be broken and there is no easy way to detect that.

  2. Another way is a bit more complex - we can create our own version of that helpers and forbid anybody from calling default helpers or ModelMetadata.FromLambdaExpression for model classes with renamed properties.

  3. Finally, combination of previous two would be preferred: write own analogue to get property name with redefinition support, then pass that into default helper. Something like this:

    @model Some.Namespace.TestModel
    @Html.DisplayName(Html.For(x => x.TestProperty)) 
    

    Compilation-time and intellisense support and no need to spend a lot of time for complete set of helpers. Profit!

Also everything described above work like a charm for model binding. During model binding process default binder also use metadata, gathered by TypeDescriptor.

But I guess binding json data is the best use case. You know, lots of web software and standards use lowercase_separated_by_underscores naming convention. Unfortunately this is not usual convention for C#. Having classes with members named in different convention looks ugly and can end up in troubles. Especially when you have tools that whining every time about naming violation.

ASP.NET MVC default model binder does not bind json to model the same way as it happens when you call newtonsoft's JsonConverter.DeserializeObject method. Instead, json parsed into dictionary. For example:

{
    complex: {
        text: "blabla",
        value: 12.34
    },
    num: 1
}

will be translated into following dictionary:

{ "complex.text", "blabla" }
{ "complex.value", "12.34" }
{ "num", "1" }

And later these values along with others values from query string, route data and so on, collected by different implementations of IValueProvider, will be used by default binder to bind a model with help of metadata, gathered by TypeDescriptor.

So we came full circle from creating model, rendering, binding it back and use it.

The short answer is NO and long answer still NO. There is no built-in helper, attribute, model binder, whatever is it (Nothing out of box).

But what I did in before answer (I deleted it) was an awful solution that I realized yesterday. I am going to put it in github for who still wants to see (maybe it solves somebody problem) (I don't suggest it also!)

Now I searched it for again and I couldn't find anything helpful. If you are using something like AutoMapper or ValueInjecter like tool for mapping your ViewModel objects to Business objects and if you want to obfuscate that View Model parameters also, probably you are in some trouble. Of course you can do it but strongly typed html helpers are not going to help you alot. I even not talking about the if other developers taking branch and working over common view models.

Luckily my project (4 people working on it, and its commercial use for) not that big for now, so I decided to change View Model property names! (It is still lot work to do. Hundreds of view models to obfuscate their properties!!!) Thank you Asp.Net MVC !

There some ways in the links which I gave in question. But also if you still want to use the BindAlias attribute, I can only suggest you to use the following extension methods. At least you dont have to write same alias string which you write in BindAlias attribute.

Here it is:

public static string AliasNameFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
    Expression<Func<TModel, TProperty>> expression)
{
    var memberExpression = ExpressionHelpers.GetMemberExpression(expression);
    if (memberExpression == null)
        throw new InvalidOperationException("Expression must be a member expression");
    var aliasAttr = memberExpression.Member.GetAttribute<BindAliasAttribute>();
    if (aliasAttr != null)
    {
        return MvcHtmlString.Create(aliasAttr.Alias).ToHtmlString();
    }
    return htmlHelper.NameFor(expression).ToHtmlString();
}

public static string AliasIdFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
    Expression<Func<TModel, TProperty>> expression)
{
    var memberExpression = ExpressionHelpers.GetMemberExpression(expression);
    if (memberExpression == null)
        throw new InvalidOperationException("Expression must be a member expression");
    var aliasAttr = memberExpression.Member.GetAttribute<BindAliasAttribute>();
    if (aliasAttr != null)
    {
        return MvcHtmlString.Create(TagBuilder.CreateSanitizedId(aliasAttr.Alias)).ToHtmlString();
    }
    return htmlHelper.IdFor(expression).ToHtmlString();
}



public static T GetAttribute<T>(this ICustomAttributeProvider provider)
    where T : Attribute
{
    var attributes = provider.GetCustomAttributes(typeof(T), true);
    return attributes.Length > 0 ? attributes[0] as T : null;
}

public static MemberExpression GetMemberExpression<TModel, TProperty>(Expression<Func<TModel, TProperty>> expression)
{
    MemberExpression memberExpression;
    if (expression.Body is UnaryExpression)
    {
        var unaryExpression = (UnaryExpression)expression.Body;
        memberExpression = (MemberExpression)unaryExpression.Operand;
    }
    else
    {
        memberExpression = (MemberExpression)expression.Body;
    }
    return memberExpression;
}

When you want to use it:

[ModelBinder(typeof(AliasModelBinder))]
public class FilterViewModel
{
    [BindAlias("someText")]
    public string FilterParameter { get; set; }
}

In html:

@* at least you dont write "someText" here again *@
@Html.Editor(Html.AliasNameFor(model => model.FilterParameter))
@Html.ValidationMessage(Html.AliasNameFor(model => model.FilterParameter))

So I am leaving this answer here like this. This is even not an answer (and there is no answer for MVC 5) but who searching in google for same problem might find useful this experience.

And here is the github repo: https://github.com/yusufuzun/so-view-model-bind-20869735

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!