asp.net-mvc – 使用Automapper将字符串映射到枚举
发布时间:2020-12-16 04:37:27 所属栏目:asp.Net 来源:网络整理
导读:我的问题是从已从数据库返回的 Linq2Sql对象中保护Viewmodel.我们已经在一些领域做到了这一点并且有一个很好的分层模式,但是最新的项目要求使用一些枚举,这引起了全面的麻烦.目前我们从数据库中撤回然后使用Automapper来水合(或展平)到我们的View模型中,但是
我的问题是从已从数据库返回的
Linq2Sql对象中保护Viewmodel.我们已经在一些领域做到了这一点并且有一个很好的分层模式,但是最新的项目要求使用一些枚举,这引起了全面的麻烦.目前我们从数据库中撤回然后使用Automapper来水合(或展平)到我们的View模型中,但是模型中的枚举似乎导致了Automapper的问题.我已经尝试创建自定义resovler,它已满足我所有其他映射要求,但它在这个实例中不起作用.
代码示例如下: public class CustomerBillingTabView{ public string PaymentMethod {get; set;} ...other details } public class BillingViewModel{ public PaymentMethodType PaymentMethod {get; set;} ...other details } public enum PaymentMethodType { Invoice,DirectDebit,CreditCard,Other } public class PaymentMethodTypeResolver : ValueResolver<CustomerBillingTabView,PaymentMethodType> { protected override PaymentMethodType ResolveCore(CustomerBillingTabView source) { if (string.IsNullOrWhiteSpace(source.PaymentMethod)) { source.PaymentMethod = source.PaymentMethod.Replace(" ",""); return (PaymentMethodType)Enum.Parse(typeof(PaymentMethodType),source.PaymentMethod,true); } return PaymentMethodType.Other; } } CreateMap<CustomerBillingTabView,CustomerBillingViewModel>() .ForMember(c => c.CollectionMethod,opt => opt.ResolveUsing<PaymentMethodTypeResolver>()) 我收到以下错误 [ArgumentException: Type provided must be an Enum. Parameter name: enumType] System.Enum.TryParseEnum(Type enumType,String value,Boolean ignoreCase,EnumResult& parseResult) +9626766 System.Enum.Parse(Type enumType,Boolean ignoreCase) +80 AutoMapper.Mappers.EnumMapper.Map(ResolutionContext context,IMappingEngineRunner mapper) +231 AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) +720 我想坚持使用Automapper进行所有的映射操作,但是我看到很多人说它没有做这种类型的映射,所以我开始怀疑我是否在错误中使用它办法?另外,我已经看过一些关于ValueInjecter的提及 – 这是Automapper的替代方案,还是只是插入Automapper中的漏洞来模拟水化并使用Automapper进行展平? 是的,我可以在我的ViewModel中使用一个字符串,但我不是魔术字符串的粉丝,帮助者使用这个特殊项目在许多地方执行某些逻辑. 解决方法
这是AutoMapper文档的一个问题.如果您下载AutoMapper源,那里有一些示例.您想要的代码如下所示:
public class PaymentMethodTypeResolver : ValueResolver<CustomerBillingTabView,PaymentMethodType> { protected override PaymentMethodType ResolveCore(CustomerBillingTabView source) { string paymentMethod = source.Context.SourceValue as string; if (string.IsNullOrWhiteSpace(paymentMethod)) { paymentMethod = paymentMethod.Replace(" ",""); return source.New((PaymentMethodType)Enum.Parse(typeof(PaymentMethodType),paymentMethod,true)); } return source.New(PaymentMethodType.Other); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc-3 – Apress Pro Asp.net MVC框架3 – SportsS
- asp.net-mvc – ASP.NET MVC 5 Identity 2.0,Windows Auth,
- asp.net-mvc – AuthenticationManager.SignIn与Authentica
- asp.net-mvc – MVC 3应用程序中的自定义Http处理程序
- asp.net – 为每个用户创建专用URL(ASP .net)
- asp.net mvc长时间操作
- asp.net-mvc – 是否有可能在ASP.NET MVC中对一些AddModelE
- asp.net – 关键字不支持:’server’
- asp.net – 如何确定IUSR密码
- asp.net – 使用MVC 4的.NET 4.5数据访问应用程序块
推荐文章
站长推荐
- asp.net-mvc-3 – 模型绑定器和隐藏字段
- asp.net-mvc – 由Url.Action生成的Asp.Net MVC错
- asp.net – 防止XSS(跨站脚本)
- asp.net – 将可以为空的int绑定到asp:TextBox
- ASP.Net 4.0中可用的新功能是什么?
- 在asp.net mvc 3中实现FilterAttribute,IActionF
- asp.net – HTML5编辑器从微软词语粘贴时丢失格式
- asp.net – ASP菜单没有显示
- asp.net-mvc – 使用响应流的MVC控制器
- CookieAuthenticationOptions,ExpireTimeSpan不起
热点阅读