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 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:

<span style="background: white; color: black">    </span><span style="background: white; color: blue">public </span><span style="background: white; color: black">Eco.ObjectRepresentation.</span><span style="background: white; color: #2b91af">IElement </span><span style="background: white; color: black">Execute(Eco.UmlRt.</span><span style="background: white; color: #2b91af">IClassifier </span><span style="background: white; color: black">classifier, <br>                                                     Eco.ObjectRepresentation.</span><span style="background: white; color: #2b91af">IObject </span><span style="background: white; color: black">theobject, <br>                                                     Eco.UmlRt.</span><span style="background: white; color: #2b91af">IMethod </span><span style="background: white; color: black">method, <br>                                                     Eco.ObjectRepresentation.</span><span style="background: white; color: #2b91af">IModifiableVariableList </span><span style="background: white; color: black">variableList)
    {
      </span><span style="background: white; color: blue">if </span><span style="background: white; color: black">(theobject != </span><span style="background: white; color: blue">null </span><span style="background: white; color: black">&& method != </span><span style="background: white; color: blue">null</span><span style="background: white; color: black">)
      {
        </span><span style="background: white; color: blue">if </span><span style="background: white; color: black">(theobject.UmlClass.Name == </span><span style="background: white; color: #a31515">"SiteAsset"</span><span style="background: white; color: black">)
        {
          </span><span style="background: white; color: blue">if </span><span style="background: white; color: black">(method.Name == </span><span style="background: white; color: #a31515">"SendAsset"</span><span style="background: white; color: black">)
          {
            </span><span style="background: white; color: blue">string </span><span style="background: white; color: black">res = </span><span style="background: white; color: #a31515">"Sending asset: " </span><span style="background: white; color: black">+ SendSiteAssetToTurnkeySite(theobject, variableList);
            </span><span style="background: white; color: blue">return </span><span style="background: white; color: #2b91af">EcoServiceHelper</span><span style="background: white; color: black">.GetVariableFactoryService(theobject.ServiceProvider).CreateConstant(res);
          }
</span><span style="background: white; color: black">        }
        </span><span style="background: white; color: blue">else if </span><span style="background: white; color: black">(theobject.UmlClass.Name == </span><span style="background: white; color: #a31515">"RuntimeKey"</span><span style="background: white; color: black">)
        {
          </span><span style="background: white; color: blue">if </span><span style="background: white; color: black">(method.Name == </span><span style="background: white; color: #a31515">"GenerateKey"</span><span style="background: white; color: black">)
          {
            </span><span style="background: white; color: blue">return </span><span style="background: white; color: black">RuntimeKey_GenerateKey(theobject, variableList);
          }
        }
</span><span style="background: white; color: black">        </span><span style="background: white; color: blue">else
        </span><span style="background: white; color: black">{
            </span><span style="background: white; color: blue">string </span><span style="background: white; color: black">res = </span><span style="background: white; color: #a31515">"No logic for " </span><span style="background: white; color: black">+ theobject.UmlClass.Name + </span><span style="background: white; color: #a31515">"." </span><span style="background: white; color: black">+ method.Name;
            </span><span style="background: white; color: blue">return </span><span style="background: white; color: #2b91af">EcoServiceHelper</span><span style="background: white; color: black">.GetVariableFactoryService(theobject.ServiceProvider).CreateConstant(res);
          
        }
</span>
<span style="background: white; color: black">        }
     }
     </span><span style="background: white; color: blue">return null</span><span style="background: white; color: black">;
}</span>

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:

<span style="background: white; color: black">variableList[</span><span style="background: white; color: #a31515">"vStartPart"</span><span style="background: white; color: black">].Element.GetValue<</span><span style="background: white; color: blue">string</span><span style="background: white; color: black">>()</span>

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

<span style="background: white; color: blue">var </span><span style="background: white; color: black">pageStyle = viewoverride.Properties[</span><span style="background: white; color: #a31515">"PageStyle"</span><span style="background: white; color: black">].GetValue<</span><span style="background: white; color: blue">string</span><span style="background: white; color: black">>();</span>

I can navigate associations like this:

<span style="background: white; color: #2b91af">IObject </span><span style="background: white; color: black">Product = theobject.Properties[</span><span style="background: white; color: #a31515">"Product"</span><span style="background: white; color: black">].Element </span><span style="background: white; color: blue">as </span><span style="background: white; color: #2b91af">IObject</span><span style="background: white; color: black">;
</span>
<span style="background: white; color: black">
</span>

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:

<a href="//www.capableobjects.com/wp-content/uploads/2016/07/image17.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="//www.capableobjects.com/wp-content/uploads/2016/07/image_thumb17.png" width="602" height="484"></a>

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.

This page was edited 89 days ago on 02/10/2024. What links here