SimpleMvvmToolkit and DevExpress - Serialization errors

Jul 24, 2013 at 12:05 PM
I have a DevExpress DxGrid bound to an ObservableCollection of viewmodels based on SimpleMvvmToolkit's ViewModelDetailBase. The viewmodel has 2 properties exposed by itself (a string and a boolean) and a few other properties are exposed by its base class (ViewModelDetailBase), one of them, or course, is the model behind the viewmodel.

Everytime I use the grid to modify the contents of one of the properties and leave that field, I get an error saying "The type xxx cannot be serialized.." (xxx is the type of the Model) followed by the suggestion to use DataContractAttribute to circomvent the issue.
I am not sure how and where to look for a solution. Maybe I should read up on it, but why is serialization needed here??

Anyway, I hope you can shed a light on this. I'd appreciate some pointers to get me looking in the right direction. A sandbox project that reproduces the issue can be found behind this wetransfer link.


Best regards, ~Rob
Coordinator
Jul 24, 2013 at 8:53 PM
The toolkit clones entities in order to support editability, so that changes can be reversed when an edit is cancelled. To make your entities serializable, it's best to use DataContract on the class and DataMember on each property.
Jul 29, 2013 at 12:33 PM
Thanks for responding!

I've added the DataContract attribute to my viewmodel and datamember'ed each property. However, I'm not sure how to continue from here since the error I now receive is about the ModelBase base class (it cannot be serialized).

Your response is highly appreciated.

Thanks and best regards,
~Rob
Coordinator
Jul 29, 2013 at 6:04 PM
I'll look further into the issue ...
Jul 30, 2013 at 7:09 AM
Edited Jul 30, 2013 at 7:13 AM
Hi Rob,

I had a look at the example you provided. Because the base class clones the object so that it can reverted back if cancelled, for serialisation to occur, you should have a parameter-less constructor in your model.

All I did was added following constructor alongside your constructor in MerkModel.cs and the problem was solved.
        public MerkModel()
        {
            this.merk = new tblMerk();
        }
You DO NOT need to add [DataContract] or [DataMember] attributes to your model and its properties.

Hope this solves your problem.
Jul 30, 2013 at 8:15 AM
Hi Shakyad,

This indeed solves the issue. Thanks a lot! (And it makes sense :-) )

It felt a bit strange having to decorate all my properties with those "datacontract" properties. I would expect that a framework would not impose that kind of extra work. And indeed, it doesn't :)

I'll see if this also solves the issue in my actual project.

Thanks again. Many kudos coming your way.

Best regards,
~Rob
Coordinator
Jul 30, 2013 at 12:50 PM
Thanks Shakyad for locating the issue. Regarding the DataContract attribute, most of the time you can leave it off - providing your class is public and your properties are public as well. Try removing both DataContract and DataMember from your class. It
should work if you a default ctor and no cyclic references.
Jul 30, 2013 at 1:59 PM
tonysneed wrote:
Thanks Shakyad for locating the issue. Regarding the DataContract attribute, most of the time you can leave it off - providing your class is public and your properties are public as well. Try removing both DataContract and DataMember from your class. It
should work if you a default ctor and no cyclic references.
Thanks Tony, I think I am getting close to understanding how your framework works.
I really appreciate the level of support, from both you and the community.

Best regards,
~Rob
Jul 30, 2013 at 2:10 PM
Rob,

This toolkit is great and it has helped me a lot in my project. Hence, I try to help whenever I can. I have had good support from community and Tony when I needed it and now the contributors are just growing.

Good to see you are getting the hang of it. I remember when I understood the messaging technique, I was just so ecstatic about the ways I could use it in my project.

All the best with your project and once again thanks to Tony for the toolkit. Waiting for the next release.