Thursday, November 13, 2014

Binding redirects at runtime

Hopfully the C# audience can read this F#, but this is how you would do it =)
// https://dotnetfiddle.net/UnS2vU
printfn "starting up"
open System
open System.Reflection

let RedirectAssembly shortName (targetVersion : Version) publicKeyToken = 
    let rec onResolveEvent = new ResolveEventHandler( fun sender evArgs ->
        let requestedAssembly = 
            AssemblyName(evArgs.Name)
        if requestedAssembly.Name <> shortName 
        then 
            printfn "redirect firing for %s" requestedAssembly.Name; Unchecked.defaultof<Assembly>
        else 
            printfn 
                "Redirecting assembly load of %s ,\tloaded by %s" 
                evArgs.Name 
                (if evArgs.RequestingAssembly = null then 
                     "(unknown)"
                 else 
                     evArgs.RequestingAssembly.FullName)
            requestedAssembly.Version <- targetVersion
            requestedAssembly.SetPublicKeyToken (AssemblyName(sprintf "x, PublicKeyToken=%s" publicKeyToken).GetPublicKeyToken())
            requestedAssembly.CultureInfo <- System.Globalization.CultureInfo.InvariantCulture
            AppDomain.CurrentDomain.remove_AssemblyResolve(onResolveEvent)
            Assembly.Load (requestedAssembly)
            )
    AppDomain.CurrentDomain.add_AssemblyResolve(onResolveEvent)
    
// sample usage
RedirectAssembly "FSharp.Core" (Version("4.3.1.0")) "b03f5f7f11d50a3a"

Thursday, October 9, 2014

Using a Git layer on top of TFS

Not having git bugs me. Working with any other source control system takes away some of my power.

With a git layer over whatever other source control system is required for the team:

  • I can check-in or revert individual lines of code in a file. 
  • I can create a branch for local modifications that should never be checked into the team source control.
  • I can create mini-commits that don't have to be logged as individual commits in the team source (for teams that prefer one large change-set)
    • I can create my own personal historical record of change save-points and notes/comments
  • I can select which lines of a file should go into a check-in without shelving the entire change.

How to:

Create a junction or hardlink to the folder that contains your code. This junction must not be in the tree that your tfs workspace mapping sees. If it is, Visual Studio will then, forcefully and persistently, use git as your source control and refuse to talk to TFS about your changes.

So I have all my code under c:\development\foo

  1.  I change working directory to c:\projects\bar and mklink /h /j foobar c:\development\foo
  2. git init
  3. Always point visual studio at the legacy folder c:\development\foo.
  4. Always point your git tool(s) at c:\projects\bar
  5. profit!


Monday, June 30, 2014

Why am I so enthusiastic about F#?

I'm suddenly really passionate about F#. This is new as of ~3 weeks ago.
Why?


Thursday, April 17, 2014

T4 ApiController Generator

This T4 assumes your DbContext is called ApplicationDbContext in a file called ApplicationDbContext.cs and does not account for whatever custom namespaces need to be imported for this file for your use. It is set up for DI, but lacks consumption of an IRepository pattern or anything similar. It also expects to be in a directory below the EnvDteHelper.ttinclude.
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Data.Entity.Design" #>
<#@ import namespace="System.Globalization" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Data.Entity.Design.PluralizationServices" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
<# DTE Dte;#>
<#@ include file="../EnvDteHelper.ttinclude"#>
<# 
  var suggestedNs=System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint");
  var projects = RecurseSolutionProjects(Dte);
  var q= from p in projects
      from pi in p.ProjectItems.Cast<ProjectItem>()
      where pi.FileCodeModel!=null
      select new{p,pi,CodeElements=pi.FileCodeModel.CodeElements.Cast<CodeElement>()};
  var context= q.Where(x=>x.pi.Name=="ApplicationDbContext.cs").First();
  var typesToMap = Descendants(context.CodeElements,ce=>ce.Children.Cast<CodeElement>()).OfType<CodeProperty>();
  var project=GetProjectContainingT4File(Dte,false);
  var pluralizationService = PluralizationService.CreateService(new CultureInfo("en-US"));
#>
using System.Linq;
using System.Web.Http.OData;

using Microsoft.AspNet.Identity.EntityFramework;

namespace <#=suggestedNs#>
{
<#  foreach(var tm in typesToMap){
  var singular=pluralizationService.Singularize(tm.Name);
  var plural=pluralizationService.Pluralize(tm.Name);
#>

    public class <#=tm.Name#>Controller : ODataController
    {
        readonly ApplicationIdentityContext _db;

        public <#=tm.Name#>Controller(ApplicationIdentityContext db)
        {
            _db = db;
        }

        /// GET api/<#=singular#>
        public IQueryable<<#=singular#>> Get()
        {
            return _db.<#=plural#>;
        }

        // GET api/<#=singular#>/5
        public <#=singular#> Get(int id)
        {
            return _db.<#=plural#>.FirstOrDefault(x=>x.<#=singular#>Id==id);
        }

    }
<#}#>
}

Monday, April 14, 2014

VS2013 negative look-behind on a sql server database project

Find me all the sql tables that have an unnamed foreign key.
Find all "(?>!(CONSTRAINT|--) .*)FOREIGN KEY", Regular expressions, Subfolders, Find Results 1, Current Project: Project.Sql\Project.Sql.sqlproj, "*.SQL"