DataContractSerializer objects conversion performace improvement
-
You should consider using Fasterflect[^] or Automapper[^]. Your current implementation is hugely malperformant. [Edit] And you're really mapping between objects, not converting types, right? [/Edit] /ravi
My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
Object converting - right. The problem is that i want to convert objects without knowing its members - it should be done using their names (one property data in object one is copied to object two's property which has the same name). using serialization i can serialize one object from one module to an object of another module easily. I was looking for a way to serialize from one object to another without using a memory stream in the middle - that will solve my problem.
-
Object converting - right. The problem is that i want to convert objects without knowing its members - it should be done using their names (one property data in object one is copied to object two's property which has the same name). using serialization i can serialize one object from one module to an object of another module easily. I was looking for a way to serialize from one object to another without using a memory stream in the middle - that will solve my problem.
impeham wrote:
it should be done using their names
That's exactly what I thought you wanted to do. You're mapping (not converting) objects. Use Automapper instead of serialization. It's orders of magnitude faster. /ravi
My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
-
impeham wrote:
it should be done using their names
That's exactly what I thought you wanted to do. You're mapping (not converting) objects. Use Automapper instead of serialization. It's orders of magnitude faster. /ravi
My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
-
impeham wrote:
it should be done using their names
That's exactly what I thought you wanted to do. You're mapping (not converting) objects. Use Automapper instead of serialization. It's orders of magnitude faster. /ravi
My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
After checking AutoMapper i realized that i need to specifically map all internal types of the objects from source to target. this requires as my objects can contain many types and it will require a LOT of work (not to mention maintenance as they change) before it can be used and i need something to do the conversions automatically with minimal work => leads me to my first posted code. So i return to the original question - is it possible to skip the MemoryStream read/write?
-
After checking AutoMapper i realized that i need to specifically map all internal types of the objects from source to target. this requires as my objects can contain many types and it will require a LOT of work (not to mention maintenance as they change) before it can be used and i need something to do the conversions automatically with minimal work => leads me to my first posted code. So i return to the original question - is it possible to skip the MemoryStream read/write?
impeham wrote:
is it possible to skip the MemoryStream read/write?
Only if you write mappings for each internal object. My philosophy is to build
Clone()
methods for each object (class), and call them for nested objects as required. ImplementingClone()
is easily done by calling .NET'sMemberwiseClone()
for most properties. You will find the effort required to do this small in comparison to the run-time performance gain. /raviMy new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
-
impeham wrote:
is it possible to skip the MemoryStream read/write?
Only if you write mappings for each internal object. My philosophy is to build
Clone()
methods for each object (class), and call them for nested objects as required. ImplementingClone()
is easily done by calling .NET'sMemberwiseClone()
for most properties. You will find the effort required to do this small in comparison to the run-time performance gain. /raviMy new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
-
That will take a lot of work for implementation & maintenance afterwards which i am trying to avoid. I will have to think about this more. Thanks.
If you have a generalized object graph and a need to map any type of object to any other type, I would like to suggest that you revisit your design. Object mapping is mostly used when converting DTOs to business objects, and frameworks like Automapper and Fasterflect prove to be very convenient (and fast) in these cases. If you choose to go with your existing design, you may want to consider
XmlSerializer
which caches serializers for different types, causing a one-time performance hit on first convert. But this is also not as performant as simple mapping. Cheers, /raviMy new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
-
If you have a generalized object graph and a need to map any type of object to any other type, I would like to suggest that you revisit your design. Object mapping is mostly used when converting DTOs to business objects, and frameworks like Automapper and Fasterflect prove to be very convenient (and fast) in these cases. If you choose to go with your existing design, you may want to consider
XmlSerializer
which caches serializers for different types, causing a one-time performance hit on first convert. But this is also not as performant as simple mapping. Cheers, /raviMy new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
-
Unfortunately, i cannot add the XmlInclude attribute to my types, so using XmlSerializer won't work.
-