Sometimes you need to run code whenever a single link (a 0..1 or 1 association end) on an object is changed — for example, to log the change, stamp a timestamp, or update another attribute.
In MDriven you do this by letting the class implement the ISingleLinkCatcher interface. Whenever any single link on an object of that class is set or changed, the framework calls SingleLinkModified on that object.
ISingleLinkCatcher is C# code, so in an MDriven project it requires CodeDress where the compiled model code is uploaded with the model.
Implementing ISingleLinkCatcher
- Open your model project in Visual Studio and run Code Generation if you have changed the model.
- Open the partial class file for the class you want to watch (for example
Thing.cs). - Add
ISingleLinkCatcherto the class declaration and use Implement interface in Visual Studio. - Write your side effect in
SingleLinkModified.
using Eco.ObjectRepresentation;
using System;
namespace Package1
{
public partial class Thing : ISingleLinkCatcher
{
public void SingleLinkModified(IAssociationEnd ae, IEcoObject oldValue, IEcoObject newValue)
{
this.SomeString = ae.Name + " changed " + DateTime.Now.ToLongTimeString();
}
}
}
The method parameters:
| Parameter | Description |
|---|---|
ae
|
The association end that changed. Use ae.Name to see which link it was.
|
oldValue
|
The object the link pointed to before the change (may be null). |
newValue
|
The object the link points to now (may be null). |
Because one method handles every single link on the class, check ae.Name if you only want to react to specific links.
Getting the code into Turnkey
Turnkey runs your C# code only if the compiled assemblies are uploaded together with the model. The steps below follow the setup shown in the video.
1. Build the assemblies into the AssetsTK folder
On .NET (Core/6+), a class library does not copy all its dependencies to its output folder. To get a complete set of binaries, add a small console application to the solution that:
- references your model project, and
- sets its
BaseOutputPathto<ModelName>_AssetsTK\ModelCodeAssembliesnext to your model file.
Example console app .csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<BaseOutputPath>..\MyModelProject\MDrivenModel_AssetsTK\ModelCodeAssemblies</BaseOutputPath>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\MyModelProject\MyModelProject.csproj" />
</ItemGroup>
</Project>
Adjust the paths and the MDrivenModel prefix to match your own model file name.
2. Build the solution
Build the console app so all assemblies end up in ModelCodeAssemblies.
3. Upload the model to MDrivenServer
When you upload, MDriven Designer zips the _AssetsTK folder and uploads it with the model. The Turnkey site that uses this MDrivenServer then picks up the new code.
Note: The Designer only uploads when it detects a change. If you have only changed C# code and not the model, the checksum may be unchanged, and the new code is not uploaded. Make a small change to the model (anything that changes the checksum) before uploading, to make sure the new assemblies are sent.
Testing it
- Open the Turnkey application and go to a view showing an object of your class.
- Change the single link (for example, pick another value in a combobox bound to the link).
- The side effect runs at once. In the example,
SomeStringshows something likeSomeValue changed 15:53:08.
Tips
ISingleLinkCatcheris usually the cleanest way to react to link changes, compared with attribute-level hooks.- Keep side effects small and predictable. Code here runs on every link change for every object of the class.
- If nothing seems to happen in Turnkey, check that the model really was re-uploaded (see the checksum note above) and that MDrivenServer reports the code as found and dressed.
