Lance Larsen - .NET Architect Madison Wisconsin

All posts in Code

imageMicrosoft is creating some very cool “Cognitive Services” that we as developers can easily utilize in our applications to accomplish some amazing results with a few simple calls to these Azure service.

If you haven’t setup the Cognitive Services – take a look at Setting up Azure Cognitive Services.

We’ll start with the “Face [Detection] API

“Detect one or more human faces in an image and get back face rectangles for where in the image the faces are, along with face attributes which contain machine learning-based predictions of facial features. … The face attribute features available are: Age, Gender, Pose, Smile, and Facial Hair…” 

As my eventual goal is to integrate these into Hololens for you guys – we’ll be doing this integration into UWP (Universal Windows Platform) – but starting with desktop apps.

Completed project is up on GitHub @ FaceApiUwp

Ok, let’s dive right into code!  Fire up Visual Studio…

Read more


imageFYI – here’s a trick so we don’t need to specify our local computer’s name in our web.config connectionString – because if you have multiple developers working on the same code – they each have to have different web.config versions with their individual computer names.

Generally we see…

<connectionStrings>
     <add name=”MyContext” connectionString=”Data Source=MYCOMPUTER; Initial Catalog=MyDatabase; Integrated Security=True; MultipleActiveResultSets=True” providerName=”System.Data.SqlClient” />
</connectionStrings>

What we want…

<connectionStrings>
    <add name=”MyContext” connectionString=”Data Source=.; Initial Catalog=MyDatabase; Integrated Security=True; MultipleActiveResultSets=True” providerName=”System.Data.SqlClient” />
</connectionStrings>



How?

1) Open your “Sql Server Configuration Manager” application.
2) Expand your “Sql Server Network Configuration”.
3) Right-click on “TCP/IP”.
4) Scroll down to “TCP Dynamic Ports” (which will be blank by default) and enter “0” which enables it. 
5) Restart your SQL service and you’re DONE!

Thanks Travis Feirtag (@therobotgeek) for this trick!


imageSome days CSS needs a little love thrown it’s way… My OCD and I have an agreement – I do what it wants and it makes me a better maker. :)

Needed an elegant solution for customizing the standard <UL><LI> html bullets, and strongly dislike having to create a graphic for this – so after some non-trivial searching though not-so-good solutions came across some nice CSS to make this happen with FontAwesome – and always good to share nice solutions.  Enjoy.

Here’s the jsFiddle ( http://jsfiddle.net/lancelarsen/e08tp2ga/ )

Read more


imageHad the need to create a nice UX (user experience) by moving a boostrap “div” up and down within a bootstrap “row” collection on a website.

Always love sharing some elegant code, so check out this jsFiddle to see it working…

https://jsfiddle.net/lancelarsen/814rteua/

https://jsfiddle.net/lancelarsen/w4fob4pv/ (variant for nested bootstrap divs)

Read more


( Download Code )

Was looking for a simple and elegant way to log some metric for methods (mainly method name, start time, stop time and elapsed time) – I’ve done this in PostSharp in my article “Holy Grail of Application Profiling with AOP” – but client wasn’t using them.

I’ve used reflection with success in the past, but was having some issues with Async methods not giving their real method names, was getting “MoveNext” instead — so sleuthed around for an alternative and found some awesome C# attributes that I’d never seen…

[CallerMemeberName] – gets the method name calling the attribute
[CallerFilePathAttribute] – gets the path to the C# file
[CallerLineNumberAttribute] – gets the line of the method in code

Combining those with the C# Stopwatch Class – and using the Dispose patter that I love because you can easily code the “using” statement around what you’re looking at ( see below ) – came up with the following cool code that – as always – I love to share.

Emphasis on elegant, to call this code – you can easily do the following…

using (StopwatchLog.Track())
{
   // Your code here...
}

It collects the information about executing method and logs metrics when the stopwatch log is disposed.

Read more


So AngularJs is pretty sweet – especially considering the power and elegance of how you can use “filters” to change the way values appear in the UI.  There is a built in “number” filter that you can apply to a number value and have it always display a set number of decimal places, i.e. “1″ -> “1.0″  But, we wanted a filter that did the following…

“1.56″ => “1.6″
“1.5″ => “1.5″
“1.0″ => “1″
“1″ => “1″

So I wrote a simple custom filter, setDecmial! 

As I love jsFiddle – here’s the live working version: http://jsfiddle.net/lancelarsen/Tx7Ty/

If anyone has suggestions for further improvements, always love to keep making the code better. :) 

