AutoMapper中是否有一种方法可以指定要修剪所有映射的字符串 [英] Is there a way in AutoMapper to specify that all strings being mapped are to be trimmed

查看:77
本文介绍了AutoMapper中是否有一种方法可以指定要修剪所有映射的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将WinForms应用程序转换为ASP.NET MVC4,并尝试使用AutoMapper实现ViewModel方法,以在域实体和ViewModel之间进行映射,主要是将更复杂的对象图展平为ViewModel,以便在视图中轻松绑定

我遇到的问题是,当某些字段留空时,域模型要应用Trim()时将抛出NullReferenceException.

最初,这一切都是在域对象中处理的,例如在Customer对象中,我有一个字符串属性,该属性会进行修剪以确保传递的任何值都被修剪,这样就不必在原始的WinForms中用值来使原始WinForms混乱可以进行设置,只需将其传递给域模型,然后在值保持不变的情况下让它设置Trim或ToUpper.

public virtual String Name
{
    get { return _name; }
    set { _name = StringFormatting.ToTitleCase(value.Trim()); }
}

public virtual String Email
{
    get { return _email; }
    set { _email = value.Trim(); }
}

就我的ViewModel而言,我刚刚获得了自动实现的属性:

public class CustomerCreateViewModel
{
    [Required(ErrorMessage="Please enter a name for this customer")]
    public string Name { get; set; }

    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }
}

但是,如果在提交表单时电子邮件文本框为空,则会在

public class CustomerCreateViewModel
{
    [Required(ErrorMessage="Please enter a name for this customer")]
    public string Name { get; set; }

    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }
}

上抛出

set {_email = value.Trim(); }

为了解决这个问题,我添加了一个setter保护子句

public virtual String Email
{
    get { return _email; }
    set
    {
        if (_email == value) return;
        _email = value.Trim();
    }
}

且该起作用的之处在于该字段的错误消失了,但它移到了下一个属性Mobile-我重复了此修复程序,然后又遍历了下一个和下一个下一个.这些都不是必需的,因此将它们正确传递为空是正确的.

AutoMapper中是否有一种方法可以指定要修剪所有映射的字符串? 我是在错误地处理这个问题吗?不需要修剪字符串吗?还是应该执行修剪的行踪,我的偏好是只需要在提交字符串时修剪字符串,而不必在任何单独控制器的代码中修剪字符串,感觉应该在ViewModel/Domain模型中的某个地方完成,但是在哪里?

解决方案

您可以像这样使用ITypeConverter<M,V> ...

public class ModelToViewModelConverter : ITypeConverter<Model, ViewModel>
{
        public UIItemViewModel Convert(ResolutionContext context)
        {
            Model model = (Model)context.SourceValue;
            // Perform your logic 
        }
}

I'm converting a WinForms app to ASP.NET MVC4 and trying to implement a ViewModel approach using AutoMapper to map between the domain entities and the ViewModels, mostly to flatten the more complex object graphs into ViewModels for easy binding in the views.

The problem I'm running into is that when leaving some fields blank that the domain model wants to apply a Trim() a NullReferenceException is thrown.

Originally this was all handled in the domain objects for example in the Customer object I have a string property that trims to ensure whatever value was passed in is trimmed, and that way not have to clutter the original WinForms with Trim everywhere the value could be set, just rely on passing it to the domain model and let it set do the Trim or ToUpper as the value was persisted.

public virtual String Name
{
    get { return _name; }
    set { _name = StringFormatting.ToTitleCase(value.Trim()); }
}

public virtual String Email
{
    get { return _email; }
    set { _email = value.Trim(); }
}

and in terms of my ViewModel I've just got auto implemented properties:

public class CustomerCreateViewModel
{
    [Required(ErrorMessage="Please enter a name for this customer")]
    public string Name { get; set; }

    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }
}

However if the email textbox is empty when the form is submitted then a NullReferenceException would be thrown on

set { _email = value.Trim() ; }

To get around that I added a setter guard clause

public virtual String Email
{
    get { return _email; }
    set
    {
        if (_email == value) return;
        _email = value.Trim();
    }
}

and that worked in that the error with that field went away, but it moved to the next property, Mobile - which I repeated the fix for and then on to the next and the next and the next. None of these are required so it is correct for them to be passed in empty.

Is there a way in AutoMapper to specify that all strings being mapped are to be trimmed? Am I approaching this wrongly; is it not necessary to Trim strings? Or whereabouts should that trimming be performed, my preference would be to only need to trim strings as they're submitted, but not in the code for any individual controller, it feels like it should be done somewhere in the ViewModel / Domain model, but where?

解决方案

You can use ITypeConverter<M,V> like so...

public class ModelToViewModelConverter : ITypeConverter<Model, ViewModel>
{
        public UIItemViewModel Convert(ResolutionContext context)
        {
            Model model = (Model)context.SourceValue;
            // Perform your logic 
        }
}

这篇关于AutoMapper中是否有一种方法可以指定要修剪所有映射的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