{"id":11479,"date":"2015-09-28T07:55:00","date_gmt":"2015-09-28T07:55:00","guid":{"rendered":"https:\/\/viewmyprojects.com\/winwirewp\/?p=11479"},"modified":"2023-11-30T05:50:39","modified_gmt":"2023-11-30T05:50:39","slug":"how-to-assign-values-from-one-object-to-another-dynamically-in-c","status":"publish","type":"post","link":"https:\/\/viewmyprojects.com\/winwirewp\/blog\/how-to-assign-values-from-one-object-to-another-dynamically-in-c\/","title":{"rendered":"How to Assign Values from One Object to Another Dynamically in C#"},"content":{"rendered":"\n<p>In many applications, there might be a situation where it is required to map different types of objects. Many a times applications don\u2019t expose the actual database entity to the world. It always prefers to give the Data Transfer Object (DTO) rather than the actual object. Also while writing some business logic there may be a need to move data from temp tables to actual tables. In these situations, Automapper gives opportunity to avoid writing dirty code (object to object mapping).<\/p>\n\n\n\n<p>\u2022 Install \u201c<strong>AutoMapper<\/strong>\u201d using package manager console.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"425\" height=\"50\" src=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/vam.webp\" alt=\"\" class=\"wp-image-18596\" srcset=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/vam.webp 425w, https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/vam-300x35.webp 300w\" sizes=\"auto, (max-width: 425px) 100vw, 425px\" \/><\/figure><\/div>\n\n\n<p><strong>Objects with Same Properties:<br><\/strong><br>1. Below are&nbsp;<strong>two classes<\/strong>&nbsp;one actual and the other a DTO class.<\/p>\n\n\n\n<p><em>public class Order<\/em><br><em>{<\/em><br><em>public string&nbsp;<strong>UserName&nbsp;<\/strong>{ get; set; }<\/em><br><em>public string&nbsp;<strong>Item&nbsp;<\/strong>{ get; set; }<\/em><br><em>public decimal&nbsp;<strong>Price&nbsp;<\/strong>{ get; set; }<\/em><br><em>public int&nbsp;<strong>Quantity&nbsp;<\/strong>{ get; set; }<\/em><br><em>}<\/em><\/p>\n\n\n\n<p><em>public class OrderDTO<\/em><br><em>{<\/em><br><em>public string&nbsp;<strong>Item&nbsp;<\/strong>{ get; set; }<\/em><br><em>public string&nbsp;<strong>UserName&nbsp;<\/strong>{ get; set; }<\/em><br><em>public decimal&nbsp;<strong>Price&nbsp;<\/strong>{ get; set; }<\/em><br><em>public int&nbsp;<strong>Quantity&nbsp;<\/strong>{ get; set; }<\/em><br><em>}<\/em><\/p>\n\n\n\n<p>2. Automapper requires to configure the mapping of the objects before it can be used<\/p>\n\n\n\n<p>For example, in this sample application where the conversion of Order object to Order DTO is being done, first configure the same in starting of the application or before calling the map method.<\/p>\n\n\n\n<p><em>Mapper.CreateMap&lt;Order, OrderDTO&gt;();<\/em><\/p>\n\n\n\n<p>3. Create an Order object with static data (In real time this data might come from database).<br><em>Order orderObj = new Order()<\/em><br><em>{<\/em><br><em>Item = \u201cItem name\u201d,<\/em><br><em>Price = 5,<\/em><br><em>Quantity = 1,<\/em><br><em>UserName = \u201cUsername\u201d<\/em><br><em>};<\/em><\/p>\n\n\n\n<p>4. Call the map method to get the converted object as shown below.<br><em>OrderDTO _orderDTO = Mapper.Map&lt;Order, OrderDTO&gt;(orderObj);<\/em><\/p>\n\n\n\n<p>5. Automapper returns the mapped object.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"617\" height=\"257\" src=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/automapper-big.webp\" alt=\"\" class=\"wp-image-18598\" srcset=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/automapper-big.webp 617w, https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/automapper-big-300x125.webp 300w\" sizes=\"auto, (max-width: 617px) 100vw, 617px\" \/><\/figure><\/div>\n\n\n<p><strong>Objects with Different Properties:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Generally, there may be a case where one object is present as a property in another object. While exposing DTO\u2019s, preferably it\u2019s a good practice to give all the required properties in one object rather than in many objects.<\/li>\n<\/ol>\n\n\n\n<p><strong>For example<\/strong>&nbsp;order object contains the user object and that contains the name of the user.&nbsp;<em>Here are the objects:<\/em><\/p>\n\n\n\n<p><em>publicclassUser<\/em><\/p>\n\n\n\n<p><em>{<\/em><\/p>\n\n\n\n<p><em>publicstring Name { get; set; }<\/em><\/p>\n\n\n\n<p><em>}<\/em><\/p>\n\n\n\n<p><em>publicclassOrder<\/em><\/p>\n\n\n\n<p><em>{<\/em><\/p>\n\n\n\n<p><em>publicUser User { get; set; }<\/em><\/p>\n\n\n\n<p><em>publicstring Item { get; set; }<\/em><\/p>\n\n\n\n<p><em>publicdecimal Price { get; set; }<\/em><\/p>\n\n\n\n<p><em>publicint Quantity { get; set; }<\/em><\/p>\n\n\n\n<p><em>}<\/em><\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li>Update the sample code as mentioned below and run the application. The&nbsp;<strong>UserName &nbsp;<\/strong>in the&nbsp;<strong>DTO object<\/strong>&nbsp;is assigned with the value of the entity. This is because the entityname concatenated with the propertyname (entityname + propertyname) matches the DTO property name.<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"581\" height=\"284\" src=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/automapper-small.webp\" alt=\"\" class=\"wp-image-18600\" srcset=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/automapper-small.webp 581w, https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/automapper-small-300x147.webp 300w\" sizes=\"auto, (max-width: 581px) 100vw, 581px\" \/><\/figure><\/div>","protected":false},"excerpt":{"rendered":"<p>In many applications, there might be a situation where it is required to map different types of objects. Many a times applications don\u2019t expose the actual database entity to the world. It always prefers to give the Data Transfer Object (DTO) rather than the actual object. Also while writing some business logic there may be&hellip; <a class=\"more-link\" href=\"https:\/\/viewmyprojects.com\/winwirewp\/blog\/how-to-assign-values-from-one-object-to-another-dynamically-in-c\/\">Continue reading <span class=\"screen-reader-text\">How to Assign Values from One Object to Another Dynamically in C#<\/span><\/a><\/p>\n","protected":false},"author":45,"featured_media":16779,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_eb_attr":"","_uag_custom_page_level_css":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-11479","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","entry"],"acf":[],"featured_image_src":"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/dynamically-c.webp","author_info":{"display_name":"Vamshi","author_link":"https:\/\/viewmyprojects.com\/winwirewp\/author\/vamshi\/"},"views":4340,"uagb_featured_image_src":{"full":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/dynamically-c.webp",800,440,false],"thumbnail":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/dynamically-c-150x150.webp",150,150,true],"medium":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/dynamically-c-300x165.webp",300,165,true],"medium_large":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/dynamically-c-768x422.webp",750,412,true],"large":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/dynamically-c.webp",750,413,false],"1536x1536":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/dynamically-c.webp",800,440,false],"2048x2048":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/dynamically-c.webp",800,440,false],"post-thumbnail":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/dynamically-c.webp",800,440,false]},"uagb_author_info":{"display_name":"Vamshi","author_link":"https:\/\/viewmyprojects.com\/winwirewp\/author\/vamshi\/"},"uagb_comment_info":0,"uagb_excerpt":"In many applications, there might be a situation where it is required to map different types of objects. Many a times applications don\u2019t expose the actual database entity to the world. It always prefers to give the Data Transfer Object (DTO) rather than the actual object. Also while writing some business logic there may be&hellip;&hellip;","_links":{"self":[{"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/posts\/11479","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/users\/45"}],"replies":[{"embeddable":true,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/comments?post=11479"}],"version-history":[{"count":2,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/posts\/11479\/revisions"}],"predecessor-version":[{"id":18601,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/posts\/11479\/revisions\/18601"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/media\/16779"}],"wp:attachment":[{"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/media?parent=11479"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/categories?post=11479"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/tags?post=11479"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}