3
votes

Having called a .Net object as shown below (cfdump shown beneath)...

<cfobject type=".NET" name="GoCardless" class="GoCardlessSdk.GoCardless" assembly="#expandpath("../GoCardlessSdk.dll")#,#expandpath("../Newtonsoft.Json.dll")#,#expandpath("../RestSharp.dll")#">

cfdump of .Net object

I now need to set an enum. The example .Net code for this would be:

GoCardless.Environment = GoCardless.Environments.Sandbox;

The C# code for the class is shown below:

using System;
using System.Collections.Generic;
using System.Reflection;
using GoCardlessSdk.Api;
using GoCardlessSdk.Connect;
using GoCardlessSdk.Helpers;
using GoCardlessSdk.Partners;

namespace GoCardlessSdk
{
    public static class GoCardless
    {
        static GoCardless()
        {
            AccountDetails = new AccountDetails();
        }

        public enum Environments
        {
            Production,
            Sandbox,
            Test
        }

        public static readonly Dictionary<Environments, string> BaseUrls =
            new Dictionary<Environments, string>
                {
                    {Environments.Production, "https://gocardless.com"},
                    {Environments.Sandbox, "https://sandbox.gocardless.com"},
                    {Environments.Test, "http://gocardless.com"}
                };

        private static string _baseUrl;

        public static string BaseUrl
        {
            get { return _baseUrl ?? BaseUrls[Environment ?? Environments.Production]; }
            set { _baseUrl = value.Trim(); }
        }

        public static Environments? Environment { get; set; }

        public static AccountDetails AccountDetails { get; set; }

        public static ApiClient Api
        {
        get { return new ApiClient(AccountDetails.Token); }
        }

        public static ConnectClient Connect
        {
            get { return new ConnectClient(); }
        }

        public static PartnerClient Partner
        {
            get { return new PartnerClient(); }
        }


        internal static string UserAgent = GetUserAgent();
        private static string GetUserAgent()
        {
            try
            {
                return "gocardless-dotnet/v" + GetAssemblyFileVersion();
            }
            catch
            {
                return "gocardless-dotnet";
            }
        }
        private static string GetAssemblyFileVersion()
        {
            Assembly assembly = Assembly.GetAssembly(typeof (GoCardless));
            var attributes = assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false)
                    as AssemblyFileVersionAttribute[];

            if (attributes != null && attributes.Length == 1)
            {
                return attributes[0].Version;
            }
            return "";
        }

        private static Func<string> _generateNonce;
        internal static Func<string> GenerateNonce
        {
            get { return _generateNonce ?? (_generateNonce = Utils.GenerateNonce); }
            set { _generateNonce = value; }
        }

        private static Func<DateTimeOffset> _getUtcNow; 
        internal static Func<DateTimeOffset> GetUtcNow
        {
            get { return _getUtcNow ?? (_getUtcNow = () => DateTimeOffset.UtcNow); }
            set { _getUtcNow = value; }
        } 
    }
    }

Is anybody able to explain to me how I would set the enum in ColdFusion?

Thanks!

Update

In response to Leigh's utility class solution my code is as follows:

<cfscript>

    GoCardless = createObject(".net", "GoCardlessSdk.GoCardless","path to GoCardless DLL,paths to supporting DLLs");

    Environments = createObject(".net", "GoCardlessSdk.GoCardless$Environments", "path to GoCardless DLL");

    util = createObject(".net", "GoCardlessSdk.GoCardlessSdkUtil", "path to utility DLL");

    dumps etc...

</cfscript>

If I remove the last createobject() call the first two run perfectly well. Even if I include the paths to all DLLs in all createobject() calls I still get the same error.

1

1 Answers

4
votes

(Disclaimer: My current test environment is CF9, but the results were the same as yours)

Normally you can modify a field by using set_FieldName( value ). However, I can see from your screen shot that method does not exist. I was not sure why, so I did a little searching. Based on what I have read, your class is using Nullable Types:

    public static Environments? Environment { get; set; }

That seems to be the root of the problem. From what I can tell, jnbridge (underlying tool used for .net interop) does not seem to generate proxies when Nullable Types are involved. That would explain why the methods to access the Evironment field are missing. If you remove the ? operator, it works just fine.

CF Code:

<cfscript>
    goCardless = createObject(".net", "GoCardlessSdk.GoCardless", ExpandPath("./GoCardlessSdk.dll"));
    Environments = createObject(".net", "GoCardlessSdk.GoCardless$Environments", ExpandPath("./GoCardlessSdk.dll"));
    goCardLess.set_Environment(Environments.Test);
    writeDump( goCardLess.get_Environment().toString() );
</cfscript>

Test Class: (Without Nullable Type)

namespace GoCardlessSdk
{
    public static class GoCardless
    {
        static GoCardless()
        {
        }

        public enum Environments
        {
            Production,
            Sandbox,
            Test
        }

        public static Environments Environment { get; set; }
    }
}

Update 1:

  • So modifying the class to eliminate the Nullable type is one option
  • Another possibility is to generate the proxies manually. jnbridge also has issues with .net generics, so generating the proxies yourself might work.
  • A third possibility is to write a helper class that sets/gets the value without using the Nullable type. It is a bit of a hack, but does work.

Update 2/3:

Here is a rough example of a simpler helper class. If you want the helper get method to return null (like the original) then use getNullableEnvironment. If you prefer to return a default, instead of null, use getEnvironment. As far as setup all I did was:

  • Created a new solution in VS
  • Added reference to GoCardlessSDK.dll

<cfscript>
   dllPaths = arrayToList( [ ExpandPath("GoCardlessUtil.dll")
                            , ExpandPath("GoCardlessSdk.dll")
                            , ExpandPath("Newtonsoft.Json.dll")
                            , ExpandPath("RestSharp.dll")
                            ]
                        );
    goCardless = createObject(".net", "GoCardlessSdk.GoCardless", dllPaths);
    Environments = createObject(".net", "GoCardlessSdk.GoCardless$Environments", dllPaths);
    util = createObject(".net", "GoCardlessUtil.GoCardlessSdkUtil", dllPaths );
    WriteDump("before="& util.getNullableEnvironment());
    util.setEnvironment(Environments.Production);
    WriteDump("after="& util.getNullableEnvironment().toString());
</cfscript> 

GoCardlessSdkUtil.cs

using System;
using System.Collections.Generic;
using System.Text;
using GoCardlessSdk;

namespace GoCardlessUtil
{
    public class GoCardlessSdkUtil
    {
        public static void setEnvironment(GoCardless.Environments environ)
        {
            GoCardless.Environment = environ;
        }

        /*
        // Enum's cannot be null. So we are applying a default
        // value ie "Test" instead of returning null
        public static GoCardless.Environments getEnvironment()
        {
            return GoCardless.Environment.HasValue ? GoCardless.Environment.Value : GoCardless.Environments.Test;
        }
        */

        // This is the closest to the original method
        // Since enum's cannot be null, we must return type Object instead of Environment
        // Note: This will return be null/undefined the first time it is invoked
        public static Object getNullableEnvironment()
        {
            return GoCardless.Environment;
        }
    }
}