Skip to content

Don't Repeat Your Biml - C# Extension Methods

Biml Wheel.

In a previous blog post, we looked at how to use C#/VB Code Files in Biml. There are several benefits to moving custom C# code into separate files. It allows you to reuse that code across multiple projects and solutions. You can maintain the code in your editor of choice, taking advantage of intellisense and syntax highlighting. And finally, my personal favorite: you can create custom extension methods.

In this post, we will look at how to simplify our Biml projects by creating and using C# extension methods. We will build on the examples from the previous C#/VB Code Files in Biml blog post.

C# Extension Methods in Biml

But first, what are extension methods? In short, you can add your own custom methods to existing objects, without modifying the original object. The extension method will look like it belongs to that object, just like any other built-in method. You can almost think of extension methods as add-ins or plugins to the core Biml language 🤓

<!-- Did you know that GetBiml() is actually an extension method? You can call it like this: -->
<#=package.GetBiml()#>
 
<!-- You can call custom extension methods in the same way: -->
<#=package.GetSomeCustomInformation()#>

So how do we create an extension method?

Example: From Helper Methods to Extension Methods

In the previous C#/VB Code Files in Biml blog post, we created the following code file with a helper class and helper method:

NamingStandards.cs - Helper Class and Helper Method:

using Varigence.Languages.Biml.Table;
 
public static class NamingStandards {
 
  public static string GetTableName(AstTableNode table) {
    return table.Schema.Name.ToUpper() + "_" + table.Name.ToUpper();
  }
 
}

To use this helper method, we first had to specify the name of the Helper class, then pass in the table as a parameter:

<#=NamingStandards.GetTableName(table)#>

This works perfectly, but it is not the prettiest solution. We can do better! Let’s turn this helper method into an extension method. All we have to do is add the this modifier to the first parameter, where the first parameter is the object you’re creating the extension method for:

NamingStandards.cs - Extension Method:

using Varigence.Languages.Biml.Table;
 
public static class NamingStandards {
 
  public static string GetTableName(this AstTableNode table) {
    return table.Schema.Name.ToUpper() + "_" + table.Name.ToUpper();
  }
 
}

That’s it! I promise! Yes, it really is as simple as… this 😎 Anyway!

Now that we have our updated code file and a reference to it, we can use the new extension method without having to worry about which class it belongs to:

<#@ code file="NamingStandards.cs" #>
<Biml xmlns="http://schemas.varigence.com/biml.xsd">
  <Packages>
    <# foreach (var table in RootNode.Tables) { #>
      <Package Name="<#=table.GetTableName()#>" ConstraintMode="Linear">
        <Tasks>
          <ExecuteSQL Name="Truncate <#=table.GetTableName()#>" ConnectionName="Destination">
            <DirectInput>TRUNCATE TABLE dbo.<#=table.GetTableName()#>
            </DirectInput>
          </ExecuteSQL>
          <Dataflow Name="Load <#=table.GetTableName()#>">
            <Transformations>
              <OleDbSource Name="Source <#=table.SsisSafeScopedName#>" ConnectionName="Source">
                <ExternalTableInput Table="<#=table.SchemaQualifiedName#>" />
              </OleDbSource>
              <OleDbDestination Name="Destination <#=table.GetTableName()#>" ConnectionName="Destination">
                <ExternalTableOutput Table="dbo.<#=table.GetTableName()#>" />
              </OleDbDestination>
            </Transformations>
          </Dataflow>
        </Tasks>
      </Package>
    <# } #>
  </Packages>
</Biml>

Summary

In this post, we looked at how to turn a helper class and helper method into an extension method. By adding the keyword this, we can call the extension method on an object, instead of calling the helper method and passing in the object as a parameter.

Share or Comment?

About the Author

Professional headshot of Cathrine Wilhelmsen.Cathrine Wilhelmsen is a Microsoft Data Platform MVP, international speaker, author, blogger, organizer, and chronic volunteer. She loves data and coding, as well as teaching and sharing knowledge - oh, and sci-fi, gaming, coffee and chocolate 🤓