<div ng-app="myApp">
    <div ng-controller="Ctrl">
        setDecimal Filter (1): <span>{{val | setDecimal:1}}</span><br>
        setDecimal Filter (2): <span>{{val | setDecimal:2}}</span><br>
    </div>
</div>

var app = angular.module('myApp', []);

app.filter('setDecimal', function ($filter) {
    return function (input, places) {
        if (isNaN(input)) return input;
        // If we want 1 decimal place, we want to mult/div by 10
        // If we want 2 decimal places, we want to mult/div by 100, etc
        // So use the following to create that factor
        var factor = "1" + Array(+(places > 0 && places + 1)).join("0");
        return Math.round(input * factor) / factor;
    };
});

app.controller('Ctrl', function ($scope) {
    $scope.val = 1.56;
});

Always loved JavaScript and client side scripting ( where appropriate <grin> ) – so when we had a new green field project to start, looked to what the best-in-class technologies were – settled on AngularJS (@angularjs). 

It is a client-side MVC (Model-View-Controller) pattern and it rocks for making SPA (Single-Page-Applications) that emphasize responsiveness.  Take a look at either the AngularJS site, or there are a couple great AngularJS videos on Pluralsight – specifically “AngularJS Fundamentals” and “AngularJS for .NET Developers”.

I know that I have a ton to learn about AngularJS – and wanted to share what I’m learning along the way…

Specifically, have a page that has a list of items to be displayed – and want to filter them by a parameter based on what the user clicked.  So in AngularJS we can separate showing the items to be displayed into one controller – the “CollectionController” – and the filters to be applied into another controller – the “FilterController”.  Found many examples / discussion on how to communicate between the controller, but some seemed to be way overkill or confusing – so got the following code crafted – and wanted to share as it turned out pretty slick! :)

To boot, have the live code running on JSFiddle – which again, if you haven’t played with is WAY awesome!

http://jsfiddle.net/lancelarsen/qjkyM/

Read more


mysqlAs a primarily Microsoft SQL Server developer, when playing around with MySQL database ( specifically using MySQL Workbench ) wanted to be able to press F5 to execute the SQL commands that I was writing, by default you need to press Control-Enter or Control-Shift-Enter – but there is no GUI interface to remap keys shortcuts like there is in most Microsoft programs… so… found that the following worked and wanted to share!

Locate your “main_menu.xml” MySQL configuration file – mine was in “C:\Program Files (x86)\MySQL\MySQL Workbench CE 6.0.6\data\main_menu.xml”.  Find the following XML.
 

   1:  <value type="object" struct-name="app.MenuItem" id="com.mysql.wb.menu.query.execute_current_statementwin"> 
   2:    <link type="object" key="owner" struct-name="app.MenuItem">com.mysql.wb.menu.query</link> 
   3:    <value type="string" key="caption">Execute Current Statement</value> 
   4:    <value type="string" key="name">query.execute_current_statement</value> 
   5:    <value type="string" key="command">builtin:query.execute_current_statement</value> 
   6:    <value type="string" key="itemType">action</value> 
   7:    <value type="string" key="shortcut">Modifier+Return</value>
   8:    <value type="string" key="platform">windows</value>
   9:  </value> 

Replace the “shortcut” xml (line 7 above) with the following, exit and reopen MySQL Workbench – and you’re rocking with F5!

<value type="string" key="shortcut">F5</value>


imageYou should almost always store your date/times in your database as UTC – you may think it’s not important now, but as soon as your application needs to display times to users in different time zones ( to paraphrase Yoda ) …it will be, it will be… 

But do you need to know and store your user’s timezone to display it correctly? 

Nope, JavaScript and momentjs to the rescue…! :)

Read more


large_jquery_logoIn my blog “NuGet – Limiting updates for jQuery and Bootstrap” – we looked at continuing to utilize NuGet to keep us up to date on the latest jQuery 1.x version ( even though 2.x is out ).  Reason being that we needed to stick with 1.x because we are still supporting IE 7/8. :(

So… jQuery 1.x and 2.x have feature parity – but you want to use jQuery 2.x when you can, simply because it’s a smaller download as they were able to take out a lot of IE7/8 “fixes”.  Note, jQuery’s post on conditionally include one or the other based on the version of the browser.

But we also like to use the cool MVC bundling and minification feature – can we have the best of all worlds? Oh ya… :)

Read more