{"id":3121,"date":"2015-04-17T20:46:29","date_gmt":"2015-04-17T20:46:29","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=3121"},"modified":"2015-04-17T20:46:29","modified_gmt":"2015-04-17T20:46:29","slug":"automapper-profiles","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/automapper-profiles\/","title":{"rendered":"AutoMapper Profiles"},"content":{"rendered":"<p>When creating mappings for AutoMapper we can easily end up with a mass of CreateMap methods in  the format<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nMapper.CreateMap&lt;string, NameType&gt;();\r\nMapper.CreateMap&lt;NameType, string&gt;();\r\n<\/pre>\n<p>An alternate way of partitioning the various map creation methods is using the AutoMapper <em>Profile<\/em> class.<\/p>\n<p>Instead we can create profiles with as fine or coarse granularity as you like, here&#8217;s an example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class NameTypeProfile : Profile\r\n{\r\n   protected override void Configure()\r\n   {\r\n      CreateMap&lt;string, NameType&gt;()\r\n      CreateMap&lt;NameType, string&gt;();\r\n   }\r\n}\r\n<\/pre>\n<p>To register the profiles we need to then use<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nMapper.AddProfile(new NameTypeProfile());\r\n<\/pre>\n<p>which can also become a little tedious, but there&#8217;s an alternative to this&#8230;<\/p>\n<p><strong>AutoAutoMapper Alternative<\/strong><\/p>\n<p>So instead of writing the code to add each profile we can use the AutoAutoMapper in the following way<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nAutoAutoMapper.AutoProfiler.RegisterProfiles();\r\n<\/pre>\n<p>this will find the profiles within the current assembly or those supplied as params arguments to the RegisterProfiles method for us. This way the profiles are registered for you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When creating mappings for AutoMapper we can easily end up with a mass of CreateMap methods in the format Mapper.CreateMap&lt;string, NameType&gt;(); Mapper.CreateMap&lt;NameType, string&gt;(); An alternate way of partitioning the various map creation methods is using the AutoMapper Profile class. Instead we can create profiles with as fine or coarse granularity as you like, here&#8217;s an [&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-3121","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\/3121","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=3121"}],"version-history":[{"count":2,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3121\/revisions"}],"predecessor-version":[{"id":3137,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3121\/revisions\/3137"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=3121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=3121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=3121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}