top of page
Writer's pictureThe Tech Platform

Dynamic Data Transfer Object in .Net Core

In c# to assemble property values from source to destination can be achieved by mapping source DTO properties to destination DTO class properties.

EorderRequest.cs (source poco class)


OrderResponse.cs (Destination poco class)


EorderRequest.cs is source class DTO contains order, product, customer ,payment details that customer has placed to buy products from ecommerce site .

OrderResponse.cs is destination class DTO contains shipping , order, product, customer ,payment details that customer receives after order processed.

OrderResponse.cs and EorderRequest.cs DTOs are having common properties as order, product, customer class.

To avoid duplicate properties in OrderResponse.cs class , we can take advantage of reflection in .Net to inspect or retrieve metadata of a type at runtime.

using reflection (system.Reflection) , GetProperty(string property) method can be used to generate destination DTO class properties that are public property in source class.

OrderResponse.cs using reflection


OrderResponse DTO class inherits EorderRequest class and using reflection GetProperty(property) method is used to generate public properties from EorderRequest.cs (source)in OrderResponse.cs (destination)during runtime.


OrderProcessor.cs


OrderProcessor class will process order and based on product availability , order ship date and delivery date is decided. orderResponse method will assemble source and destination properties. Here OrderResponse class (destination class) contains public properties that got generated during runtime from EorderRequest class using .Net Reflection.



Source: Medium - Karthik


The Tech Platform

0 comments

Comments


bottom of page