107

Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Embed Size (px)

Citation preview

Page 1: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)
Page 2: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Level Up Your Biml:Best Practices and Coding Techniques

Cathrine Wilhelmsen

Page 3: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Session Description

You already know how to use Biml to build a staging environment in an hour, so

let's dive straight into some of the more advanced features of Biml.

Attend this session for an overview of Biml best practices and coding techniques.

Learn how to centralize and reuse code with include files and the CallBimlScript

method. Make your code easier to read and write by utilizing LINQ (Language-

Integrated Queries). Share code between files by using Annotations and

ObjectTags. And finally, if standard Biml is not enough to solve your problems,

you can create your own C# helper classes and extension methods to implement

custom logic.

Start improving your code today and level up your Biml in no time!

Page 4: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Cathrine Wilhelmsen

@cathrinew

cathrinewilhelmsen.net

Data Warehouse Architect

Business Intelligence Developer

Page 5: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

You…

Know basic Biml and BimlScript

Completed BimlScript.com lessons

Have created a staging environment

…?

Page 6: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Today…

Code Management

Practical Biml Programming

C# Classes and Methods

… :)

Page 7: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Quick Recap of

Basic Biml

Page 8: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

What is Biml?

Business Intelligence Markup Language

Easy to read and write XML language

Describes business intelligence objects:

• Databases, Schemas, Tables, Views, Columns

• SSIS Packages

• SSAS Cubes

• Metadata

Page 9: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

What do you need?

Page 10: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

…or you can use the new Biml tools

Page 11: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

How does it work?

Page 12: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

dem

o t

ime!

Let's generate

some packages!

Page 13: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Ok, so we can go from Biml to SSIS…

Page 14: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

…can we go from SSIS to Biml?

Page 15: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Yes! :)

Page 16: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

dem

o t

ime!

Let's reverse-engineer

some packages!

Page 17: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

The magic is in the BimlScript!

Extend Biml with C# or VB code blocks

Import database structure and metadata

Loop over tables and columns

Expressions replace static values

BimlScript allows you to control and manipulate Biml code

Page 18: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

BimlScript Code Nuggets

<# … #> Control Nuggets (Control logic)

<#= … #> Text Nuggets (Returns string)

<#@ … #> Directives (Compiler instructions)

<#+ … #> Class Nuggets (Create C# classes)

Page 19: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

How does it work?

Page 20: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Yes, but how does it work?

Page 21: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Yes, but how does it actually work?

<Biml xmlns="http://schemas.varigence.com/biml.xsd"><Packages>

<# foreach (var table in RootNode.Tables) { #><Package Name="Load_<#=table.Name#>"></Package>

<# } #></Packages>

</Biml> <Biml xmlns="http://schemas.varigence.com/biml.xsd"><Packages>

<Package Name="Load_Customer"/><Package Name="Load_Product"/><Package Name="Load_Sales"/>

</Packages></Biml>

Page 22: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Biml vs. BimlScript

Automate, control and

manipulate Biml with C#

Flat XML"Just text"

Page 23: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

dem

o t

ime!

Let's generate

a lot of packages!

Page 24: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Code

Management

Page 25: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Don't Repeat Yourself

Move common code to separate files

Centralize and reuse in many projects

Update code once for all projects

1. Include files

2. CallBimlScript with Parameters

3. Tiered Biml files

Page 26: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

BimlExpress vs. BimlOnline / BimlStudio

"Black Box"

Only SSIS packages visible

Visual Editors

All in-memory objects visible

Page 27: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Include Files

Include common code in multiple files and projects

Can include many file types: .biml .txt .sql .cs

Use the include directive

<#@ include file="CommonCode.biml" #>The directive will be replaced by the included file

Include pulls code from the included file into the main file

Works like an automated Copy & Paste

Page 28: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Include Files

Page 29: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Include Files

Page 30: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Include Files

Page 31: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

CallBimlScript with Parameters

Works like a parameterized include

File to be called (callee) specifies input parameters it accepts

<#@ property name="Parameter" type="String" #>File that calls (caller) passes input parameters

<#=CallBimlScript("CommonCode.biml", Parameter)#>

CallBimlScript pushes parameters from the caller to the callee, and the

callee returns code

Page 32: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

CallBimlScript with Parameters

