{"id":42,"date":"2009-09-18T14:44:03","date_gmt":"2009-09-18T12:44:03","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=42"},"modified":"2009-09-18T14:44:03","modified_gmt":"2009-09-18T12:44:03","slug":"assemblers-with-convertall","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/assemblers-with-convertall","title":{"rendered":"Assemblers with ConvertAll"},"content":{"rendered":"<p>In most applications we have some kind of <strong>assembler classes<\/strong>, that create models <strong>used in the presentation layer<\/strong>:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class EntityComboModelAssembler\n{\n   public List&lt;comboBoxModel&gt; CreateComboBoxModels(IList&lt;entity&gt; entities)\n   {\n      List&lt;comboBoxModel&gt; list = new List&lt;comboBoxModel&gt;();\n      foreach (Entityentity in entities)\n      {\n         list.Add(new ComboBoxModel(entity, entity.Name));\n      }\n      return list;\n   }\n}\n<\/pre>\n<p>It is possible to write this code in 1 (one) line of code:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class EntityComboModelAssembler\n{\n   public List&lt;comboBoxModel&gt; CreateComboBoxModels(IList&lt;entity&gt; entities)\n   {\n      return entities.AsList().ConvertAll(x =&gt; new ComboBoxModel(x, x.Name));\n   }\n}\n<\/pre>\n<p>We can also remove <em>AsList<\/em> method by using extension method that adds <em>ConvertAll <\/em>method to <em>IList <\/em>interface:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic static class IListExtensions\n{\n   public static List&lt;tout&gt; ConvertAll&lt;tin, TOut&gt;(this IList&lt;tin&gt; source, Converter&lt;tin, TOut&gt; converter)\n   {\n      return source.ToList().ConvertAll(converter);\n   }\n}\n<\/pre>\n<p>And the final code:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class EntityComboModelAssembler\n{\n   public List&lt;comboBoxModel&gt; CreateComboBoxModels(IList&lt;entity&gt; entities)\n   {\n      return entities.ConvertAll(x =&gt; new ComboBoxModel(x, x.Name));\n   }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In most applications we have some kind of assembler classes, that create models used in the presentation layer: public class EntityComboModelAssembler { public List&lt;comboBoxModel&gt; CreateComboBoxModels(IList&lt;entity&gt; entities) { List&lt;comboBoxModel&gt; list = new List&lt;comboBoxModel&gt;(); foreach (Entityentity in entities) { list.Add(new ComboBoxModel(entity, entity.Name)); } return list; } } It is possible to write this code in 1 (one) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[15],"class_list":["post-42","post","type-post","status-publish","format-standard","hentry","category-programming","tag-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/42"}],"collection":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/comments?post=42"}],"version-history":[{"count":0,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/42\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=42"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=42"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=42"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}