{"id":10,"date":"2015-05-16T09:29:00","date_gmt":"2015-05-16T09:29:00","guid":{"rendered":""},"modified":"2016-01-30T22:42:17","modified_gmt":"2016-01-30T22:42:17","slug":"c-dynamic-type-can-be-useful","status":"publish","type":"post","link":"https:\/\/tpodolak.com\/blog\/2015\/05\/16\/c-dynamic-type-can-be-useful\/","title":{"rendered":"C# &#8211; dynamic type can be useful"},"content":{"rendered":"<p>I am not a big fan of <i>dynamic<\/i> type in C#, however there are certain situations in which it can be handy. Let&#8217;s assume that we have following piece of code (which I encounter quite often at my work)<\/p>\n<pre lang=\"csharp\">\r\npublic abstract class Weapon\r\n{\r\n    \/\/some properties\r\n}\r\n\r\npublic class Gun : Weapon\r\n{\r\n     \/\/some properties\r\n}\r\n\r\n\/\/ more derived types\r\n\r\nvar weaponService = new WeaponService();\r\nIList<TransferObjects.Weapon> data = weaponService.GetWeapons();\r\nList<Models.Weapon> weapons = data.Select(WeaponConverter.Convert).ToList();\r\n<\/pre>\n<p>As you can see we have to convert <i>DTO<\/i> objects into &#8220;<i>normal<\/i>&#8221; objects using some kind of converter. This is not a problematic when you can use some auto-mappings libraries (<i>Automapper<\/i>,<i>FastMapper<\/i>) however I am not allowed to do that in a company I work for. So the question is how to write <i>WeaponConverter.Convert<\/i> method in some elegant way. The obvious way (but definitely not elegant) is just to use lots of &#8220;<i>if<\/i>&#8221; statements<\/p>\n<pre lang=\"csharp\">\r\npublic static Models.Weapon ConvertOld(TransferObjects.Weapon weapon)\r\n{\r\n    if (weapon == null)\r\n        return null;\r\n        \r\n    if (weapon is TransferObjects.Gun)\r\n        return ConvertInternal((TransferObjects.Gun) weapon);\r\n        \r\n    if (weapon is TransferObjects.Sword)\r\n        return ConvertInternal((TransferObjects.Sword) weapon);\r\n        \r\n    throw new ArgumentException(\"Unknown weapon\", nameof(weapon));\r\n}\r\n\/\/ ConvertInternal implementation goes here for every derived type\r\n<\/pre>\n<p>But this is really ugly, much better idea (at least from my point of view) is to leverage <i>dynamic<\/i> keyword introduced in <i>.NET 4<\/i>. Thanks to it our <i>Convert<\/i> method can be reduced to this<\/p>\n<pre lang=\"csharp\">\r\npublic static Models.Weapon Convert(TransferObjects.Weapon weapon)\r\n{\r\n    return weapon != null ? ConvertInternal((dynamic)weapon) : null;\r\n}\r\n\r\n\/\/ ConvertInternal implementation goes here for every derived type\r\n<\/pre>\n<p>Source code for this post can be found <a href=\"https:\/\/github.com\/tpodolak\/Blog\/tree\/master\/DynamicCanBeUseful\">here<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am not a big fan of dynamic type in C#, however there are certain situations in which it can be handy. Let&#8217;s assume that we have following piece of code (which I encounter quite often at my work) public abstract class Weapon { \/\/some properties } public class Gun : Weapon { \/\/some properties [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,16],"tags":[162,159,171],"class_list":["post-10","post","type-post","status-publish","format-standard","hentry","category-c","category-dynamic","tag-net-4-0","tag-c","tag-dynamic"],"_links":{"self":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/10","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/comments?post=10"}],"version-history":[{"count":4,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/10\/revisions"}],"predecessor-version":[{"id":499,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/10\/revisions\/499"}],"wp:attachment":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/media?parent=10"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/categories?post=10"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/tags?post=10"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}