Page 33: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

CallBimlScript with Parameters

Page 34: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

CallBimlScript with Parameters

Page 35: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

CallBimlScript with Parameters

Page 36: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

CallBimlScript with Parameters

Page 37: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Tiered Biml Files

Split Biml code in multiple files and use the template directive:

<#@ template tier="1" #>

Create objects in-memory from lowest to highest tier to:

• Solve logical dependencies

• Simulate manual workflows

In-memory objects are added to the RootNode

Higher tiers can get objects added to RootNode in lower tiers

Page 38: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

What is this RootNode?

The RootNode contains all in-memory objects:

• Connections, Databases, Schemas, Tables

• Projects, Packages

• Annotations, Metadata

Query the RootNode to loop over collections:<# foreach (var table in RootNode.Tables) { #>

Query the RootNode to get specific objects:<#=RootNode.Tables["Product"].Schema#>

Page 39: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Inside the Black Box: Tiered Biml Files

<#@ template tier="1" #><Connections>...</Connections>

<#@ template tier="2" #><Packages>...</Packages>

<#@ template tier="3" #><Package>...</Package>

Page 40: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Inside the Black Box: Tiered Biml Files

<#@ template tier="1" #><Connections>...</Connections>

<#@ template tier="2" #><Packages>...</Packages>

<#@ template tier="3" #><Package>...</Package>

Page 41: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Inside the Black Box: Tiered Biml Files

<#@ template tier="1" #><Connections>...</Connections>

<#@ template tier="2" #><Packages>...</Packages>

<#@ template tier="3" #><Package>...</Package>

Page 42: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Inside the Black Box: Tiered Biml Files

<#@ template tier="1" #><Connections>...</Connections>

<#@ template tier="2" #><Packages>...</Packages>

<#@ template tier="3" #><Package>...</Package>

Page 43: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Inside the Black Box: Tiered Biml Files

<#@ template tier="1" #><Connections>...</Connections>

<#@ template tier="2" #><Packages>...</Packages>

<#@ template tier="3" #><Package>...</Package>

Page 44: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Inside the Black Box: Tiered Biml Files

<#@ template tier="1" #><Connections>...</Connections>

<#@ template tier="2" #><Packages>...</Packages>

<#@ template tier="3" #><Package>...</Package>

Page 45: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Inside the Black Box: Tiered Biml Files

<#@ template tier="1" #><Connections>...</Connections>

<#@ template tier="2" #><Packages>...</Packages>

<#@ template tier="3" #><Package>...</Package>

Page 46: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Inside the Black Box: Tiered Biml Files

<#@ template tier="1" #><Connections>...</Connections>

<#@ template tier="2" #><Packages>...</Packages>

<#@ template tier="3" #><Package>...</Package>

Page 47: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Inside the Black Box: Tiered Biml Files

<#@ template tier="1" #><Connections>...</Connections>

<#@ template tier="2" #><Packages>...</Packages>

<#@ template tier="3" #><Package>...</Package>

Page 48: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Inside the Black Box: Tiered Biml Files

<#@ template tier="1" #><Connections>...</Connections>

<#@ template tier="2" #><Packages>...</Packages>

<#@ template tier="3" #><Package>...</Package>

Page 49: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Inside the Black Box: Tiered Biml Files

Page 50: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

How do you use Tiered Biml files?

1. Create Biml files with specified tiers

2. Select all the tiered Biml files

3. Right-click and click Generate SSIS Packages

1

2

3

Page 51: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

dem

o t

ime!

How does this

actually work?

Page 52: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Debugging Biml

Page 53: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Debugging Biml

BimlExpress is a "black box":• You can only see the generated SSIS packages

• It is not possible to see the compiled Biml first

Add a high-tier helper file to save compiled, flat Biml to file• Check Biml For Errors to save flat Biml without generating packages

Page 54: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

SaveFlatBimlToFile.biml

Add the helper file to your project…

<#@ template tier="999" #><# System.IO.File.WriteAllText(

@"C:\Biml\FlatBiml.xml", RootNode.GetBiml()

); #>

Page 55: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

SaveFlatBimlToFile.biml

…with a high tier so it is executed as the last step

<#@ template tier="999" #><# System.IO.File.WriteAllText(

