level up your biml: best practices and coding techniques (sqlsaturday sacramento)
TRANSCRIPT
![Page 1: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/1.jpg)
Level Up Your Biml:Best Practices andCoding Techniques
Cathrine Wilhelmsen
SQLSaturday Sacramento · July 23rd 2016
![Page 2: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/2.jpg)
Session Description
Is your Biml solution starting to remind you of a bowl of tangled spaghetti code? Good! That means you are solving real problems while saving a lot of time. The next step is to make sure that your solution does not grow too complex and confusing – you do not want to waste all that saved time on future maintenance!
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 3: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/3.jpg)
Code Management
…the next 60 minutes…
C# Classes and MethodsPractical Biml Coding
![Page 4: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/4.jpg)
Cathrine Wilhelmsen
@cathrinew
cathrinew.net
Data Warehouse ArchitectBusiness Intelligence Developer
![Page 5: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/5.jpg)
Biml vs.BimlScript
![Page 6: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/6.jpg)
Biml vs. BimlScript
Generate, control and manipulate Biml with C#
XML Language"Just plain text"
![Page 7: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/7.jpg)
Biml
Biml is an XML language – it's just plain text
Easy to read and write – but can be verbose
You probably don't want to write 50000 lines of Biml code manually...?
![Page 8: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/8.jpg)
Generating Biml
You can use any tool to generate Biml for you:• Text editor macros
• Excel
• PowerShell
• T-SQL
…but the recommended tool is BimlScript :)
![Page 9: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/9.jpg)
What is BimlScript?
Extend Biml with C# or VB code blocks
Import database structure and metadata
Loop over tables and columns
Expressions replace static values
Allows you to generate, control and manipulate Biml code
![Page 10: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/10.jpg)
BimlScript Code Nuggets
<# … #> Control Nuggets (Control logic)
<#= … #> Text Nuggets (Returns string)
<#@ … #> Directives (Compiler instructions)
<#+ … #> Class Nuggets (Create C# classes)
![Page 11: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/11.jpg)
BimlScript Syntax
<# var con = SchemaManager.CreateConnectionNode(...); #>
<# var metadata = con.GetDatabaseSchema(); #>
<Biml xmlns="http://schemas.varigence.com/biml.xsd">
<Packages>
<# foreach (var table in metadata.TableNodes) { #>
<Package Name="Load_<#=table.Name#>"></Package>
<# } #>
</Packages>
</Biml>
![Page 12: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/12.jpg)
BimlScript Syntax: Import metadata
<# var con = SchemaManager.CreateConnectionNode(...); #>
<# var metadata = con.GetDatabaseSchema(); #>
<Biml xmlns="http://schemas.varigence.com/biml.xsd">
<Packages>
<# foreach (var table in metadata.TableNodes) { #>
<Package Name="Load_<#=table.Name#>"></Package>
<# } #>
</Packages>
</Biml>
![Page 13: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/13.jpg)
BimlScript Syntax: Loop over tables
<# var con = SchemaManager.CreateConnectionNode(...); #>
<# var metadata = con.GetDatabaseSchema(); #>
<Biml xmlns="http://schemas.varigence.com/biml.xsd">
<Packages>
<# foreach (var table in metadata.TableNodes) { #>
<Package Name="Load_<#=table.Name#>"></Package>
<# } #>
</Packages>
</Biml>
![Page 14: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/14.jpg)
BimlScript Syntax: Replace static values
<# var con = SchemaManager.CreateConnectionNode(...); #>
<# var metadata = con.GetDatabaseSchema(); #>
<Biml xmlns="http://schemas.varigence.com/biml.xsd">
<Packages>
<# foreach (var table in metadata.TableNodes) { #>
<Package Name="Load_<#=table.Name#>"></Package>
<# } #>
</Packages>
</Biml>
![Page 15: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/15.jpg)
How does it work?
![Page 16: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/16.jpg)
Yes, but how does it work?
![Page 17: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/17.jpg)
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="LoadCustomer"></Package>
<Package Name="LoadProduct"></Package>
<Package Name="LoadSales"></Package>
</Packages>
</Biml>
![Page 18: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/18.jpg)
Biml ToolsComparison
![Page 19: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/19.jpg)
What do you need?
![Page 20: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/20.jpg)
BIDS Helper
Free open-source add-in for Visual Studio
60+ features for SSIS, SSAS and SSRS
Includes basic Biml package generator
bidshelper.codeplex.com
![Page 21: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/21.jpg)
…or you can use the new Biml tools…
![Page 22: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/22.jpg)
BimlExpress
Free add-in for Visual Studio
Code editor with syntax highlighting and Biml Intellisense
More frequent updates than BIDS Helper
varigence.com/bimlexpress
![Page 23: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/23.jpg)
BimlOnline
Free browser-based Biml editor
Code editor with Biml and C# Intellisense
Reverse-engineer from SSIS to Biml
bimlonline.com
![Page 24: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/24.jpg)
BimlStudio
Licensed full-featured development environment for Biml
Visual designer and metadata modeling
Full-stack automation and transformers
varigence.com/bimlstudio
![Page 25: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/25.jpg)
Biml Tools Comparison
Free
Biml Intellisense
"Black Box"
Free
Full Intellisense
Logical View
Licensed
Full IDE
Preview BimlScript
![Page 26: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/26.jpg)
CodeManagement
![Page 27: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/27.jpg)
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 28: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/28.jpg)
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
Works like an automated Copy & Paste
![Page 29: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/29.jpg)
Include Files
![Page 30: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/30.jpg)
Include Files
![Page 31: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/31.jpg)
Include Files
![Page 32: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/32.jpg)
CallBimlScript with Parameters
Works like a parameterized include (or stored procedure)
File to be called (callee) specifies accepted parameters:
<#@ property name="Parameter" type="String" #>
File that calls (caller) passes parameters:
<#=CallBimlScript("Common.biml", Parameter)#>
![Page 33: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/33.jpg)
CallBimlScript with Parameters
![Page 34: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/34.jpg)
CallBimlScript with Parameters
![Page 35: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/35.jpg)
CallBimlScript with Parameters
![Page 36: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/36.jpg)
CallBimlScript with Parameters
![Page 37: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/37.jpg)
CallBimlScript with Parameters
![Page 38: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/38.jpg)
Tiered Biml Files
Split Biml code in multiple files
Specify the tier per file by using the template directive:
<#@ template tier="2" #>
Biml is compiled from lowest to highest tier to:
• Solve logical dependencies
• Build solutions in multiple steps behind the scenes
![Page 39: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/39.jpg)
Tiered Biml Files
Biml is compiled step-by-step from lowest to highest tier
• Biml files with no BimlScript are implicitly tier 0
• Biml files with BimlScript are implicitly tier 1
For each tier, objects are added to the in-memory RootNode
Higher tiers can use objects defined in lower tiers
![Page 40: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/40.jpg)
What is this RootNode?
When working with Biml, the <Biml> root element contains collections of elements:
When working with BimlScript, the RootNode object contains collections of objects:
<Biml><Connections>...</Connections><Databases>...</Databases><Schemas>...</Schemas><Tables>...</Tables><Projects>...</Projects><Packages>...</Packages>
</Biml>
![Page 41: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/41.jpg)
Tiered Biml Files: Behind the Scenes
<#@ 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 (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/42.jpg)
Tiered Biml Files: Behind the Scenes
<#@ 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 (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/43.jpg)
Tiered Biml Files: Behind the Scenes
<#@ 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 (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/44.jpg)
Tiered Biml Files: Behind the Scenes
<#@ 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 (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/45.jpg)
Tiered Biml Files: Behind the Scenes
<#@ 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 (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/46.jpg)
Tiered Biml Files: Behind the Scenes
<#@ 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 (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/47.jpg)
Tiered Biml Files: Behind the Scenes
<#@ 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 (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/48.jpg)
Tiered Biml Files: Behind the Scenes
<#@ 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 (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/49.jpg)
Tiered Biml Files: Behind the Scenes
<#@ template tier="1" #><Connections>...</Connections>
<#@ template tier="2" #><Packages>...</Packages>
<#@ template tier="3" #><Package>...</Package>
![Page 50: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/50.jpg)
Tiered Biml Files: Behind the Scenes
<#@ template tier="1" #><Connections>...</Connections>
<#@ template tier="2" #><Packages>...</Packages>
<#@ template tier="3" #><Package>...</Package>
![Page 51: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/51.jpg)
Tiered Biml Files: Behind the Scenes
![Page 52: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/52.jpg)
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 53: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/53.jpg)
How does this actually work?
![Page 54: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/54.jpg)
Annotationsand ObjectTags
![Page 55: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/55.jpg)
Annotations and ObjectTags
Annotations and ObjectTags are Key / Value pairs
Use them to pass code between Biml files
Annotations: String / String
ObjectTags: String / Object
![Page 56: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/56.jpg)
Annotations
Create annotations:<OleDbConnection Name="Destination" ConnectionString="…">
<Annotations>
<Annotation Tag="Schema">AW2014</Annotation>
</Annotations>
</OleDbConnection>
Use annotations:RootNode.OleDbConnections["Destination"].GetTag("Schema");
![Page 57: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/57.jpg)
ObjectTags
Create ObjectTags:RootNode.OleDbConnections["Destination"].ObjectTag["Filter"]
= new List<string>{"Product","ProductCategory"};
Use ObjectTags:RootNode.OleDbConnections["Destination"].ObjectTag["Filter"];
![Page 58: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/58.jpg)
LINQ
![Page 59: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/59.jpg)
LINQ (Language-Integrated Query)
One language to query:
SQL Server Databases
Datasets
Collections
Two ways to write queries:
SQL-ish Syntax
Extension Methods
![Page 60: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/60.jpg)
LINQ Extension Methods
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 61: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/61.jpg)
LINQ Extension Methods Example
var numConnections = RootNode.Connections.Count()
foreach (var table in RootNode.Tables.Where(…))
if (RootNode.Packages.Any(…))
![Page 62: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/62.jpg)
LINQ Extension Methods Example
foreach (var table in RootNode.Tables.Where(…))
if (RootNode.Packages.Any(…))
But what do you put in here?
![Page 63: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/63.jpg)
Lambda Expressions
"A lambda expression is an anonymous function that you can use to create delegates or expression tree types"
![Page 64: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/64.jpg)
Lambda Expressions
…huh? o_O
![Page 65: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/65.jpg)
Lambda Expressions
table => table.Name == "Product"
![Page 66: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/66.jpg)
Lambda Expressions
table => table.Name == "Product"
The arrow is the lambda operator
![Page 67: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/67.jpg)
Lambda Expressions
table => table.Name == "Product"
Input parameter is on the left side
![Page 68: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/68.jpg)
Lambda Expressions
table => table.Name == "Product"
Expression is on the right side
![Page 69: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/69.jpg)
LINQ and Lambda
Chain LINQ Methods and use Lambda Expressions for simple and powerful querying of collections:
.Where(table => table.Schema.Name == "Production")
.OrderBy(table => table.Name)
![Page 70: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/70.jpg)
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 71: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/71.jpg)
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 72: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/72.jpg)
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 73: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/73.jpg)
LINQ: Sort collections
Reverse()
Returns the collection sorted in reverse order
RootNode.Tables.Reverse()
![Page 74: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/74.jpg)
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 75: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/75.jpg)
LINQ: Aggregate collections
Count()
Returns the number of elements in the collection
RootNode.Tables.Count()
RootNode.Tables.Count(t => t.Schema.Name == "Production")
![Page 76: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/76.jpg)
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 77: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/77.jpg)
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 78: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/78.jpg)
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 79: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/79.jpg)
LINQ: Check collections
Contains()
Returns true if collection contains element
RootNode.Databases.Contains(AdventureWorks2014)
![Page 80: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/80.jpg)
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 81: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/81.jpg)
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 82: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/82.jpg)
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 83: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/83.jpg)
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 84: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/84.jpg)
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 85: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/85.jpg)
Debugging
![Page 86: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/86.jpg)
Debugging Biml
BimlExpress is a "black box":• You only see the generated SSIS packages
• Not possible to view expanded BimlScript or compiled Biml
Add high-tier helper files to debug and save compiled Biml to files• Use Check Biml For Errors to debug without generating packages
![Page 87: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/87.jpg)
SaveCompiledBimlToFile.biml
Add the helper file to your project…
<#@ template tier="999" #><# System.IO.File.WriteAllText(
@"C:\Biml\CompiledBiml.xml", RootNode.GetBiml()
); #>
![Page 88: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/88.jpg)
SaveCompiledBimlToFile.biml
…with a high tier so it is executed as the last step
<#@ template tier="999" #><# System.IO.File.WriteAllText(
@"C:\Biml\CompiledBiml.xml", RootNode.GetBiml()
); #>
![Page 89: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/89.jpg)
SaveCompiledBimlToFile.biml
It creates a file…
<#@ template tier="999" #><# System.IO.File.WriteAllText(
@"C:\Biml\CompiledBiml.xml", RootNode.GetBiml()
); #>
![Page 90: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/90.jpg)
SaveCompiledBimlToFile.biml
…at the specified path…
<#@ template tier="999" #><# System.IO.File.WriteAllText(
@"C:\Biml\CompiledBiml.xml", RootNode.GetBiml()
); #>
![Page 91: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/91.jpg)
SaveCompiledBimlToFile.biml
…with all the Biml for all the objects in RootNode :)
<#@ template tier="999" #><# System.IO.File.WriteAllText(
@"C:\Biml\CompiledBiml.xml", RootNode.GetBiml()
); #>
![Page 92: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/92.jpg)
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 93: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/93.jpg)
How do you debug Biml?
![Page 94: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/94.jpg)
C# Classesand Methods
![Page 95: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/95.jpg)
C# Classes and Methods
BimlScript and LINQ not enough?
Need to reuse C# code?
Create your own classes and methods!
![Page 96: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/96.jpg)
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 97: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/97.jpg)
C# Classes and Methods: …to this
public static class HelperClass {
public static bool AnnotationTagExists(AstNode node, string tag) {
if (node.GetTag(tag) != "") {
return true;
}
return false;
}
}
![Page 98: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/98.jpg)
C# Classes and Methods: …to this
public static class HelperClass {
public static bool AnnotationTagExists(AstNode node, string tag) {
return (node.GetTag(tag) != "") ? true : false;
}
}
![Page 99: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/99.jpg)
C# Classes and Methods: …to this
public static class HelperClass {
public static bool AnnotationTagExists(AstNode node, string tag) {
return (node.GetTag(tag) != "");
}
}
![Page 100: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/100.jpg)
Where do you put your C# code?
Inline code nuggets
Included Biml files with code nuggets
Reference code files
![Page 101: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/101.jpg)
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 102: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/102.jpg)
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 103: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/103.jpg)
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 104: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/104.jpg)
ExtensionMethods
![Page 105: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/105.jpg)
Extension Methods
"Make it look like the method belongs to an object instead of a helper class"
![Page 106: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/106.jpg)
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 107: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/107.jpg)
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 108: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/108.jpg)
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 109: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/109.jpg)
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 110: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/110.jpg)
Code Management
…the past 60 minutes…
C# Classes and MethodsPractical Biml Coding
![Page 111: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/111.jpg)
Get things done
Start small
Start simple
Start with ugly code
Keep going
Expand
Improve
Deliver often
![Page 112: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/112.jpg)
Biml on Monday…
…BimlBreak the rest of the week
![Page 113: Level Up Your Biml: Best Practices and Coding Techniques (SQLSaturday Sacramento)](https://reader031.vdocument.in/reader031/viewer/2022030305/587542811a28abb8208b5493/html5/thumbnails/113.jpg)
@cathrinew
cathrinew.net
linkedin.com/in/cathrinewilhelmsen
slideshare.net/cathrinewilhelmsen
Biml resources and references:
cathrinew.net/biml