Caché .Net BindingアプリケーションをIRISの.Net Native APIを利用して書き換える方法(その2)
ここで紹介するサンプルは、以下のGitHubから入手可能です。
Powershellから.NETインタフェースを呼び出すサンプル1
callclassmethod.ps1というファイル名です。
処理内容は、Caché (IRIS)のバージョン情報を表示するという非常に単純なものです。
Caché では.Net Bindingを使用していましたが、IRIS用に.Net Native APIで書き換えてみます。
以前のCaché のコードは以下の通りです。
Add-Type -Path 'C:\InterSystems\Ensemble\dev\dotnet\bin\v4.0.30319\InterSystems.Data.CacheClient.dll'
$cc = New-Object InterSystems.Data.CacheClient.CacheConnection("localhost",1972,"USER","_SYSTEM","SYS")
$cc.Open()
$ms = New-Object InterSystems.Data.CacheTypes.CacheMethodSignature
$ms.Clear()
$ms.SetReturnType($cc,[InterSystems.Data.CacheTypes.ClientTypeId]::tString)[InterSystems.Data.CacheTypes.CacheObject]::RunClassMethod($cc,"%SYSTEM.Version","GetVersion",$ms)
$ms.ReturnValue._Value
$cc = New-Object InterSystems.Data.CacheClient.CacheConnection("localhost",1972,"USER","_SYSTEM","SYS")
$cc.Open()
$ms = New-Object InterSystems.Data.CacheTypes.CacheMethodSignature
$ms.Clear()
$ms.SetReturnType($cc,[InterSystems.Data.CacheTypes.ClientTypeId]::tString)[InterSystems.Data.CacheTypes.CacheObject]::RunClassMethod($cc,"%SYSTEM.Version","GetVersion",$ms)
$ms.ReturnValue._Value
IRISでは以下のように書き換えます。
Add-Type -Path 'C:\InterSystems\IRIS\dev\dotnet\bin\v4.5\InterSystems.Data.IRISClient.dll'
$cc = New-Object InterSystems.Data.IRISClient.IRISConnection("localhost",1972,"USER","_SYSTEM","SYS")
$cc.Open()
$iris = [InterSystems.Data.IRISClient.ADO.IRIS]::CreateIRIS($cc)
$ReturnValue = $iris.ClassMethodString("%SYSTEM.Version","GetVersion")
$ReturnValue
$cc = New-Object InterSystems.Data.IRISClient.IRISConnection("localhost",1972,"USER","_SYSTEM","SYS")
$cc.Open()
$iris = [InterSystems.Data.IRISClient.ADO.IRIS]::CreateIRIS($cc)
$ReturnValue = $iris.ClassMethodString("%SYSTEM.Version","GetVersion")
$ReturnValue
Powershellから.NETインタフェースを呼び出すサンプル2
classload.ps1というファイル名です。
処理内容は、xml形式のクラス定義をCaché /IRISにロードする処理になります。
以前のCaché のコードは以下の通りです。
Add-Type -Path 'C:\InterSystems\Ensemble\dev\dotnet\bin\v4.0.30319\InterSystems.Data.CacheClient.dll'
$cc = New-Object InterSystems.Data.CacheClient.CacheConnection("localhost",1972,"USER","_SYSTEM","SYS")
$cc.Open()
$ms = New-Object InterSystems.Data.CacheTypes.CacheMethodSignature
$ms.Clear()
$ms.SetReturnType($cc,[InterSystems.Data.CacheTypes.ClientTypeId]::tString)
$ms.Add("c:\temp\load.xml",1)
[InterSystems.Data.CacheTypes.CacheObject]::RunClassMethod($cc,"%SYSTEM.OBJ","Load",$ms)
$ms.ReturnValue._Value
$cc = New-Object InterSystems.Data.CacheClient.CacheConnection("localhost",1972,"USER","_SYSTEM","SYS")
$cc.Open()
$ms = New-Object InterSystems.Data.CacheTypes.CacheMethodSignature
$ms.Clear()
$ms.SetReturnType($cc,[InterSystems.Data.CacheTypes.ClientTypeId]::tString)
$ms.Add("c:\temp\load.xml",1)
[InterSystems.Data.CacheTypes.CacheObject]::RunClassMethod($cc,"%SYSTEM.OBJ","Load",$ms)
$ms.ReturnValue._Value
IRISでは以下のように書き換えます。
Add-Type -Path 'C:\InterSystems\IRIS\dev\dotnet\bin\v4.5\InterSystems.Data.IRISClient.dll'
$cc = New-Object InterSystems.Data.IRISClient.IRISConnection("localhost",1972,"USER","_SYSTEM","SYS")
$cc.Open()
$iris = [InterSystems.Data.IRISClient.ADO.IRIS]::CreateIRIS($cc)
$iris.ClassMethodStatusCode("%SYSTEM.OBJ","Load","c:\temp\load.xml","ck-d")
$cc = New-Object InterSystems.Data.IRISClient.IRISConnection("localhost",1972,"USER","_SYSTEM","SYS")
$cc.Open()
$iris = [InterSystems.Data.IRISClient.ADO.IRIS]::CreateIRIS($cc)
$iris.ClassMethodStatusCode("%SYSTEM.OBJ","Load","c:\temp\load.xml","ck-d")
IRISの方がコード量が少なくてすむことがおわかりいただけると思います。
注意点は、そのクラスメソッドの戻り値のタイプによって、呼び出すメソッドが変わることです。
サンプル1の場合は、バージョン情報というテキスト情報が返ってくる想定ですので、ClassMethodStringを使用します。
サンプル2の場合は、戻り値はステータスコードが返ってくる想定ですので、ClassMethodStatusCodeを使用します。
.NETインタフェースを呼び出すコンソールアプリケーション
内容は、Powershellサンプルと同じです。
以下Caché 用のコードです。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using InterSystems.Data.CacheClient;
using InterSystems.Data.CacheTypes;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
CacheConnection conn = new CacheConnection();
conn.ConnectionString = "Server = localhost; Log File=cprovider.log;Port=1972; Namespace=Samples; Password = SYS; User ID = _SYSTEM;";
conn.Open();
CacheMethodSignature ms = new CacheMethodSignature();
ms.SetReturnType(conn, ClientTypeId.tString);
CacheObject.RunClassMethod(conn, "%SYSTEM.Version", "GetVersion", ms);
Console.Write("ReturnValue = " + ms.ReturnValue._Value);
conn.Close();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using InterSystems.Data.CacheClient;
using InterSystems.Data.CacheTypes;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
CacheConnection conn = new CacheConnection();
conn.ConnectionString = "Server = localhost; Log File=cprovider.log;Port=1972; Namespace=Samples; Password = SYS; User ID = _SYSTEM;";
conn.Open();
CacheMethodSignature ms = new CacheMethodSignature();
ms.SetReturnType(conn, ClientTypeId.tString);
CacheObject.RunClassMethod(conn, "%SYSTEM.Version", "GetVersion", ms);
Console.Write("ReturnValue = " + ms.ReturnValue._Value);
conn.Close();
}
}
}
IRISで.Net Native APIを使用して書き換えたコードは以下になります。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using InterSystems.Data.IRISClient;
using InterSystems.Data.IRISClient.ADO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
IRISConnection conn = new IRISConnection();
IRIS iris;
conn.ConnectionString = "Server = localhost; Log File=irisnativeapi.log;Port=1972; Namespace=USER; Password = SYS; User ID = _SYSTEM;";
conn.Open();
iris = IRIS.CreateIRIS(conn);
string ReturnValue = iris.ClassMethodString("%SYSTEM.Version", "GetVersion");
Console.Write("ReturnValue = " + ReturnValue);
conn.Close();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using InterSystems.Data.IRISClient;
using InterSystems.Data.IRISClient.ADO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
IRISConnection conn = new IRISConnection();
IRIS iris;
conn.ConnectionString = "Server = localhost; Log File=irisnativeapi.log;Port=1972; Namespace=USER; Password = SYS; User ID = _SYSTEM;";
conn.Open();
iris = IRIS.CreateIRIS(conn);
string ReturnValue = iris.ClassMethodString("%SYSTEM.Version", "GetVersion");
Console.Write("ReturnValue = " + ReturnValue);
conn.Close();
}
}
}