Lance Larsen - .NET Architect Madison Wisconsin

All posts in Code

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

CFNeed to setup a “Created” DateTime field in your SQL table that is auto-computed to add the current date and time that the field is inserted into the database? 

If you’re doing so in SQL – in the “Default Value or Binding” you just need to enter GETDATE() or GETUTCDATE() – insert a record, and you’ll see the date/time value automatically added to that column.  “…that was easy…”

But how to do the same thing when using Entity Framework Code First?

No big deal here either – as long as you add the couple lines of code described here…

Read more

imageimageI love to say “…if it’s not in NuGet, it’s DEAD to me…” – and for the most part that is very true. :)

If you don’t know what NuGet is – you MUST – look at Introducing NuGet: the smart way to bring bits into your projects

However, as I and many developers need to continue to support IE7/8 – we can’t update to the latest versions of jQuery or Bootstrap, as jQuery version 2.+ and Bootstrap version 3.+ no longer support IE7/8…

Also considering jQuery is continuing to update their 1.* version with updates – how do we handle that and continue to use NuGet inside of Visual Studio to show us when there are updates that we DO want to install?

Here’s the solution!

In your “packages.config” file – add the allowedVersions tag to both as follows ( noting that the right parenthesis looks strange, but it is correct – it means ‘less than version X’ )…

<package id="jQuery" version="1.10.2" allowedVersions="[1.9.1,2)" targetFramework="net45" />


<package id="bootstrap" version="2.3.1" allowedVersions="[2.3.1,3)" targetFramework="net45" />

imageNow when you open up NuGet ( noting that you need to close and reopen VS and the project ) – you see the following!

For this, we were on Bootstrap 2.3.1 – and only saw an upgrade to 3.0 – but now we see we can upgrade to the latest 2.3.2!  And we’re currently at the latest jQuery 1.10.2 – so correctly don’t see an update – but we will when one comes out. :)

I love NuGet… :)

So we will pick up from where we left off in “Custom Configuration Pattern 101” – we are looking to elegantly consume complex configuration settings in App.config and Web.config files – in this case we are looking to step up our game and consume the following…

<CertificateSection authenticationMode="Test">
    <add certificateType="cipherCertificate"
    <add certificateType="serviceCertificate"

We will continue our “CustomConfigurationPattern” console application!

Read more

So if you’ve even needed to store custom data in your App.config or Web.config files before – you’ve probably used the ConfigurationManager.AppSettings for name/value pair data – but what if you have more complex settings that you want to store in the config files?

Such as…

<Certificate findValue="7776F8E373CF19EEEC88293031893B8D985792F8" 

What do we do?  We extend .NET’s System.Configuration.ConfigurationSection of course… :)

Read more

Last we saw our intrepid developers they were creating their first “Code First + Migrations” 101 project – today we join them facing the dreaded “we want to do code first, but already have an existing database!!!”.  Can they survive? Absolutely, and here’s how… :)

imageFirst we need to add “Entity Framework Power Tools” – click on “Tools” –> “Extensions and Updates…” –> select “Online” and then search for “Entity Framework Power Tools” –> “Install” and we have added some great new EF Power Tools features!

Now that we have EF Power Tools installed – if we right-click on our project we should see “Entity Framework” as one of the options – and off of that we have “Reverse Engineer Code First” and “Customize Reverse Engineer Templates”.

Read more

Had the need to randomly shuffle an IEnumerable collection that I was bringing back through Entity Framework – so did some searching and came across a couple solutions – some that were completely crazy – but took the best ideas, combined them and then wrapped the final solution into an extension method that I wanted to share. :)  Code is as follows… ( BTW – I love extension methods btw == code & forget )

   1:  public static class IenumerableExtensions
   2:  {
   3:      public static IEnumerable<T> Randomize<T>(this IEnumerable<T> enumerable)
   4:      {
   5:          var r = new Random();
   6:          return enumerable.OrderBy(x => r.Next()).ToList();
   7:      }
   8:  }

imageSo just by doing the following:

var widgets = db.Widgets.AsEnumerable().Randomize();

