You can extend a Turnkey application with server-side C# code when a modeled method must perform work such as advanced cryptography or calling an external web API.
Choose the integration path
Use a modeled rule or OCL expression when the required behavior belongs in the model. Use the server-side extension mechanism when the implementation requires C# code outside the modeled rule.
For example, a modeled RuntimeKey.GenerateKey method can delegate key generation to C# code. A modeled SiteAsset.SendAsset method can call an external web API, wait for its result, and return data to the current model.
For broader integration topics, including authentication, external APIs, import and export, and third-party services, start with Documentation:Integration.
Where server-side extension code runs
Turnkey separates the browser user interface, the Turnkey web application, MDrivenServer, and the database. The browser presents information made available through ViewModels; actions are sent for server-side processing.
The documented late-bound extension mechanism is a server-side integration point. You define a method on a model class, and Turnkey calls the extension when that modeled method is invoked. The extension receives the classifier, the object that owns the method, the method, and the supplied variables. It can return a value to the model.
Keep the ViewModel focused on the information and actions the user needs. Put the external call in the modeled method implementation rather than in browser presentation code when the call must execute on the server.
Implement a late-bound C# extension
- Create a .NET class library.
- Add a type that implements
Eco.Services.IExternalLateBoundServicefrom theEco.Interfacesassembly. - Implement
Execute. Check the model class and method name, then run the matching C# implementation. - Define the corresponding methods on the model classes.
- Upload the extension assembly and every assembly it depends on to
EXT_LateBoundAssemblyunder the Turnkey site root, using the AssetsTK strategy.
using Eco.Services;
public class ExternalLateBoundService : IExternalLateBoundService
{
public IElement Execute(
IClassifier classifier,
IObject theobject,
IMethod method,
IModifiableVariableList variableList)
{
if (theobject != null && method != null &&
theobject.UmlClass.Name == "RuntimeKey" &&
method.Name == "GenerateKey")
{
return RuntimeKey_GenerateKey(theobject, variableList);
}
return null;
}
}
Read model data and return a result
Use the supplied variable list for method parameters. For example, read a parameter named vStartPart with:
string startPart = variableList["vStartPart"].Element.GetValue<string>();
Use the owning object to read modeled properties and navigate associations. For example:
IObject product = theobject.Properties["Product"].Element as IObject;
Return a constant through the variable factory when the modeled method returns a value:
return EcoServiceHelper
.GetVariableFactoryService(theobject.ServiceProvider)
.CreateConstant("Sending asset");
Example: call an external API from a modeled method
Model a SendAsset method on SiteAsset. In Execute, identify the SiteAsset.SendAsset call, read the owning object and method variables, call the external API from the C# implementation, and return the result as a model value. The modeled method is the contract between the model and the C# extension.
Deployment checks
- Deploy every dependent assembly together with the extension assembly.
- Place the assemblies in
EXT_LateBoundAssemblyunder the Turnkey site root. - Define the class methods in the model so that Turnkey has a modeled method to invoke.
