My name is Carl. I am a software engineer and architect. I blog about C#, .NET, Javascript, AngularJS, nodejs, and more. Thanks for visiting!



Buy me a coffeeBuy me a coffee

Tags

Using C# dynamic Keyword To Replace Data Transfer Objects

This is my blog post for the first C# Advent Calendar. Checkout the other great blog posts on the calendar as well. This is also my first blog post ever so it's a season of firsts! I also encourage you to leave feedback in the comments about what you liked and didn't like and helpful feedback I can use to improve future posts.

For this post I'm going to show 3 examples of using the C# dynamic keyword when deserializing input data instead of using data transfer objects. This post assumes some C# knowledge and experience working with projects that deserialize data from different data sources so I'm going to skip the details to focus on the examples involving the dynamic keyword. The entire solution is available on GitHub.

Many modern applications need to accept data from a variety of sources. Often times we create data transfer objects just to handle requests and responses to and from the data source. I have found when working on applications that consume 3rd party web APIs I could have a few dozen data transfer objects just to handle the requests and responses to and from the API. Using dynamic in C# reduces the need for data transfer objects and keeps the logic that depends on the 3rd party data source contained to one place. This makes the overall code base smaller and easier to maintain. I have also found that many popular libraries that handle deserializing data have strongly typed methods and methods that support the dynamic keyword. I have 3 examples of using the dynamic keyword in this way. The first is processing a CSV file using CSV Helper . The second is deserializing JSON using Newtonsoft Json.NET. Finally, I'll show how to use dynamic as the parameter of a POST request in a web api project.

First I'm going to show an example of parsing a CSV file using the CSV Helper NuGet package. When using a data transfer object the code looks something like this. Read the file and parse it into a data transfer object.


using (StreamReader reader = File.OpenText(filePath))
{
    CsvReader csv = new CsvReader(reader);
    IEnumerable<BookDto> records = csv.GetRecords<BookDto>();
    // Map records to application object...
}

	
The dynamic version is not much different but without the need for the data transfer object, using dynamic instead.

using (StreamReader reader = File.OpenText(filePath))
{
     CsvReader csv = new CsvReader(reader);
     IEnumerable<dynamic> records = csv.GetRecords<dynamic>();
     // Map records to application object...
}

	
The properties of the dynamic object will also have the correct data type, so a string will be a string, an integer will be an integer, etc. This makes it possible to easily map from the dynamic type to a strongly typed application model.

Here is an example of using Json.NET to accomplish the same thing using JSON input. First the one using a data transfer object.

string jsonData = File.ReadAllText(filePath);
List<BookDto> records = JsonConvert.DeserializeObject<List<BookDto>>(jsonData);

	
Then using dynamic. There isn't much code change between the two.

string jsonData = File.ReadAllText(filePath);
List records = JsonConvert.DeserializeObject<List<dynamic>>(jsonData);

	

My final example shows how dynamic can be used as a parameter type to accept arguments in a POST request in a web API project. This prevents the need for request models and can prevent overposting since the app code can explicitly deal with the properties it needs and not only rely on the automatic model binding.

public void Post([FromBody]dynamic requestObject)
{
     Book b = new Book();
     b.Title = requestObject.title;
     b.Author = requestObject.author;
     Debug.WriteLine("Title = " + b.Title);
     Debug.WriteLine("Author = " + b.Author);
}

	

In conclusion, using the C# dynamic keyword can make a code base smaller, easier to maintain, and require less typing. Most libraries that support generic methods also support dynamic versions as well and I showed 3 common examples of that.

That concludes my first ever blog post! I hope someone found it useful or at least interesting. Please leave feedback in the comments on what you liked, what I can improve on, and suggestions for next time. Also, be sure to check out the full code in the GitHub Repository to see complete examples.




If you enjoyed this article...

Buy me a coffeeBuy me a coffee



© 2025 Carl Layton Creative Commons License Disclaimer
An unhandled error has occurred. Reload 🗙