Wednesday, October 29, 2014

Microsoft Dynamics GP 2015 Developer's Preview: .NET Framework Interoperability - Part 3

In part 2 of the series, I delivered a brief primer on the service architecture in Microsoft Dynamics GP 2015 and how you are able to consume services natively created with Dexterity. There are two types of services that can be created: services that wrap existing windows and forms functionality, i.e., creating a customer; and services that can wrap around existing sanScript procedures, i.e, retrieving customer information. The truth is, none of that stuff would be possible without the ability to interoperate with the Microsoft .NET Framework.

Today, I'm going to show you some of the .NET interop capabilities built into Dexterity 14.0 and how these can enhance the user experience and your application integration capabilities. The example used in this post is based on my previous article, Building a COM Interop Assembly to use with Microsoft Dexterity, which showed how we had to leverage .NET's ability to expose an assembly via COM interfaces, so we could reference its methods using Dexterity. In that particular example, I built some standard methods to expose the sine, cosine, and tangent trigonometric functions to a Dexterity application. This time around, I will show how to build the same functionality taking full advantage of Dexterity's new .NET interop capabilities.

The user experience will be slightly different this time. In addition to a window that will allow you to calculate any of the trigonometric functions on a specific angle (entered in degrees), we will display a .NET form showing a visual representation of that angle. The .NET form will be dynamically built and displayed from Dexterity using the methods and properties provided by the Form class (System.Windows.Forms) and the visual drawings will be delivered using the classes exposed in the System.Drawing namespace.

1. The first things we must do is incorporate references to the corresponding assemblies for the Forms class and Drawing namespaces. As developer, the first thing you will notice is the enhancements to Dexterity's Library Definition window, which now allows for the selection of a .NET Assembly library type.

Library Definition
Once the type has been selected, you can browse out to the different .NET assemblies loaded on your operating system.

.NET Assemblies
In particular, what I like is the ability to select the .NET Framework assembly that I want to work with directly, as opposed to having to compile my application for a specific Framework version as you normally would do with Visual Studio.

Resource Explorer will now reflect the different .NET namespaces selected for you application.

Resource Explorer

2. Since this article assumes some familiarity with Dexterity, I'm not going to dive into the process of building the form and window, but rather point out that it follows the same layout and properties I used in my previous article.

Form and Window Definition
The only "rarity" here is I added a local field called '(L) GraphicAngleForm' that will serve as a generic reference to the .NET form we will dynamically create. The '(L) Conversion' and '(L) Angle' fields are currency fields and are formatted with two decimals and unsigned (DLR11_U2).

Generic Reference field

3. The code for our '(L) Sine' button will look something like this.

MGB_Trigonometric_Test l_Sine_CHG
// Created by Mariano Gomez, MVP
//  This code is licensed under the Creative Commons 
//  Attribution-NonCommercial-ShareAlike 3.5 Generic license.
using System;
using System.Windows.Forms;
using System.Drawing;

local currency l_angle;
local Form f;

'(L) Prompt' = "The Sin(%1°) value is ";
substitute '(L) Prompt', str('(L) Angle');

// calculate the sine value of the angle after converting it to radians
'(L) Conversion' = Math.Sin(Math.PI * '(L) Angle' / 180.0);

if empty('(L) GraphicAngleForm') then
 f = new Form();
 '(L) GraphicAngleForm' = f;

 f.Text = "Graphical Representation of the Angle entered";
 f.BackColor = Color.White;
 // Set the size of the form
 f.ClientSize = new Size(640, 480);
 // Make the form a fixed size
 f.MaximumSize = f.ClientSize;
 f.MinimumSize = f.ClientSize;
 // event handlers
 f.Paint += PaintAngle of form MGB_Trigonometric_Test;
 f.FormClosed += CloseDrawingForm of form MGB_Trigonometric_Test;
end if;

// show and activate our .NET form
'(L) GraphicAngleForm'.Show();
'(L) GraphicAngleForm'.Activate();

When you really look at the code, the first thing you will notice is the first 3 lines. sanScript now implements the using statement just like C#. sanScript has also been extended to use the implement statement for dynamically referencing an assembly.

using System;
using System.Windows.Forms;
using System.Drawing;

Next we can then calculate the sine of the angle in radians by simply calling the Math.Sin() method in the System namespace (the System namespace is part of the Microsoft Core Library, mscorlib.dll). We also reference the Math.PI constant to perform the conversion of the angle from degrees to radians.

// calculate the sine value of the angle after converting it to radians
'(L) Conversion' = Math.Sin(Math.PI * '(L) Angle' / 180.0);

