Programming with Framework EDI .NET Hybrid

 

Framework EDI .NET hybrid provides all the development feature of Framework EDI (FREDI) in a .NET environment.  However, there are subtle differences in the names of the classes as well as the names of the properties and methods of the classes themselves.

To start programming using FREDI hybrid, take the following steps:

  1. Add the FREDI .NET hybrid reference
  2. Use namespace "Edidev.FrameworkEDI"
  3. Use ediDocument as the top level object
  4. Use Dispose method to release FREDI objects
  5. Use Set method to assign FREDI objects
  6. Use For..Next instead of For Each
  7. Samples

 

Use namespace "Edidev.FrameworkEDI"

After adding the FREDI .NET hybrid reference, any modules that implements the methods and properties of the FREDI must specify the "Edidev.FrameworkEDI" namespace.  Example,

In VB.NET,

Imports Edidev.FrameworkEDI

In C#,

using Edidev.FrameworkEDI;

 

Use ediDocument as the top level object

Create an instance of the top level object ediDocument for FREDI .NET hybrid.  The ediDocument encapsulates all classes containing methods and properties for implementation.  Example,

In VB.NET,

Dim oEdiDoc As ediDocument
oEdiDoc = New ediDocument

n C#,

ediDocument ediDoc = new ediDocument();

 

Use Dispose method to release FREDI objects

In the .NET environment, FREDI does not know when an object is no longer in use and therefore cannot determine when to clean up the object.  In the ActiveX/COM environment, when the client references an object in the server it attaches to the dispatch pointer, and detaches from the dispatch pointer when the object is no longer required.  When attaching and detaching, the reference count of the object is decremented and incremented respectively.  A reference count of zero indicates that the object is no longer in use, and FREDI knows to clean up the object.  In .NET, managed code is cleaned up by the garbage collector,  and FREDI .NET objects referenced by clients are .NET managed objects.  However, a FREDI managed object wraps an unmanaged resource which pins the managed object such that it cannot be cleaned up by the garbage collector during optimization.  When the managed object is no longer in use it should be released by explicitly calling the .NET Dispose method, which unpins the unmanaged resource, and the managed object can then be cleaned up by the garbage collector at optimization time.  For example, in the Visual Basic statement

Set oSegment = Nothing

The equivalent VB.NET code using the Dispose method is as follows,

oSegment.Dispose

 

Use Set method to assign FREDI objects

As previously mentioned above, in the .NET environment, FREDI does not know when an object is no longer in use and therefore cannot determine when to clean up the object.  When a variable, already containing an object, is being assigned another object, the previous object is not destroyed until the application exits.  For example, in the statement

oSegment = oSegment.Next

the managed object in the oSegment variable on the left side of the equal sign is no longer used because it will be overwritten by the managed object returned by the method oSegment.Next.  The managed object that was originally in oSegment is left in memory and does not get cleaned up until the application exits.  In the ActiveX/COM environment, the COM client notifies the server that the previous object is no longer in use by releasing the object's dispatch pointer.  This is not the case in .NET.  Eventually multiple calls to this statement will continue to accumulate objects, consume memory and result in performance degradation.

To overcome this problem, explicitly call the Set method of the object.  Example, in VB.NET

ediDataSegment.Set(oSegment, oSegment.Next)

The Set method is actually equivalent to the following call using the Dispose method.  Here we use the Dispose method to indicate that the previous object is no longer used by the client.

Dim oSegment As ediDataSegment

Dim oSegment2 As ediDataSegment

:

oSegment2 = oSegment.Next

oSegment.Dispose

oSegment = oSegment2

Or in general, 

If Not oSegment Is Nothing Then

    oSegment.Dispose

End If

oSegment = <Assign new object>

 

Use For..Next instead of For Each 

Currently the Framework EDI .NET hybrid does not support enumerators.  In Visual Basic, for example, one could enumerate through a collection using the For Each statement, and the enumerator would get an item at each instance of the loop.  For example,

Dim oElement As ediDataElement
Dim oElements As ediDataElements

For Each oElement in oElements

Debug.Print "Element ID: " & oElement.ID

Next

The code above has to be replaced using the For..Next loop.  For example,

Dim oElement As ediDataElement
Dim oElements As ediDataElements
Dim i As Integer

For i = 1 To oElements.Count

ediDataElement.Set(oElement, oElements.DataElement(i))
Debug.Print "Element ID: " & oElement.ID

Next

 

Samples

Sample program to translate an EDI file in VB.NET.  This sample demonstrates how to use the Set() method.

Sample program to translate an EDI file in VB.NET.  This sample demonstrates how to use the Dispose() method.