Friday, March 18, 2016

Upgrading to Angular Component Router

I have a use case where I can benefit from upgrading to the 'new' component router in AngularJs 1.5. Below are my very quickly jotted down notes.

Wow! My app is so much faster after the upgrade!

I needed to have nested routes.  Basically a list of (different) things on the left nav.  When clicked, the correct controller and view need to show in the center of the page.
For HTML I'm using Bootstrap so the initial html is pretty easy, just a row with two columns (col-md-3, col-md-9)

My primary resource is the AngularJs docs .

I decided to NOT upgrade to html 5 routing mode.

I'm ripping out my old routing, although I'm not quite confident I need to do this.


  • I started by making my top nav an angular component. This is the top level of the component tree.
    • I pulled the top nav (html) out of the index.html and making a topNav.html template.
    • Then I basically copied the component example in the AngularJs docs. Kept this in my index.js file or the ng-app's first controller file.
    • changed the ng-href's into ng-links in the topNav.html
    • wired up all the immediate routes mentioned in the topNav
      • which leads to making components in all the other modules
      • I'm working them one at a time.
      • Read the docs carefully, if your routes have sub routes you have to include /... in your parent route definition.
  • Converting an existing controller over to a component.
    • follow the example in the docs
    • I'm using the vm.foo syntax in the controller, I kept that but also added an extra line, might be unnecessary though
      • $ctrl = this
    • changed all my controllerAs syntax for ctrl to $ctrl in my HTML (had to do this.)
    • Another big gotcha was route parameters
      • required and optional, you will need to visit the AngularJs 2.0 docs for this. 
      • The biggest unexpected part is the syntax changes based on the route definition
        • if in the ng-link you have extra parameters (that are not in the route definition) they will show up as optional parameters in either ?foo=bar or ;foo=bar.
        • Usually the required parameters show up in the route like #/user/eric
    • $location.search('id',vm..selectedThing.id) is now
      •  this.$router.navigate(['MyThingRoute', { id: vm.selectedThing.id }]);
  • Navigating to a nested component from a home or other page.
    • <a ng-link="['UsersList','UserDetail',{userId:$ctrl.id}]">{{$ctrl.name}}</a>
      • This invokes the UsersList component, then the UserDetail component, and then the user id is added to the route auto-magically.


Thursday, May 7, 2015

Unit Testing Invalid Model State in a WebApi 2 Controller

I choose to do this operation in two different tests.
1. Test the controller behavior when the model state is invalid.  Do this by forcing the model state to invalid by adding an entry before the method is called.
2. Test the model state attributes independently from the controller.

For the controller Test

[TestMethod]
public void WhenModelStateIsInvalidDoNotSave()
{
  var mockRepo = new Mock<IProductRepo>();
  mockRepo.Setup(x=>x.Save(It.IsAny<Product>());

  var ctrl = new ProductController(mockRepo.Object);
  ctrl.ModelState.AddModelError("SomeRandomProperty","SomeRandomProperty was not valid");

  var actual = ctrl.Post(new Product());

  mockRepo.Verify(x=>x.Save(It.IsAny<int>()),Times.Never);
  Assert.IsInstanceOfType(actual, typeof(InvalidModelStateResult));
}


public IHttpActionResult Post([FromBody] Product model)
{
  if(!ModelState.IsValid)
    {
       return BadRequest(ModelState);
     }
   
   ProductRepo.Save(model);
    return Ok();
}

Wednesday, March 25, 2015

AngularJs Style Guide

Simple but radical concept, have the tests right next to the code.  In server side code, I'm so used to separating tests into a separate project. On some projects I work on the tests need to be stripped out for the production build, even thought unit tests should be harmless, but not my call.  This can be done with the build scripts, if needed.

There are many other gems in this document, check it out.
https://github.com/johnpapa/angular-styleguide#organizing-tests

Tuesday, March 10, 2015

Creating a self signed cert using makecert

http://www.jayway.com/2014/09/03/creating-self-signed-certificates-with-makecert-exe-for-development/

"C:\Program Files (x86)\Windows Kits\8.0\bin\x86\makecert.exe" ^
-n "CN=CARoot" ^
-r ^
-pe ^
-a sha512 ^
-len 4096 ^
-cy authority ^
-sv CARoot.pvk ^
CARoot.cer

"C:\Program Files (x86)\Windows Kits\8.0\bin\x86\pvk2pfx.exe" ^
-pvk CARoot.pvk ^
-spc CARoot.cer ^
-pfx CARoot.pfx ^
-po Test123

Wednesday, March 4, 2015

WebMatrix.data

My favorite quick data access tool is WebMatrix.Data.  It went through a name change, I'm posting it here so I don't forget it. 

http://www.nuget.org/packages/Microsoft.AspNet.WebPages.Data/

Thursday, September 25, 2014

Forms Authentication Sliding Expiration and the SPA

Working on a SPA with Forms Sliding expiration.  The trick is how to let the SPA know the user has timed out.  If you make a call to the server, with sliding expiration enabled... the account will never expire.

I don't have a working solution yet, but thought I would post the observation.

Tuesday, September 23, 2014

TF-Git

Testing out TF-Git.  The tools for peer review are more robust in the git ecosystem than in TFS.  The workflow seems reasonable so far.  
 
https://gittf.codeplex.com/