Once we have the angle converted and displayed in the '(L) Conversion' field, we can proceed to create the form by instantiating the Form class - in reality, this part is no different than what you would normally do for COM classes - and setting some properties for the .NET form we want to display. Once we have set the size of the window, we need to create some event handlers for when the .NET form open and closes. As you can see in the code, Dexterity now implements event handlers through the use of the += operator.

// event handlers
f.Paint += PaintAngle of form MGB_Trigonometric_Test;
f.FormClosed += CloseDrawingForm of form MGB_Trigonometric_Test;

Our event handlers in this case will be the PaintAngle (on .NET form open) and CloseDrawingForm (on .NET form close). The PaintAngle event handler will display the actual graphical representation of the angle, and the CloseDrawingForm event handler will do some cleanup for us. Now, as you may suspect, these event handlers are implemented as procedure scripts to the MGB_Trigonometric_Test form (form procedures). So let's take a look at the PaintAngle form procedure:

// Created by Mariano Gomez, MVP
//  This code is licensed under the Creative Commons 
//  Attribution-NonCommercial-ShareAlike 3.5 Generic license.
using System;
using System.Windows.Forms;
using System.Drawing;

inout System.Object sender;
inout PaintEventArgs e;

local Pen bp, rp;
local Color c;

// Get the color
c = Color.Blue;

// Create the blue pen
bp = new Pen(c, 2);

e.Graphics.DrawLine(bp, 320, 240, 520, 240);
e.Graphics.DrawLine(bp, 320, 240, 320 + Math.Cos('(L) Conversion' of window MGB_Trigonometric_Test) * 200, 240 
 - Math.Sin('(L) Conversion' of window MGB_Trigonometric_Test) * 200);

c = Color.Red;
rp = new Pen(c, 2);

e.Graphics.DrawEllipse(rp, 300, 220, 40, 40);

Again, the mechanics here is not so much the important part, but rather to highlight the clever way in which the event handler parameters are declared for the sender of the event and the event arguments being passed by the sender of the event. For this the Dexterity team chose to implement inout parameters.

Overall the PrintAngle procedure simply draws two lines and a circle. The first line is the base line of the angle and will simply be a horizontal line. The second line actually shows the displacement in reference to the base line to give the actual angle representation. This all looks something like this when the code is executed:

Angle Calculator
Having the ability to leverage .NET capabilities directly from Dexterity has now opened up a new realm of possibilities for Microsoft Dynamics GP business application developers. Applications that were once thought to be out of reach or required complex workarounds are now a thing of the past. Code portability is now much more compact and robust than before. The good part is, Dexterity developers do not need relearn any development patterns and/or be exposed to steep learning curves. All the .NET Framework documentation is available online with tons of sample code to go along.

This article is part of the Microsoft Dynamics GP 2015 Developer's Preview series.

Microsoft Dynamics GP 2015 Developer's Preview: Loading the VHD image - Part 1
Microsoft Dynamics GP 2015 Developer's Preview: Working with Sample URIs - Part 2

Until next post!

Mariano Gomez, MVP
Intelligent Partnerships, LLC


Meenakshi Niyogi said...

Very useful
Can't wait to go back and try this after the conference

Evincible Solutions said...

Nice post. please post an article on Microsoft Dynamics CRM.

Guillaume Simard said...

Thanks for this post. It's a good starting point to learn how to use that new feature.

Do you know if there is any way to provide exchange more complex types to .NET objects ?
For instance, I tried to do the following :

objNet.MyObject = table RM_Customer_MSTR;

Or even this :

assign objNet.MyObject as reference to table RM_Customer_MSTR;

Both methods won't work.

In other words, is it possible to transfer a table buffer to a .NET object ? I think it's painful to do a field by field approach.

Himal Patel said...

Nice article, Thanks. I am getting success accessing System.Windows.Forms assembly but can't access my custom .net Assembly ? I created a simple .net dll with a single class. and added reference of it in dex.
SanScript can't recognize the name of the class.
Am i missing anything ?

Guillaume Simard said...


The DLL file containing the class you have created must be copied in the folder where Dexterity is installed. Then you should be able to compile.

If you deploy your solution in GP, the DLL file must be copied with your chunk in the root folder where GP is installed.

dynamics erp said...

Great! Thank you so much for sharing the wonderful article about Microsoft Dynamics GP 2015, I've read all previous parts and it was really big pleasure for me. So I'm looking forward to your new posts.

microsoft ax said...

Thank you for this details information with screenshots and how to do everything from begin right! It’s very helping me, because I'm a beginner :) and it was very interesting for me.