Using dynamic to quickly create Xml

Not so long ago, I developed a functional that forms a diverse xml according to some algorithm.
It is logical that when writing the first test, the traditional question of a lazy programmer arose: how to save yourself energy, and who read the nerves and quickly and clearly imagine what I want to get?
Therefore, I came up with such a way:

Example


dynamic rootTag = new XmlBuilder("Root", _nmsps);
dynamic modelTag = new XmlBuilder("Model", _nmsps);
dynamic elementTag = new XmlBuilder("ElementRef", _nmsps);
dynamic attrTag = new XmlBuilder("ElementAttribute", _nmsps);
 XDocument doc =
                rootTag(id: Guid.Empty,
                model: modelTag(id: model.ObjectId, name: model.Name,
                                     e1: elementTag(id: er.ObjectId, name: elem1.Name, 
                                           ea1: attrTag(id: attr1.ObjectId, name: attr1.Name),
                                           ea2: attrTag(id: attr2.ObjectId, name: attr2.Name)),
                                     e2: elementTag(id: er2.ObjectId, name: elem2.Name)));

XmlBuilder is a self-written class that inherits from DynamicObject.
To create a document, it is called as a method and converts the arguments either into attributes based on the parameter name - if it is an argument of any type except XmlBuilder - or into child elements (here the parameter name is not important) - if it is XmlBuilder.
In addition, each XmlBuilder represents a tag in the resulting document.

In the test, we compare what we need with what happened using a simple comparator, as a result, this test looks something like this:


//формируем входные данные для теста
//...
//формируем документ, которому должен соответствовать результат
dynamic rootTag = new XmlBuilder("Root", _nmsps);
dynamic modelTag = new XmlBuilder("Model", _nmsps);
dynamic elementTag = new XmlBuilder("ElementRef", _nmsps);
dynamic attrTag = new XmlBuilder("ElementAttribute", _nmsps);
 XDocument targetDocument =
                rootTag(id: Guid.Empty,
                model: modelTag(id: model.ObjectId, name: model.Name,
                                     e1: elementTag(id: er.ObjectId, name: elem1.Name, 
                                           ea1: attrTag(id: attr1.ObjectId, name: attr1.Name),
                                           ea2: attrTag(id: attr2.ObjectId, name: attr2.Name)),
                                     e2: elementTag(id: er2.ObjectId, name: elem2.Name)));
//выполняем
var resultDocument = TestableMethod();
//проверяем
var _comparer = new XDocComparer();
Assert.IsTrue(_comparer.Equals(targetDocument, resultDocument));


For simplicity, I had to sacrifice unnecessary features for me (declarations, for example).
Well, the comparator, due to the fact that it is IEqualityComparer, does not show what exactly does not match, but only the final result.

Nevertheless, the implementation of XmlBuilder is here , and the implementation of XDocComparer is here , I ask you to use it if someone can come in handy.


Also popular now: