Tuesday, November 17, 2015

T4 Generates my business objects for me revisited with F#

A long time ago I wrote an article about the code I was using in T4 Generates my business objects for me. Today I've rewritten the idea with F# as the target. F# projects don't support T4 so I used a C# project to generate F# code into an F# project. The parts of the generated code
  • A readonly interface
  • A read-write interface
  • An F# record type
  • An F# module for mapping code methods (with Statically Resolved Type Parameter support aka Duck Typing or Structural Typing)
  • An F# class with INotifyPropertyChanged for WPF consumption
Benefits
  • Implementing interfaces is not allowed to be implicit so I've automated that headache.
  • INotifyPropertyChanged desires magic strings (exception in the lovely new C# 6 nameof operator) so automation makes sure the strings are correct as of the last time T4 was run
  • If I pass around the read-only interface, I don't care how far down the rabbit hole of that object goes, it's immutable, no one is modifying it
  • If I pass around the read-write interface, all the consuming methods can be ignorant of concrete types
  • I'm generating Metadata from the db into the comments on the properties (tons of untapped potential here)

View the generated code and generator t4

- Generator and generated code from new T4 to F#

Things I want to add:

  • foreign key information to the auto-generated property comments
  • consider having the Read-Write interface implement the Read-Only interface
  • figure out how to update Microsoft's Type Providers to have them implement my interfaces.
  • add an option for generating Sql Sprocs that work against these types

Friday, February 20, 2015

Get Code Coverage from VS without writing automated tests

Create a batch file somewhere in your system as such:
"C:\Program Files (x86)\Microsoft Visual Studio 12.0\Team Tools\Performance Tools\vsinstr.exe" -coverage "%1"
"C:\Program Files (x86)\Microsoft Visual Studio 12.0\Team Tools\Performance Tools\VSPerfCmd.exe" /start:coverage /output:run.coverage
start "%1" /wait "%1"
"C:\Program Files (x86)\Microsoft Visual Studio 12.0\Team Tools\Performance Tools\VSPerfCmd.exe" /shutdown
notepad "run.coverage"
echo "%1"
echo "%2"

pause
Create an External Tool Entry in your Visual Studio Tools Menu.
  • Title: RunCovered

  • Command: c:\projects\config\batches\RunCovered.bat
  • Arguments
    $(TargetName)$(TargetExt) $(BinDir)
  • Initial Directory:
    $(BinDir)

replace the command with wherever your new shiny batch file lives.

Then in trigger that External Tool on the Tools Menu.

It will launch your app, and you can then click around or do whatever is needed to activate parts of your application, and the results will be saved into the same directory as your executable file as run.coverage. Open this file in your already open Visual Studio instance, or drag and drop the file from explorer into VS.

There you have your App's coverage metrics for that run.