Wednesday, June 3, 2015

PowerShell scripts to apply configuration transformations for App.Config or Web.Config files

By default, Visual Studio provides configuration transformation for Web.config file. As well as, App.Config files can be transformed using a "SlowCheetah" or similar add-ons available in Visual Studio Gallary.

But there may be the cases where the configuration transformation is not supported by project template in Visual Studio, or in case, during TFS build, if you would want to create configuration transformation files for all of the release configurations, and not particular to a single release configuration. 

Through this, the same deployment package created during TFS build can be deployed on different environments.

Following is the PowerShell function I have created to achieve this -

#Apply config transformation
function applyConfigTransformation($src,$xdt,$dst)
{
Add-Type -Path "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\Web\Microsoft.Web.XmlTransform.dll"

try 
{
Write-Host 'applyConfigTransformation - Called'
Write-Host $src
$doc = New-Object Microsoft.Web.XmlTransform.XmlTransformableDocument
$doc.PreserveWhiteSpace = $true
Write-Host 'applyConfigTransformation - Load Called'
$doc.Load($src)
Write-Host 'applyConfigTransformation - Load completed'

$trn = New-Object Microsoft.Web.XmlTransform.XmlTransformation($xdt)

if ($trn.Apply($doc))
{
Write-Host 'applyConfigTransformation - $trn.Apply called'
$doc.Save($dst)
Write-Output "Output file: $dst"
Write-Host 'applyConfigTransformation - $trn.Apply completed'
}
else
{
throw "Transformation terminated with status False"
}
}
catch
{
Write-Output $Error[0].Exception

}

Following is how this function can be called -

$src = "C:Projects\MyWebApp\web.config"
$xdt = "C:Projects\MyWebApp\Configs\web.PreProduction.config"
$dst = "C:Projects\MyWebApp\web.PreProduction.config"
applyConfigTransformation $src $xdt $dst

Friday, April 24, 2015

Powershell Scripts to replace "setvar" variable in SQL-CMD script file before running the SQL scripts

Consider having a following SQL file, to run it an SQL-CMD mode:

C:\PowershellTest\CreateDatabase.sql

:setvar ImagesLocation 'C:\ImagesStore\'
........
Update ImagesStore
SET ImagesLocation = '$(ImagesLocation)'
.......


Now, when you run this sql-cmd scripts file during the deployments, you would also want to change the value of ImagesLocation variable, as the location may vary for different environment.

This can be achieved through using regular expressions in Powershell scripts.
In order to do that, you can create a following function in your Powershell deployment or pre-deployment scripts:

#function to replace cmdlet variable in SQL-CMD scripts (i.e., the ones assigned by :setvar). For strange reasons, command line variable assignments has lower precendence than the Sqlcmd scripts setvar. 
function replaceCmdletParameterValueInFile( $file, $key, $value ) {
    $content = Get-Content $file
    if ( $content -match ":setvar\s*$key\s*[\',\""][\w\d\.\:\\\-]*[\'\""_]" ) {
        $content -replace ":setvar\s*$key\s*[\',\""][\w\d\.\:\\\-]*[\'\""_]", ":setvar $key $value" |
        Set-Content $file     
    } else {
        Add-Content $file "$key = $value"
    }
}

Call this function in a following manner:

$scriptfile = "C:\PowershellTest\UpdateImagesLocation.sql"
replacePatternMatchingValueInFile $scriptfile"SET @ImagesLocation" "'\\datashare\appImages'"
replaceCmdParameterValueInFile "C:\PowershellTest\CreateDatabase.sql" "ImagesLocation" "'\\datashare\ImagesStore'"



As a result, the variable assignment for ImagesLocation would be changed to a different value in the sql file, and then that file can be used in Invoke-Sqlcmd to run it.

Powershell Scripts to replace Key value pair in SQL script file before running the SQL scripts

Consider having a following SQL file:

C:\PowershellTest\UpdateImagesLocation.sql

DECLARE @ImagesLocation NVARCHAR(max)
SET @ImagesLocation = 'C:\ImagesStore\'
.....

Now, when you run this sql scripts file during the deployments, you would also want to change the value of @ImagesLocation, as the location may vary for different environment.

This can be achieved through using regular expressions in Powershell scripts.
In order to do that, you can create a following function in your Powershell deployment or pre-deployment scripts:

#if an SQL file contains 'SET @variable_name=value', then this function can be called to replace value by actual value.
function replacePatternMatchingValueInFile( $file, $key, $value ) {
    $content = Get-Content $file
    if ( $content -match "^$key\s*=" ) {
        $content -replace "^$key\s*=.*", "$key = $value" |
        Set-Content $file     
    } else {
        Add-Content $file "$key = $value"
    }
}

Call this function in a following manner:

$scriptfile = "C:\PowershellTest\UpdateImagesLocation.sql"
replacePatternMatchingValueInFile $scriptfile"SET @ImagesLocation" "'\\datashare\appImages'"

As a result, the variable assignment for @ImagesLocation would be changed to a different value in the sql file.

Thursday, September 4, 2014

How to get name of the member (property name, etc) of a .NET class without using hard-coded/ magic strings

Consider following sample class:

public class City()
{
public int CityId { get; set; }
public string CityName { get; set; }
}

For various reasons, we need name of the properties/ field of the class in a string variable in our code. (For example, while binding ID and Text fields in DropDown controls, etc).

One option which is quite easy is to use the hard-coded strings directly.
For exmaple,
string idField = "CityId";

But the biggest disadvantage with the above approach is that whenever a field name or property name is changed, you will need to manually search and edit such string values. If you forget, it will not give you any compile error and you will only come to know when some exception will be thrown from your code.

In order to get rid of such issues, we should avoid using hard-coded values.

.NET 3.5 or higher provides support of encapsulating methods using Func helper which we can use to find a name of the property/ field.

using System;
using System.Linq.Expressions;

string idField =
                ((MemberExpression)((Expression<Func<City, int>>)(c => c.CityId)).Body).Member.Name;

string textField =
                ((MemberExpression)((Expression<Func<City, string>>)(c => c.CityName)).Body).Member.Name;

That means, now whenever some changes property "CityId" to "City_Id" or whatever, s/he must will need to change the above code otherwise it will always give a compile error.

Hope this helps

Monday, July 28, 2014

Exploring Abstract class (OOP concepts) in .NET - My Observations

Following are my observations with Abstract classes in .NET.
Feel free to write in comments if you think of more points to be added in this list, or if you want know more details about one or more points :)

  1. A class must be declared with "abstract" keyword
  2. It can contain complete implantation of methods
  3. Methods declared with "abstract" keyword can contain only declarations in Abstract class
  4. We cannot create an instance of abstract class
  5. Abstract class must be inherited
  6. A class derived from an abstract class must contain complete implementation of all abstract methods
  7. An abstract class can be derived from another abstract class
  8. Abstract class can be used as a parameter in methods
  9. If a class contains an abstract method, then that class must be marked as "abstract"
  10. An abstract class can have a constructor
  11. An abstract class can be inherited from a non-abstract class
  12. Constructor rules are also applied in case of abstract classes inheritance