@"C:\Biml\FlatBiml.xml", RootNode.GetBiml()

); #>

Page 56: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

SaveFlatBimlToFile.biml

It creates a file…

<#@ template tier="999" #><# System.IO.File.WriteAllText(

@"C:\Biml\FlatBiml.xml", RootNode.GetBiml()

); #>

Page 57: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

SaveFlatBimlToFile.biml

…at the specified path…

<#@ template tier="999" #><# System.IO.File.WriteAllText(

@"C:\Biml\FlatBiml.xml", RootNode.GetBiml()

); #>

Page 58: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

SaveFlatBimlToFile.biml

…with all the Biml for all the object in RootNode

<#@ template tier="999" #><# System.IO.File.WriteAllText(

@"C:\Biml\FlatBiml.xml", RootNode.GetBiml()

); #>

Page 59: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

How do you use this helper file?

1. Create the helper file

2. Select all the Biml files and the helper file

3. Right-click and click Check Biml For Errors

1

2

3

Page 60: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

dem

o t

ime!

How is this

helper file used?

Page 61: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Annotations and

ObjectTags

Page 62: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Annotations and ObjectTags

Biml Annotations != SSIS Annotations

Annotations are string/string Key/Value pairs

ObjectTags are string/object Key/Value pairs

Use Annotations and ObjectTags to pass code

between Biml files

Page 63: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Annotations

Create annotations:<OleDbConnection Name="Destination" ConnectionString="…"><Annotations><Annotation Tag="Schema">AW2014</Annotation>

</Annotations></OleDbConnection>

Use annotations:<# var destinationSchema =

RootNode.OleDbConnections["Destination"].GetTag("Schema"); #>

Page 64: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

ObjectTags

Create ObjectTags:<# RootNode.OleDbConnections["Destination"].ObjectTag["TableFilter"] = new List<string> {"Product","ProductSubcategory","ProductCategory"};

#>

Use ObjectTags:<#var TableFilter = (List<string>)RootNode.OleDbConnections["Destination"].ObjectTag["TableFilter"];

#>

Page 65: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

LINQ

Page 66: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

LINQ (Language-Integrated Query)

One language to query:

SQL Server Databases

XML Documents

Datasets

Collections

Two ways to write queries:

SQL-like Syntax

Extension Methods

Page 67: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

LINQ Extension Methods

..and many, many more!

Sort

OrderBy, ThenBy

Filter

Where, OfType

Group

GroupBy

Aggregate

Count, Sum

Check Collections

All, Any, Contains

Get Elements

First, Last, ElementAt

Project Collections

Select, SelectMany

Page 68: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

LINQ Extension Methods

var numConnections = RootNode.Connections.Count()

foreach (var table in RootNode.Tables.Where(…))

if (RootNode.Packages.Any(…))

Page 69: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

LINQ and Lambda expressions

Use lambda expressions to filter or specify values:

.Where(table => table.Schema.Name == "Production").OrderBy(table => table.Name)

Page 70: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

LINQ and Lambda expressions

For each element in the collection…

.Where(table => table.Schema.Name == "Production").OrderBy(table => table.Name)

Page 71: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

LINQ and Lambda expressions

…evaluate a criteria or get a value:

.Where(table => table.Schema.Name == "Production").OrderBy(table => table.Name)

Page 72: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

LINQ: Filter collections

Where()Returns the filtered collection with all elements that meet the criteria

RootNode.Tables.Where(t => t.Schema.Name == "Production")

OfType()Returns the filtered collection with all elements of the specified type

RootNode.Connections.OfType<AstExcelOleDbConnectionNode>()

Page 73: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

LINQ: Sort collections

OrderBy()Returns the collection sorted by key…

RootNode.Tables.OrderBy(t => t.Name)

ThenBy()…then sorted by secondary key

RootNode.Tables.OrderBy(t => t.Schema.Name).ThenBy(t => t.Name)

Page 74: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

LINQ: Sort collections

OrderByDescending()Returns the collection sorted by key…

RootNode.Tables.OrderByDescending(t => t.Name)

ThenByDescending()…then sorted by secondary key

RootNode.Tables.OrderBy(t => t.Schema.Name).ThenByDescending(t => t.Name)

Page 75: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

LINQ: Sort collections

