Replacing DTO with Anonymous object

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 architect of a system, much more simpler, as most DTOs are removed.

this article can be seen in 

http://www.codeproject.com/Articles/1018716/Replacing-DTO-with-Anonymous-object

 .

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

Download the code from here: http://1drv.ms/1MM4JPA

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 anonymous objects in their parameters/return, wondering if its sufficient 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 anonymous?

 .

History

V1.0 creation of the article

 

 

About Reader Man حسين ناجي الصفافير

حسين ناجي الصفافير Hussain Naji Al-Safafeer Info: https://readerman1.wordpress.com/aboutme
This entry was posted in Development and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s