P.S. - Please note all keyword and code snippets (if demonstrated above) are in C#.NET 

Thursday, July 24, 2014

HTML/ CSS: FIX >> Contents do not appear on new line after floated DIVs.

When we use "float:left", or "float:right" styles in DIVs, we come across an issue due to which the contents after those floated DIVs is not appearing in new line, rather they start appearing immediately besides the completion of last floated DIV.

In order to make further contents appearing in new line, add following DIV after floated DIVs:

<div style="clear: both;"></div>

How constructors are called while the classes are inherited: Exploring Object Oriented Programming and Concepts

Constructor Calls in Inheritance

Case study 1:

ClassA is a base class of ClassB.
Both classes have default constructor.

Observations:
While creating a new instance of ClassB (without parameters):
- it will first call default constructor of ClassB which will call constructor of ClassA.
- it will complete executing code of default constructor of ClassA.
- after that, it will complete executing code of default constructor of ClassB.

Case study 2:

ClassA is a base class of ClassB.
ClassA has no default constructor, but only parameterized constructor.
ClassB has a default constructor.


Observations: 
- it will not allow creating ClassA without a default constructor 
- if you want to do so, you need to call a base class constructor from ClassB's constructor: 
For example, following is the constructor of class A 
public ClassA(string someVariable) ..... some code .... }  
To allow ClassA without a default constructor, its all child classes should call base class constructor as below: 
public ClassB(): base("some value") 
or
public ClassB(string fullName): base(fullName)