Reverse()Returns the collection sorted in reverse order

RootNode.Tables.Reverse()

Page 76: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

LINQ: Group collections

GroupBy()Returns a collection of key-value pairs where each value is a new collection

RootNode.Tables.GroupBy(t => t.Schema.Name)

Page 77: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

LINQ: Aggregate collections

Count()Returns the number of elements in the collection

RootNode.Tables.Count()RootNode.Tables.Count(t => t.Schema.Name == "Production")

Page 78: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

LINQ: Aggregate collections

Sum()Returns the sum of the (numeric) values in the collection

RootNode.Tables.Sum(t => t.Columns.Count)

Average()Returns the average value of the (numeric) values in the collection

RootNode.Tables.Average(t => t.Columns.Count)

Page 79: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

LINQ: Aggregate collections

Min()Returns the minimum value of the (numeric) values in the collection

RootNode.Tables.Min(t => t.Columns.Count)

Max()Returns the maximum value of the (numeric) values in the collection

RootNode.Tables.Max(t => t.Columns.Count)

Page 80: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

LINQ: Check collections

All()Returns true if all elements in the collection meet the criteria

RootNode.Databases.All(d => d.Name.StartsWith("A"))

Any()Returns true if any element in the collection meets the criteria

RootNode.Databases.Any(d => d.Name.Contains("DW"))

Page 81: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

LINQ: Check collections

Contains()Returns true if collection contains element

RootNode.Databases.Contains(AdventureWorks2014)

Page 82: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

LINQ: Get elements

First()Returns the first element in the collection (that meets the criteria)

RootNode.Tables.First()RootNode.Tables.First(t => t.Schema.Name == "Production")

FirstOrDefault()Returns the first element in the collection or default value (that meets the criteria)

RootNode.Tables.FirstOrDefault()RootNode.Tables.FirstOrDefault(t => t.Schema.Name == "Production")

Page 83: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

LINQ: Get elements

Last()Returns the last element in the collection (that meets the criteria)

RootNode.Tables.Last()RootNode.Tables.Last(t => t.Schema.Name == "Production")

LastOrDefault()Returns the last element in the collection or default value (that meets the criteria)

RootNode.Tables.LastOrDefault()RootNode.Tables.LastOrDefault(t => t.Schema.Name == "Production")

Page 84: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

LINQ: Get elements

ElementAt()Returns the element in the collection at the specified index

RootNode.Tables.ElementAt(42)

ElementAtOrDefault()Returns the element in the collection or default value at the specified index

RootNode.Tables.ElementAtOrDefault(42)

Page 85: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

LINQ: Project collections

Select()Creates a new collection from one collection

A list of table names:

RootNode.Tables.Select(t => t.Name)

A list of table and schema names:

RootNode.Tables.Select(t => new {t.Name, t.Schema.Name})

Page 86: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

LINQ: Project collections

SelectMany()Creates a new collection from many collections and merges the collections

A list of all columns from all tables:

RootNode.Tables.SelectMany(t => t.Columns)

Page 87: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

dem

o t

ime!

How is LINQ used

in Biml projects?

Page 88: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

C#

Classes and

Methods

Page 89: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

C# Classes and Methods

BimlScript and LINQ not enough?

Need to reuse C# code?

Create your own classes and methods!

Page 90: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

C# Classes and Methods: From this…

public static class HelperClass {public static bool AnnotationTagExists(AstNode node, string tag) {if (node.GetTag(tag) != "") {return true;

} else {return false;

}}

}

Page 91: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

C# Classes and Methods: …to this

public static class HelperClass {public static bool AnnotationTagExists(AstNode node, string tag) {return (node.GetTag(tag) != "") ? true : false;

}} * For bools you can just use:

return (node.GetTag(tag) != "");

But in this example we'll use the verbose, SSIS-like syntax

because it can be reused with other data types, like…

Page 92: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

C# Classes and Methods: …or this

public static class HelperClass {public static string AnnotationTagExists(AstNode node, string tag) {return (node.GetTag(tag) != "") ? "Yes" : "No";

}}

Page 93: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Where do you put your code?

Inline code nuggets

Included Biml files with code nuggets

Reference code files

Page 94: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

C# Classes and Methods: Inline

