Click here to Skip to main content
15,885,132 members
Articles / All Topics

Replacing DTO with Anonymous Object

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
13 Aug 2015CPOL2 min read 11.5K   1   5
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 →

Converting any object to dynamic

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:

http://1drv.ms/1MM4JPA

 

Technologies used

AutoMapper: https://www.nuget.org/packages/AutoMapper

Newtonsoft.Json a JSON Serializer: https://www.nuget.org/packages/Newtonsoft.Json

MainForm

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.

 


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Dev
United Arab Emirates United Arab Emirates
Hussain Naji Al-Safafeer

Work: Software Developer (Assistant manager)
Like: Programming, reading.
Technical site: https://readerman1.wordpress.com

Comments and Discussions

 
QuestionType has been changed Pin
Nelek21-Oct-15 0:42
protectorNelek21-Oct-15 0:42 
AnswerRe: Type has been changed Pin
Reader Man San21-Oct-15 2:28
professionalReader Man San21-Oct-15 2:28 
GeneralRe: Type has been changed Pin
Nelek21-Oct-15 3:59
protectorNelek21-Oct-15 3:59 
GeneralRe: Type has been changed Pin
Sean Ewington21-Oct-15 4:03
staffSean Ewington21-Oct-15 4:03 
GeneralRe: Type has been changed Pin
Nelek21-Oct-15 4:14
protectorNelek21-Oct-15 4:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.