How to separate deserialization attributes from my model classes?

I am currently developing an application that relies heavily on the .NET serializer to convert between objects and XML. It works fine, but inserts serialization / deserialization attributes directly into my model cable, as a pretty poor design choice.

Is it possible to somehow share these attributes with the class itself? An example of what I would like to achieve can be seen here.

Thank you in advance for a great day.

+6
source share
1 answer

Unfortunately, the answer to this question is not as simple and understandable as you can hope for. Serializers sometimes need tips on how to match the textual representation of your data with the representation of conceptual objects (and vice versa). This most often refers to XML than JSON, because it is more structured (elements, attributes, namespaces, schema, etc.). You probably refer to the example of creating an EF model that you specified, not for serialization, but for a mapping / relational schema that is very different from XML serialization.

Even tools like JSON.NET have the attributes that are needed when the names of your serialized members don't quite match the properties of your object, and you don't want to write your own converter.

If attribute pollution really bothers you, you can introduce another layer between the models and the XML. You can then have types that contain attributes and serve the sole purpose of serializing in and out of XML, and then use a tool like AutoMapper or ValueInjecter to convert from this layer to your model layer.

I also don’t always like attributes that pollute my types, for example, with MVC model validation attributes and especially for providing EF tips on how my entity model compares with the relational schema. However, this is one example where I think it might be appropriate because you get a lot from it with a fairly minimal amount of code.

There seems to be at least one free XML serialization tool, but I'm not sure how good it is:

https://fluentlyxml.codeplex.com/

http://trycatchfail.com/blog/post/fluent-xml-serializatione28093introduction.aspx

+3
source

Source: https://habr.com/ru/post/981922/


All Articles