Skip to content

High level API with O R mapper

Daniel Frantík edited this page Jul 20, 2026 · 16 revisions

Third option is to use the High-level API with strongly typed entities. This API uses an internal O/R mapper to map MikroTik response sentences to prepared (or your own) objects. It lives in its own assembly (tik4net.objects.dll), but since 4.0 that assembly ships inside the main tik4net NuGet package — nothing extra to install. (Before 4.0 it was a separate package, tik4net.objects and later tik4net.entities; see Upgrading from 3.x to 4.0.)

All methods of this API are implemented as extensions of ITikConnection and ITikCommand. That means that the ADO.NET like API and the High-level API can be used together.

NOTE: do not forget to put using tik4net.Objects; in your class header.

The main entry point is the MikroTik router connection object (ITikConnection). An instance of the connection object should be instantiated via a factory (ConnectionFactory). The command instance should be created via ITikConnection methods.

using tik4net;
using tik4net.Objects;
// ...
connection = ConnectionFactory.OpenConnection(TikConnectionType.Api, HOST, USER, PASS);
var interfaces = connection.LoadAll<Interface>();

Using High-level API

Single row modify sample

// SCRIPT equivalent: interface set comment=test [/interface find name=ether1]
private static void ChangeCommentOnInterfaceWithName(ITikConnection connection, string interfaceName, string newComment)
{
  // Load by name (filter)
  var iface = connection.LoadByName<Objects.Interface.Interface>(interfaceName);
  // modify iface object + write changes to mikrotik
  iface.Comment = newComment;
  connection.Save(iface);
}
// SCRIPT equivalent: interface set comment=test [/interface find default-name=ether1]
private static void ChangeCommentOnInterfaceWithDefaultName(ITikConnection connection, string defaultName, string newComment)
{
  // Load by default-name (filter)
  var iface = connection.LoadSingle<Objects.Interface.Interface>(connection.CreateParameter("default-name", defaultName));
  // modify iface object + write changes to mikrotik
  iface.Comment = newComment;
  connection.Save(iface);
}

Notes - how to find single item

  • Use LoadByName where name is unique
  • Mark item by custom comment and use filter to find (REMARKS: prefix/suffix/regex are not supported by mikrotik API)
  • Use LoadAll and C# lookup in collection for more complicated scenarios (be careful when reading a large number of rows)
  • Create custom small entity for handling large resultsets

More complex example

private static void CreateOrUpdateAddressList(ITikConnection connection)
{
  var existingAddressList = connection.LoadList<FirewallAddressList>(
          connection.CreateParameter("list", "TEST-LIST"),
          connection.CreateParameter("address", "192.168.88.1"))
      .SingleOrDefault();
  
    if (existingAddressList == null)
    {
        //Create
        var newAddressList = new FirewallAddressList()
        {
            Address = "192.168.88.1",
            List = "TEST-LIST",
        };
        connection.Save(newAddressList);
    }
    else
    {
         //Update
         existingAddressList.Comment = "Comment update: " + DateTime.Now.ToShortTimeString();

        connection.Save(existingAddressList);
    }
}

Clone this wiki locally