Muokkaa

Jaa


Coding a Custom Foreach Enumerator

Applies to: SQL Server SSIS Integration Runtime in Azure Data Factory

After you have created a class that inherits from the ForEachEnumerator base class, and applied the DtsForEachEnumeratorAttribute attribute to the class, you must override the implementation of the properties and methods of the base class to provide your custom functionality.

For a working sample of a custom enumerator, see Developing a User Interface for a Custom ForEach Enumerator.

Initializing the Enumerator

You can override the InitializeForEachEnumerator method to cache references to the connection managers defined in the package, and to cache references to the events interface that you can use to raise errors, warnings, and informational messages.

Validating the Enumerator

You override the Validate method to verify that the enumerator is correctly configured. If the method returns Failure, the enumerator and the package that contains the enumerator will not be executed. The implementation of this method is specific to each enumerator, but if the enumerator relies on Variable or ConnectionManager objects, you should add code to verify that these objects exist in the collections that are provided to the method.

The following code example demonstrates an implementation of Validate that checks for a variable specified in a property of the enumerator.

private string variableNameValue;  
  
public string VariableName  
{  
    get{ return this.variableNameValue; }  
    set{ this.variableNameValue = value; }  
}  
  
public override DTSExecResult Validate(Connections connections, VariableDispenser variableDispenser, IDTSInfoEvents infoEvents, IDTSLogging log)  
{  
    if (!variableDispenser.Contains(this.variableNameValue))  
    {  
        infoEvents.FireError(0, "MyEnumerator", "The Variable " + this.variableNameValue + " does not exist in the collection.", "", 0);  
            return DTSExecResult.Failure;  
    }  
    return DTSExecResult.Success;  
}  
Private variableNameValue As String  
  
Public Property VariableName() As String  
    Get   
         Return Me.variableNameValue  
    End Get  
    Set (ByVal Value As String)   
         Me.variableNameValue = value  
    End Set  
End Property  
  
Public Overrides Function Validate(ByVal connections As Connections, ByVal variableDispenser As VariableDispenser, ByVal infoEvents As IDTSInfoEvents, ByVal log As IDTSLogging) As DTSExecResult  
    If Not variableDispenser.Contains(Me.variableNameValue) Then  
        infoEvents.FireError(0, "MyEnumerator", "The Variable " + Me.variableNameValue + " does not exist in the collection.", "", 0)  
            Return DTSExecResult.Failure  
    End If  
    Return DTSExecResult.Success  
End Function  

Returning the Collection

During execution, the ForEachLoop container calls the GetEnumerator method of the custom enumerator. In this method, the enumerator creates and populates its collection of items, and then returns the collection. The ForEachLoop then iterates the items in the collection, and executes its control flow for each item in the collection.

The following example shows an implementation of GetEnumerator that returns an array of random integers.

public override object GetEnumerator()  
{  
    ArrayList numbers = new ArrayList();  
  
    Random randomNumber = new Random(DateTime.Now);  
  
    for( int x=0; x < 100; x++ )  
        numbers.Add( randomNumber.Next());  
  
    return numbers;  
}  
Public Overrides Function GetEnumerator() As Object  
    Dim numbers As ArrayList =  New ArrayList()   
  
    Dim randomNumber As Random =  New Random(DateTime.Now)   
  
        Dim x As Integer  
        For  x = 0 To  100- 1  Step  x + 1  
        numbers.Add(randomNumber.Next())  
        Next  
  
    Return numbers  
End Function  

See Also

Creating a Custom Foreach Enumerator
Developing a User Interface for a Custom ForEach Enumerator