Daniel Little: Coding Blog

A blog about the web and technology [javascript, Mvc, Asp.net, C#] by Daniel [Lavinski]
RSS
12/17 2013

Moving to Ghost

I’ve moved to the ghost blogging platform you can find my new blog at https://lavinski.me/

Comments
09/12 2013

Git - When to Merge

Many of use git pull and merge frequently in an effort to keep a branch up to date while working a feature. However it’s actually recommended not to merge into a feature branch until it’s complete and ready to be integrated back into the development branch.

The reason for this is that merge is a is a semantic action and actually has additional meaning than just update this branch. For example a feature branch should only be concerned with adding a single feature to a point in time. This makes development easier, you don’t want the system changing around you as you develop a feature. Merge does a great job at integrating branches when they’re ready (As long as you’re project isn’t completely different [if it is you have bigger problems]). It also lets you become more flexible, branches can merge with other branches or features without getting changes they’re not interested in.

It’s also good to remember if you just want to sync with a remote such as the origin you can use fetch which avoids the implicit merge of pull.

Comments
09/04 2013

TechEd AU Day 1

The first day at TechEd offered two main sessions the developer kick off and the Keynote. The developer kick off touched on a new Visual Studio 2013 feature called Browser Link for ASP.NET and Team Foundation Service.

The kick off started by introducing One ASP.NET which is the unification of all web related .NET technologies, frameworks and libraries like MVC, SignalR and Web Forms can be mixed and matched however you like and all fall under the one ASP.NET umbrella. There has been a range of improvement to intellisense across the board particularly in html and CSS editors. The main highlight was the introduction to Browser Link by the creator of Web Essentials for Visual Studio.

Brower link enables a persistent link from Visual Studio to whatever web browser you use. It enables a few interesting scenarios the first of which being changes made to CSS are instantly reflected in all the browsers connected by Browser Link. Edits to razor files require a little more than a save (one key combo) because behind the scenes you’re telling each connected browser to refresh the page but you neve have to leave Visual Studio. Imagine you’re trying to iron out a few CSS quirks across two or more browsers. You can have two browsers open at the same time and CSS changes appear instantly on both browsers as soon as you save the file making comparisons efficient and easy.

Browser link also lets you do the inverse, you can use the web browser to jump to the exact line in your razor template that an element corresponds to via the inspector (Ctrl-Alt-I). You can also go one step further, ctrl-alt-d enables edit mode which can let you do thinks link edit html or Action Links and other Html helpers which are updated in real time and this all works in any browser.

The next session introduced Team Foundation Service which has a set of features coming to the next version of Team Foundation Server. There are a few interesting features including git support, a free tier for up to 5 users, a scrum style backlog and Trello-esk cards. Also notably was the addition of an integrated persistent chat room through which you can share news and chat with other members but also see general activity in the project such as build and code review events.

The primary keynote had a few speakers, the main talk was from the CEO of Yammer. This was the one of the best talks I’ve seen and it talked about organisations and change. The theme was Blink and you’ll miss it. The rate at which technology is changing is happening so fast that it’s becoming even more important for people and organisations to be able to learn and react faster. If you wait too long it’s already irrelevant. This means instead of long release cycles where failure means 2-3 years of work goes down the drain, moving to pushing updates every month. This means the cost of failure is much smaller as well. What’s worse changing one month of work or one year’s worth. This is so important for organisations and this quote by Jack Welch describes it perfectly.

"If the rate of change on the outside exceeds the rate of change on the inside, the end is near."

There have been an increasing number of disruptive changes happening in the last 5 or so years. Communication has enabled modern society to move ideas and information much faster than ever before. The problem we see in companies these days is that people inside organisations don’t communicate very well, especially across departments. The outside world is moving so fast because it’s a network of self motivated people and we need to bring that into our companies and organisations. These types of organisations are called Responsive Organisations and you can read more about what it means to be one at http://www.theresponsiveorg.com/manifesto.

Comments
08/03 2013

John Carmack Keynote

John Carmack’s keynote this year was a fantastic talk about software engineering. Parts three and four of the keynote touched on all the key hot topics including Code Quality, Functional Programming, Static Typing vs Dynamic Typing, Static Analysis and Garbage Collection. Defiantly worth watching if you’re into professional development, and if not it’s still worth watching.

Comments
07/03 2013

My PowerShell Profile

This is my PowerShell profile, it does a few nice things such as add open and edit commands to the shell. Most importantly though it customizes my prompt to shorten it while still showing how deep I am in the directory hierarchy. I’ve also set up poshgit as well as a few other helpers and functions.

function Open($path) {
    explorer $path
}

function Edit {

    [CmdletBinding()]
    Param(

        [Parameter(Mandatory = $False, ValueFromPipeline = $True, ValueFromRemainingArguments = $True, Position = 0)] 
        $File
    )

    Process {
        $app = "C:\Program Files (x86)\Notepad++\notepad++.exe"

        if ($File -ne $null) {
            $parameters = '"' + $File + '"'

            $options = New-Object "System.Diagnostics.ProcessStartInfo"
            $options.FileName = $app
            $options.Arguments = $parameters
            $options.WorkingDirectory = $pwd

            $temp = [Diagnostics.Process]::Start($options).WaitForInputIdle(500)
        }
        Invoke-Item $app
    }
}

# This was already here
function elevate-process
{
    $file, [string]$arguments = $args;
    $psi = new-object System.Diagnostics.ProcessStartInfo $file;
    $psi.Arguments = $arguments;
    $psi.Verb = "runas";

    $psi.WorkingDirectory = get-location;
    [System.Diagnostics.Process]::Start($psi);
}
set-alias sudo elevate-process;


# WebGet
function get-html([string]$url)
{
    $webClient = (New-Object System.Net.WebClient);
    $webClient.DownloadString($url);
}

function shorten-path([string] $path) { 
   $loc = $path.Replace($HOME, '~') 
   # remove prefix for UNC paths
   $loc = $loc -replace '^[^:]+::', ''
   # make path shorter like tabs in Vim,
   # handle paths starting with \\ and . correctly
   return ($loc -replace '\\(\.?)([^\\])[^\\]*(?=\\)','\$1$2') 
}

function prompt { 
    # our theme
    $cdelim = [ConsoleColor]::DarkCyan 
    $chost = [ConsoleColor]::Green 
    $cloc = [ConsoleColor]::Cyan 
    $default = [ConsoleColor]::White 

    # Reset color, which can be messed up by Enable-GitColors
    $Host.UI.RawUI.ForegroundColor = $default

    write-host (shorten-path (pwd).Path) "" -n -f $cloc
    Write-VcsStatus
    return '> '
}

# gci . *.cs -Recurse | select-string . | Group Filename | Measure-Object Count -Min -Max -Average
function CountLines($directory)
{
    $pattern = "*.cs"
    $directories = [System.IO.Directory]::GetDirectories($directory)
    $files = [System.IO.Directory]::GetFiles($directory, $pattern)

    $lineCount = 0

    foreach($file in $files) {
        $lineCount += [System.IO.File]::ReadAllText($file).Split("`n").Count
    }

    foreach($subdirectory in $directories) {
        $lineCount += CountLines $subdirectory
    }

    $lineCount
}

$msbuildPath = "C:\Windows\Microsoft.NET\Framework\v4.0.30319\"
$gitPath = (Get-Item "Env:LocalAppData").Value + "\GitHub\PortableGit_93e8418133eb85e81a81e5e19c272776524496c6\cmd\"

$env:path += ";$gitPath;$msbuildPath;"


function Enable-Git {
    . 'C:\Users\me\Documents\WindowsPowerShell\Modules\posh-git\profile.example.ps1'
}

#Write-Host "For git use 'Enable-Git'"

function Load-Profile
{
    . $Profile
}

function Edit-Profile
{
    edit "C:\Users\me\Documents\WindowsPowerShell\Profile.ps1"
}

function Git-Log([switch]$OneLine, $Length)
{
    $Length = 1000;

    if ($OneLine) {
        git log --pretty=oneline -n $Length
    } else {
        git log -n $Length
    }   
}
Comments
04/29 2013

Testing emails in .NET

Testing emails can sometimes be a bit challenging. smtp4dev is a small program that intercepts all received emails so you can quickly send and view emails from any application that uses it as the SMTP server.

For a .NET application just add this to the config file.

<mailSettings>
    <smtp>
        <network host="localhost"/>
    </smtp>
</mailSettings>

Now all emails that the application sends will appear for your viewing pleasure in the email list (pictured below).

Comments
04/20 2013

Loading Settings From web.config

It’s always a good idea to be as consistent as possible when you’re writing code. So when you’re using settings from a configuration file it’s a good idea to avoid sprinkling ConfigurationManager.AppSettings["MySetting"] around the codebase, especially if you’ve making multiple calls to it.

A great way to provide consistency and to remove this duplication is to create a settings class either one static global one or multiple instance classes which I’ll then manage with dependency injection. Then I load all the settings from configuration into that class on start up.

I’ve also written a little library that makes use of reflection to make this even easier.

Once my settings are in my config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>   
    <appSettings>
        <add key="Domain" value="example.com" />
        <add key="PagingSize" value="30" />
        <add key="Invalid.C#.Identifier" value="test" />
    </appSettings>
</configuration>

I make a static or instance class depending on my needs. For simple applications with only a few settings one static class is fine.

private static class Settings
{
    public string Domain { get; set; }

    public int PagingSize { get; set; }

    [Named("Invalid.C#.Identifier")]
    public string ICID { get; set; }

}

Then using my library call either Inflate.Static or Inflate.Instance and the cool thing is I can use any key value source.

using Fire.Configuration;

Inflate.Static( typeof(Settings), x => ConfigurationManager.AppSettings[x] );


var settings = new SpecificSettings();

Inflate.Instance( settings, x => ConfigurationManager.AppSettings[x] );

All the code for this is in bitbucket at https://bitbucket.org/Lavinski/fire

There is even a nuget package http://nuget.org/packages/Fire/

Install-Package Fire
Comments
04/10 2013

Wildcards hostname in your hosts file

If you’ve ever wanted to setup a wildcard entry in your hosts file so you don’t have to add an entry for every IIS binding you’ll be happy to know there is a way.

The trick however is not to use the hosts file but a local DNS proxy instead. Acrylic DNS is a free and open source which you can download here. You use it in much the same way you would use a hosts file.

Stackoverflow has a great answer detailing how to set it up:

Configuring Acrylic DNS Proxy

To configure Acrylic DNS Proxy, install it from the above link then go to:

  1. Start
  2. Programs
  3. Acrilic DNS Proxy
  4. Config
  5. Edit Custom Hosts File

Add the folowing lines on the end of the file:

127.0.0.1   *.localhost
127.0.0.1   *.local

Restart the Acrilic DNS Proxy service:

  1. Start
  2. Programs
  3. Acrilic DNS Proxy
  4. Config
  5. Restart Acrilic Service

You will also need to adjust your DNS setting in you network interface settings:

  1. Start
  2. Control Panel
  3. Network and Internet
  4. Network Connections
  5. Local Area Connection Properties
  6. TCP/IPv4

Set “Use the following DNS server address”:

Preferred DNS Server: 127.0.0.1
Comments
03/20 2013

Multiple https bindings in IIS7 for developers

When working with websites that require https (SSL) developers always ending up facing the problem: Why can’t you use hostnames with https. This is because hostname is encrypted so IIS needs to establish the SSL connection before it can even read the hostname.

However we can get around this problem by making use of a wildcard certificate. Then it won’t matter what the hostname is and we can happily use SSL on all our internal dev sites.

You can’t actually use IIS to setup a wildcard certificate so I’ll be using a bit of Powershell to move things along.

The first step is to create the self signed certificate. Any clients will also have to trust this certificate.

$cert = New-SelfSignedCertificate -DnsName "*.company.internal" -CertStoreLocation cert:\LocalMachine\My
Export-Certificate -Cert $cert -FilePath "company.internal.cer"

$password = ConvertTo-SecureString -String "SecretHeHe" -Force –AsPlainText
Export-PfxCertificate -Cert $cert -FilePath "company.internal.pfx" -Password $password

Next add the bindings to IIS, this script will add a http and https binding for each combination of site and environment.

$sites = @(
    "website"
)

$envs = @(
    "test",
    "stage"
)

Import-Module WebAdministration
foreach ($name in $sites) {

    foreach ($env in $envs) {
        $name = "$name ($env)"

        $siteName = "$name ($env)"
        New-WebBinding -Name $siteName -Port 80 -Protocol http -HostHeader "$env.$name.company.internal"
        New-WebBinding -Name $siteName -Port 443 -Protocol https -HostHeader "$env.$name.company.internal"
    }
}

Just one thing left, setup IIS to use the certificate. To do that open IIS and import the certificate. Then select the open one of the https bindings and select the certificate (it won’t matter which one, any site on that port will use it).

Comments
03/08 2013

Properties Visual Studio passes to MSBuild

It always takes me ages to kind what properties Visual Studio makes available to the MSBuild project files so here’s the full set.

$(RemoteMachine) 
 
Set to the value of the Remote Machine property on the Debug property page. See Changing Project Settings for a C/C++ Debug Configuration for more information.
 

$(Configuration) 
 
The name of the current project configuration (for example, "Debug").
 

$(Platform) 
 
The name of current project platform (for example, "Win32").
 

$(ParentName) 
 
(Deprecated.) Name of the item containing this project item. This will be the parent folder name, or project name.
 

$(RootNameSpace) 
 
The namespace, if any, containing the application.
 

$(IntDir) 
 
Path to the directory specified for intermediate files relative to the project directory. This path should have a trailing slash. This resolves to the value for the Intermediate Directory property.
 

$(OutDir) 
 
Path to the output file directory, relative to the project directory. This path should have a trailing slash. This resolves to the value for the Output Directory property.
 

$(DevEnvDir) 
 
The installation directory of Visual Studio 2010 (defined as drive + path); includes the trailing backslash '\'.
 

$(InputDir) 
 
(Deprecated; migrated.) The directory of the input file (defined as drive + path); includes the trailing backslash '\'. If the project is the input, then this macro is equivalent to $(ProjectDir).
 

$(InputPath) 
 
(Deprecated; migrated.) The absolute path name of the input file (defined as drive + path + base name + file extension). If the project is the input, then this macro is equivalent to $(ProjectPath).
 

$(InputName) 
 
(Deprecated; migrated.) The base name of the input file. If the project is the input, then this macro is equivalent to $(ProjectName).
 

$(InputFileName) 
 
(Deprecated; migrated.) The file name of the input file (defined as base name + file extension). If the project is the input, then this macro is equivalent to $(ProjectFileName).
 

$(InputExt) 
 
(Deprecated; migrated.) The file extension of the input file. It includes the '.' before the file extension. If the project is the input, then this macro is equivalent to $(ProjectExt).
 

$(ProjectDir) 
 
The directory of the project (defined as drive + path); includes the trailing backslash '\'.
 

$(ProjectPath) 
 
The absolute path name of the project (defined as drive + path + base name + file extension).
 

$(ProjectName) 
 
The base name of the project.
 

$(ProjectFileName) 
 
The file name of the project (defined as base name + file extension).
 

$(ProjectExt) 
 
The file extension of the project. It includes the '.' before the file extension.
 

$(SolutionDir) 
 
The directory of the solution (defined as drive + path); includes the trailing backslash '\'.
 

$(SolutionPath) 
 
The absolute path name of the solution (defined as drive + path + base name + file extension).
 

$(SolutionName) 
 
The base name of the solution.
 

$(SolutionFileName) 
 
The file name of the solution (defined as base name + file extension).
 

$(SolutionExt) 
 
The file extension of the solution. It includes the '.' before the file extension.
 

$(TargetDir) 
 
The directory of the primary output file for the build (defined as drive + path); includes the trailing backslash '\'.
 

$(TargetPath) 
 
The absolute path name of the primary output file for the build (defined as drive + path + base name + file extension).
 

$(TargetName) 
 
The base name of the primary output file for the build.
 

$(TargetFileName) 
 
The file name of the primary output file for the build (defined as base name + file extension).
 

$(TargetExt) 
 
The file extension of the primary output file for the build. It includes the '.' before the file extension.
 

$(VSInstallDir) 
 
The directory into which you installed Visual Studio 2010. 

This property contains the version of the targeted Visual Studio, which might be different that the host Visual Studio. For example, when building with $(PlatformToolset) = v90, $(VSInstallDir) contains the path to the Visual Studio 2008 installation.
 

$(VCInstallDir) 
 
The directory into which you installed Visual C++ 2010. 

This property contains the version of the targeted Visual C++, which might be different that the host Visual Studio. For example, when building with $(PlatformToolset) = v90, $(VCInstallDir) contains the path to the Visual C++ 2008 installation.
 

$(FrameworkDir) 
 
The directory into which the .NET Framework was installed.
 

$(FrameworkVersion) 
 
The version of the .NET Framework used by Visual Studio. Combined with $(FrameworkDir), the full path to the version of the .NET Framework use by Visual Studio.
 

$(FrameworkSDKDir) 
 
The directory into which you installed the .NET Framework. The .NET Framework could have been installed as part of Visual Studio 2010 or separately.
 

$(WebDeployPath) 
 
The relative path from the web deployment root to where the project outputs belong. Returns the same value as RelativePath.
 

$(WebDeployRoot) 
 
The absolute path to the location of . For example, c:\inetpub\wwwroot.
 

$(SafeParentName) 
 
(Deprecated.) The name of the immediate parent in valid name format. For example, a form is the parent of a .resx file.
 

$(SafeInputName) 
 
(Deprecated.) The name of the file as a valid class name, minus file extension.
 

$(SafeRootNamespace) 
 
(Deprecated.) The namespace name in which the project wizards will add code. This namespace name will only contain characters that would be permitted in a valid C++ identifier.
 

$(FxCopDir) 
 
The path to the fxcop.cmd file. The fxcop.cmd file is not installed with all Visual C++ editions.

Source: http://msdn.microsoft.com/en-us/library/c02as0cs.aspx

Comments
1 of 4 Next