How do I get to writing a provider like F # that uses a complex scheme?

Most recently, I worked with some data for traffic and travel information, that is, data in Datex2 format. The project was short-lived and ended already, and I continued, as usual, and created a bunch of strongly typed C # classes with the xsd.exe tool, did serialization, light processing, and so on. However, now, looking back, I began to wonder if this was a good example for a provider like F #, and therefore I would take the first blow on this issue.

With this in mind, how should one approach a situation where there is a complex scheme that should not change often? Since there is no public type provider that would directly infer types from the schema, I think the parameters are:

  • Use an XML type provider .
  • Generate types with the external tool xsd.exe in this case - taking a cue from the WSDL type provider (which uses svcutil.exe ).
  • Scan the types manually (possibly by changing the output of xsd.exe ).
  • Generate types (as in the previous release) / use an XML type provider and perform continuous serialization, deserialization, and schema validation in the background.

Then I also started to wonder about the history of C # -F # (for example, generated or erased types), and what if I wanted to change the types in order to better check the constraints like <xs:element name="ilc" type="D2LogicalModel:TpegIlcPointDescriptor" maxOccurs="3"> in the schema, and provide a good developer experience.

Rolling your own types looks like a rather time-consuming end, and the last two points seem to be the most attractive, therefore, following the description below, nos in another SO post. I used System.Xml and System.CodeDom and modified the code to use Microsoft.FSharp.Compiler.CodeDom and FSharpCodeProvider to generate F # types.

Alas! The generated F # code does not compile (even after adding the appropriate references, etc.). At this moment, I thought that I could ask several directions.

Question: Is there a recommended, experienced way to create a type provider to conform to a somewhat complex XML scheme (taking into account Datex2 as an example), if I would like to use the restrictions as described in this scheme as early as possible in the development cycle?

<edit 2013-12-10: Rune FS is trying to hit this issue, see its SO question. Getting a compilation error in the provided type .

+6
source share
1 answer

This is a pretty tricky question, and I think there is no easy answer - I think you probably listed all the options, as well as most of their compromises. For one-time projects, there really is no point in creating a provider of a certain type for just one purpose, so I think that using an XML provider or code generation is the only choice. Code generation (when changing the generated code) is a maintenance nightmare.

As for the XML type provider, we have an element to add support for XSD (this would be a good community contribution, as it is pretty autonomous), so if we had this, then I believe that using the XML type provider was would be ideal because you could just pass it the DATEX II circuit.

Currently, F # Data uses erasable types (bad for C # interop), but we are really considering switching to generated types, which would make the provider suitable for use with C # (through a small F # project), so I think that type providers are the answer here, but they may need some improvements before they are perfect for your project (but then F # Data is an open source project, and we always welcome contributions :-)).

+5
source

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


All Articles