Results are randomized, and it Works Beautifully! 

Wrote the following console app – based on my previous Entity Framework “Code First + Migration” 101 blog article – that shows the Randomize Shuffle – so you can Download and try it for yourself!

Good Coding!

Download Code ( ~2.7M )

So for those who haven’t used Microsoft Entity Framework’s “Code First” approach to application development – it’s a very powerful approach that allows us developers to spin up a project with a database back end in record time!  Early on it had the distinct disadvantage that it didn’t allow for changes in your code to migrate to the database – but, as we will see, that feature has also matures and is pretty sweet now… :)

So where to start?

Open Visual Studio, click “New” –> “Project” –> “Console Application” and create your application…

imageAdd Entity Framework to the project.  We’ll do this by using “NuGet”.  Right-Click on the project, select “Manage NuGet Packages…”, enter “EntityFramework” ( usually right at the top ) and click “Install”.  This will add all of the “entity-goodness” to your project!



Right-Click on your project and click “Add” –> “Class…” –> name it “Model.cs” and insert the following code…

Read more

What’s the Holy Grail of Application Profiling?  Well, it should be…

  • Rich Data – Giving us detailed logging ability, recording which method is being called, where it’s being called from, when it starts, when it stops, overall execution time –and- if any exceptions are thrown.
  • Low Impact – Adds just a fraction of a second to the methods being profiled, highly performant. ( MS-Word says “performant” isn’t a word – Wiktionary and I disagree <grin> )
  • Minimal Code – Follows the D.R.Y. principle ( Don’t Repeat Yourself), less code == easier maintenance and generally better performance.
  • Loosely Coupled – Easy to add to a project, with little to no code added to the existing code.

So… How do we create the Holy Grail of Application Profiling?  We use a very cool technology called AOP ( Aspect Oriented Programming ). 

AOP complements Object-Oriented Programming (OOP), giving us even more tools that increase modularity and allowing us to elegantly handle “crosscutting concerns” such as logging, exception handling and other common challenges.  One of the most popular AOP tools is PostSharp (, which allows us to “IL Weave” our profiling code ( the act of adding code as IL instructions into the assemblies at compile time )!  Sweet…

So let’s start at the end – what do we get? 

We’ll if you start a brand new “MVC 4 Web Project” in Visual Studio – by adding a couple references and ONE line in your AssemblyInfo.cs file…


[assembly: ProfileAspect(AttributeTargetTypes = "ProfileWeb.Controllers.*")]

We can start logging to SQL and, for example, easily extract really rich data on pages hit and their load times such as…


Nice… Not bad for ONE line of code… Intrigued?  I know you would be…  :)

Download the Profiler Code (44 MB)

Read more

So if you haven’t looked – in VisualStudio projects under the “Properties” folder – there is a file called “AssemblyInfo.cs” – it contains many useful assembly attributes for your project including AssemblyTitle, AssemblyCompany, AssemblyCopyright, AssemblyVersion, etc.

My goal was to make a simple and elegant static class to retrieve these values – on top of that wanted to be able to retrieve the values within another project ( class library ) from the calling project, ie. what is the AssemblyTitle that is calling this class library.  I was specifically doing this for logging purposes – wanting to capture the project and version.  Accomplished both as follows…

How to get the values…

   1:  string company = ApplicationInfo.Company;
   2:  string product = ApplicationInfo.Product;
   3:  string copyright = ApplicationInfo.Copyright;
   4:  string trademark = ApplicationInfo.Trademark;
   5:  string title = ApplicationInfo.Title;
   6:  string description = ApplicationInfo.Description;
   7:  string configuration = ApplicationInfo.Configuration;
   8:  string fileversion = ApplicationInfo.FileVersion;
   9:  Version version = ApplicationInfo.Version;
  10:  string versionFull = ApplicationInfo.VersionFull;
  11:  string versionMajor = ApplicationInfo.VersionMajor;
  12:  string versionMinor = ApplicationInfo.VersionMinor;
  13:  string versionBuild = ApplicationInfo.VersionBuild;
  14:  string versionRevision = ApplicationInfo.VersionRevision;


Read more