-
Notifications
You must be signed in to change notification settings - Fork 99
High level API with O R mapper
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>();- Data entities
- Reading data entities
- Create, update, delete entities
- Advanced scenarios
- How to create custom entity
- How to use provided tools to create custom entities
// 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
LoadByNamewhere name is unique - Mark item by custom
commentand use filter to find (REMARKS: prefix/suffix/regex are not supported by mikrotik API) - Use
LoadAlland 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
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);
}
}