Log in

Calling your own c - sharp .net things from Turnkey–server side

From MDrivenWiki

As MDriven Turnkey works on your defined model and information you will have access to everything you need – until you don’t.

For example we had these use-cases that called for the ability to reach out from the standard turnkey implementation:

  1. Add a key generation algorithm that use advanced cryptography
  2. Call external web-api’s and wait for the result to add as data in the current model

Luckily there is an extension mechanism that allows for this – and once you have implemented it, it  is easy to call logic from your extensions straight from the model.

Assembly Eco.Interfaces
namespace Eco.Services
  // Summary:
  //     Allows you to supply domain object method implementations with late bound assembly
  public interface IExternalLateBoundService
    IElement Execute(IClassifier classifier, IObject theobject, IMethod method, IModifiableVariableList variableList);

Create a new class library with .net4.5. Set its output name to “TurnKey_ExternalLateBound.dll” (important)

Create a type in the assembly that implements the above interface IExternalLateBoundService.

In the Execute method – check the classifier and method name – like this:

    public Eco.ObjectRepresentation.IElement Execute(Eco.UmlRt.IClassifier classifier, 
IObject theobject,
IMethod method,
IModifiableVariableList variableList) { if (theobject != null && method != null) { if (theobject.UmlClass.Name == "SiteAsset") { if (method.Name == "SendAsset") { string res = "Sending asset: " + SendSiteAssetToTurnkeySite(theobject, variableList); return EcoServiceHelper.GetVariableFactoryService(theobject.ServiceProvider).CreateConstant(res); } } else if (theobject.UmlClass.Name == "RuntimeKey") { if (method.Name == "GenerateKey") { return RuntimeKey_GenerateKey(theobject, variableList); } } else { string res = "No logic for " + theobject.UmlClass.Name + "." + method.Name; return EcoServiceHelper.GetVariableFactoryService(theobject.ServiceProvider).CreateConstant(res); }
      return null;

As you can see from the code above it simply checks if the sent in object is of a certain model class – if so is the method named a certain way – if so we do things given the parameters we got and we return a result.

In the actual implementation of my external logic I can access the parameters sent in to the method like this:


I can access properties of the object that own the method like this:

var pageStyle = viewoverride.Properties["PageStyle"].GetValue<string>();

I can navigate associations like this:

IObject Product = theobject.Properties["Product"].Element as IObject;

To make the logic available to your turnkey site you must upload the assembly – and all other assemblies it depends on - to the folder “EXT_LateBoundAssembly” that is under the root of your turnkey site.

In your model you define the methods on the classes and leave their implementation blank and you fill in a tagged value on the method Eco.ExternalLateBound=True:

C- -1.png

Once you have followed these steps you can call your method from an action, a button or even from a derivation (if it the method is marked as a query) – and Turnkey will route the call to your assembly and execute your code.

NOTE: When you run with codedress - you should leave Body empty for ExternalLateBound methods - if not empty this will be executed instead of ExternalOverride. Leaving it empty will force the codegen to create a stub - but this will be ignored for ExternalLateBound. Mind that you may need to add return statements etc to the stub to make it compile.