Anyone already wrap Couchbase .Net API?

Posted by WinningJr on 30-Oct-2017 10:19

The Couchbase .Net API appears to only use generic methods.  Has anyone already dealt with this, before we start writing wrappers in C#?

bucket.GetDocument<dynamic>("document_id")

Thanks,

Gerry

All Replies

Posted by doa on 30-Oct-2017 10:50
Posted by Mike Fechner on 30-Oct-2017 11:53

Reflection is your friend.

github.com/.../ReflectionHelperOSS

Posted by WinningJr on 30-Oct-2017 16:53

Reflection is my friend!!  Working example below.

Mike - I had to debug to get ReflectionHelper.cls when using methods with parameters..  You are adding objects to the paremeterTypes array and Types to the object Array:

  oParameterTypes:SetValue (poParameter1, 0) .

  oParameters:SetValue (Progress.Util.TypeHelper:GetType (pcParameterType1), 0) .

For the archive, here is a working example against the Couchbase sample DB with some code stolen from Mike:

DEFINE VARIABLE oObjectType         AS System.Type                                        NO-UNDO . 
DEFINE VARIABLE oMethodInfo         AS System.Reflection.MethodInfo                       NO-UNDO . 
DEFINE VARIABLE oGenericMethod      AS System.Reflection.MethodInfo                       NO-UNDO . 
DEFINE VARIABLE oObjectTypeOfArray  AS "System.Type[]"                                    NO-UNDO . 
DEFINE VARIABLE oParameters         AS "System.Object[]"                                  NO-UNDO . 
DEFINE VARIABLE oParameterTypes     AS "System.Type[]"                                    NO-UNDO .        
DEFINE VARIABLE uri                 AS System.Uri                                         NO-UNDO.
DEFINE VARIABLE cluster             AS Couchbase.Cluster                                  NO-UNDO. 
DEFINE VARIABLE bucket              AS Couchbase.Core.IBucket                             NO-UNDO. 
DEFINE VARIABLE clientConfiguration AS Couchbase.Configuration.Client.ClientConfiguration NO-UNDO. 
DEFINE VARIABLE uriList             AS CLASS 
  "System.Collections.Generic.List<System.Uri>"             NO-UNDO.
DEFINE VARIABLE documentResult     AS CLASS 
  "Couchbase.DocumentResult<Newtonsoft.Json.Linq.JObject>" NO-UNDO. 
DEFINE VARIABLE sysObj              AS System.object                                      NO-UNDO.
DEFINE VARIABLE jObject             AS Newtonsoft.Json.Linq.JObject                       NO-UNDO. 

uri = NEW System.Uri ("http://YourServer:8091").
uriList = NEW  "System.Collections.Generic.List<System.Uri>" ().
uriList:Add(uri).
clientConfiguration = NEW Couchbase.Configuration.Client.ClientConfiguration ( ).
clientConfiguration:Servers = uriList.  
cluster = NEW Couchbase.Cluster(clientConfiguration).
bucket = cluster:OpenBucket("travel-sample"). 
    
// Define an array filled with System.Type to define the .net type of the return value
oObjectTypeOfArray = CAST (System.Array:CreateInstance (Progress.Util.TypeHelper:GetType("System.Type"), 1), "System.Type[]") .
// fill the methodtype array with the type of objects that the method returns   
oObjectTypeOfArray:SetValue (Progress.Util.TypeHelper:GetType ("Newtonsoft.Json.Linq.JObject"), 0) .

// Define an array filled with System.Object to hold the parameter values for the dynamic method
oParameters = CAST (System.Array:CreateInstance (Progress.Util.TypeHelper:GetType("System.Object"), 1), "System.Object[]") .
// Define an array filled with System.Type to hold the parameter types for the dynamic method (system.String, System.Int, etc.
oParameterTypes = CAST (System.Array:CreateInstance (Progress.Util.TypeHelper:GetType("System.Type"), 1), "System.Type[]").

// fill the parameter arrays with the values and their .net types
oParameterTypes:SetValue (Progress.Util.TypeHelper:GetType ("System.String"), 0) . 
oParameters:SetValue ( ("airline_10"), 0) .
   
// Get the system.type of the parent object            
oObjectType = bucket:GetType () . 
// Find the method by name with the correct signature based on the parameters in the type array
oMethodInfo = oObjectType:GetMethod ("GetDocument", oParameterTypes) .

// make a generic method that returns the types in the objectTypeArray
oGenericMethod = oMethodInfo:MakeGenericMethod (oObjectTypeOfArray) .  
 
sysObj =  oGenericMethod:Invoke (bucket, oParameters) .
documentResult = CAST (sysObj, "Couchbase.DocumentResult<Newtonsoft.Json.Linq.JObject>").
jObject = documentResult:Content. 
 
IF documentResult:Success THEN 
  MESSAGE 
    jObject:GetValue("name")
    VIEW-AS ALERT-BOX.
ELSE 
  MESSAGE "ERROR: " documentResult:STATUS 
    VIEW-AS ALERT-BOX.
 
RETURN

This thread is closed