Skip to content

High level API with O R mapper

Daniel Frantik edited this page Mar 5, 2016 · 12 revisions

Third option is to use High-level API with strongly typed entities. This API uses internal O/R mapper to map mikrotik repose sentences to prepared (or yours) objects. This API is in separate dll - so you should reference tik4net.objects.dll.

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

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

The main entry point is mikrotik router connection object (ITikConnection). Instance of connection object should be instancied via factory (ConnectionFactory). 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

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