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


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 uses 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. 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, 
Eco.ObjectRepresentation.
IObject theobject,
Eco.UmlRt.
IMethod method,
Eco.ObjectRepresentation.
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 and if the method is named a certain way. If so, we do things given the parameters we have and return a result.

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

variableList["vStartPart"].Element.GetValue<string>()

I can access the properties of the object that owns 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 - use the AssetsTK-strategy for this.

In your model, define the methods on the classes and leave their implementation blank. 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 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 it is 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. Understand that you may need to add return statements etc., to the stub to make it compile.

This page was edited 19 days ago on 12/02/2024. What links here