I have an outdated HTTP / XML service that I need to interact with for various functions in my application.
I need to create a wide range of requests for the service, so in order to avoid a lot of magic lines littering around the code, I decided to create xml XElement snippets to create an elementary DSL.
For instance.
Instead...
new XElement("root", new XElement("request", new XElement("messageData", ...)));
I intend to use:
Root( Request( MessageData(...) ) );
With Root, Request, and MessageData (of course, this is for illustrative purposes), which are defined as static methods that all do something like this:
private static XElement Root(params object[] content) { return new XElement("root", content); }
This gives me a pseudo-functional composition style that I like for this kind of task.
My final question is indeed one of sanity / best practices, so it is probably too subjective, however I would appreciate the opportunity to get some feedback independently.
I am going to move these private methods to an open static class so that they are easily accessible for any class that wants to compose a message for the service.
I also intend to have different functions of the service that have their messages created by specific message building classes to improve the service.
Is this a good way to implement this simple DSL, or am I missing some special sauce that will let me do it better?
The thing that makes me doubtful is that as soon as I switch these methods to another class, I increase the length of these method calls (of course, I still keep the original purpose of removing large magical strings of large volume.) Should I more to worry about the size (loc) of the DSL class, than the brevity of the syntax?
Warnings
Please note that in this case, the remote service is poorly implemented and does not comply with any general messaging standards, for example. WSDL, SOAP, XML / RPC, WCF, etc.
In these cases, it would not be wise to create manually created messages.
In rare cases, when you have to deal with a service like the one in question, and it cannot be redesigned for any reason, the answers below provide some possible solutions to this problem.