Replacing DTO with Anonymous Object





4.00/5 (1 vote)
Introduction Data transfer object (DTO) are used to pass data from layer to another instead of using the domain objects, which has very good implications if the layers are separated by wire as in server and client scenario. The problem … Continue reading →
Introduction
Data transfer object (DTO) are used to pass data from layer to another instead of using the domain objects, which has very good implications if the layers are separated by wire as in server and client scenario.
The problem with this, is creating huge number of non behavioral classes for each domain class, with only one main benefit “passing data”!
My suggestion is to use anonymous objects instead, and when need arise, they can be mapped again to their original domain class, or JSON/Reflection is used to get internal data.
This might make the design and archetect of a system, much more simpler, as most DTOs are removed.
Download the code from here:
Technologies used
AutoMapper: https://www.nuget.org/packages/AutoMapper
Newtonsoft.Json a JSON Serializer: https://www.nuget.org/packages/Newtonsoft.Json
Using the code
The attached solution has two projects:
anotherProject.vbproj: this will show that passing anonymous objects from different assemblies doesn’t matter.
testPassingAnonymousAsDTO.vbproj: the main project, which will show the user, its functionality.
Passing anonymous object from different assembly
This will get an anonymous object from another assembly and then map it to the defined class “Person”, and show its content.
Private Sub testAnonymousObjectMapping() Dim otherProject As New ClassFromOtherProject Dim AnonymousPerson = otherProject.getAnonymous() Dim oPerson = Mapper.DynamicMap(Of Person)(AnonymousPerson) txtMsg.Text = "" Dim sb As New System.Text.StringBuilder("") sb.Append("ID=" & oPerson.ID & vbTab) sb.Append(", Name=" & oPerson.Name & vbTab) sb.Append(", Country=" & oPerson.Country & vbTab) sb.Append(", City=" & oPerson.City) sb.Append(vbCrLf) txtMsg.Text = sb.ToString End Sub
Test Linq Projection
This code will test linq projection by making a linq query on a list, and projecting a new anonymous type, then passing that list as IEnumerable, to another function to show its content.
Private Sub testLinqProjection() Dim peopleList = getPeople() Dim peoplePropertiesSubset = From person In peopleList Select New With {.Name = person.Name, .City = person.City} showLinq(peoplePropertiesSubset) End Sub
Test Linq Projection from another Project
This function will create an anonymous object from a class that is in another assembly, and then show its content.
Private Sub testLinqProjectionFromAnotherProject() Dim otherProject As New ClassFromOtherProject Dim peopleList = otherProject.getAnonymousPeople() showLinq(peopleList) End Sub
Mapping from anonymous object to Person object
This function will convert the passed anonymous object to the defined class “Person”, using AutoMapper.
Sub showLinq(linqList As IEnumerable(Of Object)) Dim peopleList = From obj In linqList Select Mapper.DynamicMap(Of Person)(obj) txtMsg.Text = "" Dim sb As New System.Text.StringBuilder("") For Each oPerson In peopleList sb.Append("ID=" & oPerson.ID & vbTab) sb.Append(", Name=" & oPerson.Name & vbTab) sb.Append(", Country=" & oPerson.Country & vbTab) sb.Append(", City=" & oPerson.City) sb.Append(vbCrLf) Next txtMsg.Text = sb.ToString End Sub
Serializing Anonymous Object
This will take an anonymous object and serialize it, using JSON converter “Newtonsoft.Json”
Function serializeToJSON(anonymous_ As Object) As String Return JsonConvert.SerializeObject(anonymous_, Formatting.Indented) End Function
De-Serializing Anonymous Object
This will take a JSON string and convert it to an anonymous object
Function deSerializeToJSON(Of t)(JSON_ As String) As Object Return JObject.Parse(JSON_).ToObject(Of t) End Function
Concerns
Comments/Ideas will be appreciated:
Maybe one of my concerns is the readability of functions that will have anonymouse objects in their parameters/return, wondering if its suffecient to write good names for parameters, and increasing the documentation of the functions, to note all the properties that are going to be listed in the anonymouse?
History
V1.1 Added the code download link.
V1.0 creation of the article.