meriarith meriarith - 1 year ago 61
Linux Question

How to achieve code reuse between client and server

I have a complicated validation logic that has to be executed in Java/Linux backend and in C# .NET GUI/Windows application, and I would like to be DRY.
The logic could be accessible to both via webmethod on server part, but the validation has to be executed every time user changes the data, so calling webmethod repeatedly is not good enough. What are the ways to solve this problem and is it even possible?

I'm currently not using any scripting languages and if it's possible, I would like to avoid adding any languages into solution.

Currently, I'm considering following ways to go about:

  • It should be possible to write logic in either of the languages and then transform the source file to another language at compile time. This will probably be a class, that implements specific interface, but what about

  • It should be possible to create an unmanaged .dll, and compile it for Linux and Windows, and use JNI/PInvoke to call a method, but I'm not competent enough in creating unmanaged .dll's for different platforms, and it's most likely going to be C or C++, which is something I'd like to refrain from (adding new languages).

  • Please, advice.

    Answer Source

    What I would do: design a declarative language to describe the constraints to be apply on validation, implement a parser for it and read a description file in a in-memory model, implement a source code generator based in the model.

    If you think you can describe those restrictions as JSON-formatted-text, you can:

    1. design at most a JSON schema for the step 1 - this only if you want to be absolutely pedantic, otherwise a simple description/spec of the JSON structures should suffice
    2. skip the parser implementation and use an already JSON->plain-objects binding library (in the Java world, gson springs into one's mind): coding your plain-objects to represent the in-memory model may very well act as the specification at point 1
    3. use a templating package to generate the language specific source from the in-memory model (again in the Java world, I had good experiences with Apache's Velocity )

    But then again, that would be my preference: I did similar things 3-4 times in the recent past, so that I'm feeling at large with the approach (and aware of the drawbacks of 'source code generation' - the major one: whatever custom refinements on the generated code need to be applied, they are in danger of being overwritten at the next generation: better generate the code as classes and derive from them to customize the behaviour).

    Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download