{"id":3124,"date":"2015-04-17T21:12:54","date_gmt":"2015-04-17T21:12:54","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=3124"},"modified":"2015-04-17T21:12:54","modified_gmt":"2015-04-17T21:12:54","slug":"automapper-converters","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/automapper-converters\/","title":{"rendered":"AutoMapper Converters"},"content":{"rendered":"<p>When using AutoMapper, it may be that we&#8217;re using types which map easily to one another, for example if the objects being mapped have the same type for a property or the type can be converted using the standard type convertes, but what happens when things get a little more complex?<\/p>\n<p>Suppose we have a type, such as<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class NameType\r\n{\r\n   public string Name { get; set;\r\n}\r\n<\/pre>\n<p>and we want to map between a string and the NameType, for example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar nameType = Mapper.Map&lt;string, NameType&gt;(&quot;Hello World&quot;);\r\n<\/pre>\n<p>As you might have suspected, this will fail as AutoMapper has no way of understanding how to convert between a string and a NameType.<\/p>\n<p>You&#8217;ll see an error like this<\/p>\n<p><em><br \/>\nMissing type map configuration or unsupported mapping.<\/p>\n<p>Mapping types:<br \/>\nString -> NameType<br \/>\nSystem.String -> AutoMapperTests.Tests.NameType<\/p>\n<p>Destination path:<br \/>\nNameType<\/p>\n<p>Source value:<br \/>\nHello World<br \/>\n<\/em><\/p>\n<p>What we need to do is give AutoMapper a helping hand. One way is to supply a Func to handle the conversion, such as <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nMapper.CreateMap&lt;string, NameType&gt;().\r\n\tConvertUsing(v =&gt; new NameType { Name = v });\r\n<\/pre>\n<p>alternatively we can supply an ITypeConvert implementation, such as<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class NameConverter :\r\n   ITypeConverter&lt;string, NameType&gt;\r\n{\r\n   public NameType Convert(ResolutionContext context)\r\n   {\r\n      return new NameType {Name = (string) context.SourceValue};\r\n   }\r\n}\r\n<\/pre>\n<p>and use in like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nMapper.CreateMap&lt;string, NameType&gt;().\r\n   ConvertUsing(new NameConverter());\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>When using AutoMapper, it may be that we&#8217;re using types which map easily to one another, for example if the objects being mapped have the same type for a property or the type can be converted using the standard type convertes, but what happens when things get a little more complex? Suppose we have a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[24],"tags":[],"class_list":["post-3124","post","type-post","status-publish","format-standard","hentry","category-automapper"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3124","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=3124"}],"version-history":[{"count":1,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3124\/revisions"}],"predecessor-version":[{"id":3125,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3124\/revisions\/3125"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=3124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=3124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=3124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}