vendredi 2 octobre 2015

Dealing with legacy functions, how to prepare new interface which would adapt legacy functions

Use Case

  1. Assume that there is one Service Implemented in an multilayered .net app.

  2. Some ViewModels use this Service since it exposes functions they requires.

  3. This application has also possibility to run scripts executed by old legacy ScriptEngine (details are not imporant here).

  4. Above LegacyScriptEngine uses our Service

  5. Recently I added IronPython Engine to my application and would like to expose same functions as my old engine exposed.

  6. I was wondering how to prepare it to accomplish clean code.
  7. My solution should be flexible, e.g. in case another ScriptEngine used it could be used too.

    public class LegacyScriptEngine { Service _service;

    public LegacyScriptEngine(Service service)
    {
       _service = service;
    }
    
    Func1(int param)
    {
        _servcie.SomeFunc(param)
    }
    
    Func2(int param1, int param2, int param3, int param4)
    {
        //collect params and create someObject
        _servcie.SomeFunc(someObject);
    }
    
    //more functions...
    
    

    }

    public class PythonEngine { //Exposing same functions as old engine does PythonEngine(BigService servcie) { _mainScope.SetVariable("host_legacy", servcie); }
    }

Conclusions:

  • In my opinion IronPython engine should not know anything about LegacyEngine at all.
  • I should not expose my whole service since I would like use has only access to same functions as old engine allowed in order to cusomers could easily convert old scripts into new ones.
  • I thought to prepare an interface with same functions heading as old legacy service uses. e.g:

    public inteface LegacyFunctions { Func1(int someparams) { servcie.SomeFunc(someparams); }

    Func2(int param1, int param2, int param3, int param4)
    {
        //collect params and create someObject
        _servcie.SomeFunc(someObject);
    }
    
    

    }

  • Then I could prepare some Adapter which implements my interface and exposes it to python.

    public class ServcieToLegacyFunctionsAdapter
    {
        ServcieToLegacyFunctionsAdapter(BigService servcie)
        {
            _servcie = servcie;
        }
    
        Func1(someparams)
        {
            servcie.SomeFunc(someparams);
        }
    
        Func2(int param1, int param2, int param3, int param4)
        {
            //collect params and create someObject
            _servcie.SomeFunc(someObject);
        }
    }
    
    

What do you think about such solution, do you have any conclusions?

Aucun commentaire:

Enregistrer un commentaire