<Biml xmlns="http://schemas.varigence.com/biml.xsd"><# foreach (var table in RootNode.Tables) { #> <# if (HelperClass.AnnotationTagExists(table, "SourceSchema")) { #>...

<# } #><# } #>

</Biml>

<#+public static class HelperClass {public static bool AnnotationTagExists(AstNode node, string tag) {

return (node.GetTag(tag) != "") ? true : false;}

}#>

Page 95: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

C# Classes and Methods: Included Files

<#@ include file="HelperClass.biml" #>

<Biml xmlns="http://schemas.varigence.com/biml.xsd"><# foreach (var table in RootNode.Tables) { #> <# if (HelperClass.AnnotationTagExists(table, "SourceSchema")) { #>...

<# } #><# } #>

</Biml>

<#+public static class HelperClass {public static bool AnnotationTagExists(AstNode node, string tag) {

return (node.GetTag(tag) != "") ? true : false;}

}#>

Page 96: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

C# Classes and Methods: Code Files

<#@ code file="HelperClass.cs" #>

<Biml xmlns="http://schemas.varigence.com/biml.xsd"><# foreach (var table in RootNode.Tables) { #> <# if (HelperClass.AnnotationTagExists(table, "SourceSchema")) { #>...

<# } #><# } #>

</Biml>public static class HelperClass {public static bool AnnotationTagExists(AstNode node, string tag) {

return (node.GetTag(tag) != "") ? true : false;}

}

Page 97: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

C#

Extension

Methods

Page 98: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Extension Methods

"Make it look like the method belongs to

an object instead of a helper class"

Page 99: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Extension Methods: From this…

<#@ code file="HelperClass.cs" #>

<Biml xmlns="http://schemas.varigence.com/biml.xsd"><# foreach (var table in RootNode.Tables) { #> <# if (HelperClass.AnnotationTagExists(table, "SourceSchema")) { #>...

<# } #><# } #>

</Biml>public static class HelperClass {public static bool AnnotationTagExists(AstNode node, string tag) {

return (node.GetTag(tag) != "") ? true : false;}

}

Page 100: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Extension Methods: …to this

<#@ code file="HelperClass.cs" #>

<Biml xmlns="http://schemas.varigence.com/biml.xsd"><# foreach (var table in RootNode.Tables) { #> <# if (HelperClass.AnnotationTagExists(table, "SourceSchema")) { #>...

<# } #><# } #>

</Biml>public static class HelperClass {public static bool AnnotationTagExists(this AstNode node, string tag) {

return (node.GetTag(tag) != "") ? true : false;}

}

Page 101: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Extension Methods: …to this

<#@ code file="HelperClass.cs" #>

<Biml xmlns="http://schemas.varigence.com/biml.xsd"><# foreach (var table in RootNode.Tables) { #> <# if (table.AnnotationTagExists("SourceSchema")) { #>...

<# } #><# } #>

</Biml>public static class HelperClass {public static bool AnnotationTagExists(this AstNode node, string tag) {

return (node.GetTag(tag) != "") ? true : false;}

}

Page 102: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Extension Methods: …to this :)

<#@ code file="HelperClass.cs" #>

<Biml xmlns="http://schemas.varigence.com/biml.xsd"><# foreach (var table in RootNode.Tables.Where(t =>

t.AnnotationTagExists("SourceSchema")) { #>...

<# } #><# } #>

</Biml>public static class HelperClass {public static bool AnnotationTagExists(this AstNode node, string tag) {

return (node.GetTag(tag) != "") ? true : false;}

}

Page 103: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Questions?

Page 104: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Get things done

Start small

Start simple

Start with ugly code

Keep going

Expand

Improve

Deliver often

Page 105: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

Izpolnite anketo!

Vam je bilo predavanje všeč?

Ste se naučili kaj novega?

Vaše mnenje nam veliko pomeni!

Da bo NT konferenca prihodnje leto še boljša, vas

prosimo, da izpolnite anketo o zadovoljstvu, ki jo

najdete v svojem NTK spletnem profilu.

Page 106: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)

@cathrinew

cathrinewilhelmsen.net

linkedin.com/in/cathrinewilhelmsen

[email protected]

slideshare.net/cathrinewilhelmsen

Biml resources and references:

cathrinewilhelmsen.net/biml

Page 107: Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)