Alexander Beletsky's Development Blog: 2012-02

NancyFX Migration from 0.9 to 0.10

Recently, I've upgraded IdeaStrike project from NancyFX 0.9 to the latest 0.10 version. There were some very unclear moments, fortunately due to great support of @thecodejunkie and @grumpydev they are solved. As a result, the issue 520 was born, that contains a lot of text inside. I'll try to sum up all important things into one blog post.

Running update

With NuGet updating dependencies is very easy, just run in package manager console:

    PM> update-package

It will give you the information about the upgrade process, it should be very smooth, so the end you will see:

Successfully uninstalled 'Nancy.Hosting.Aspnet 0.9.0'.
Updating 'Nancy.Testing' from version '0.9.0' to '0.10.0' in project 'IdeaStrike.Tests'.
Successfully removed 'Nancy.Testing 0.9.0' from IdeaStrike.Tests.
Successfully removed 'Nancy 0.9.0' from IdeaStrike.Tests.
Successfully added 'Nancy 0.10.0' to IdeaStrike.Tests.
Successfully installed 'Nancy.Testing 0.10.0'.
Successfully added 'Nancy.Testing 0.10.0' to IdeaStrike.Tests.
Successfully uninstalled 'Nancy.Testing 0.9.0'.
The directory is not empty.

Successfully uninstalled 'Nancy 0.9.0'.

Fixing compilation errors

As you try to build the project after upgrade, it would fail because of compilation issues.

If you using Nancy bootstrapper and overriding RequestStratup method, you will see that method had changed the signature:

    // in 0.9
    protected override void RequestStartup(ILifetimeScope container, IPipelines pipelines);

    // in 0.10
    protected override void RequestStartup(ILifetimeScope container, IPipelines pipelines, NancyContext context)

So, it now receives additional parameter - NancyContext.

Next compilation errors is inside the Request.Headers.AcceptLanguage.

    // in 0.9
    public IEnumerable<string> AcceptLanguage { get; }
    
    // in 0.10
    public IEnumerable<Tuple<string, decimal>> AcceptLanguage { get; }

Both of these compilation errors are really easy to fix. See changes to src/Ideastrike.Nancy/IdeastrikeBootstrapper.cs in this commit.

After that, the compilation will be fine. I had to restart my Visual Studio after, since NCrunch was failing to compile the application.

Fixing failing unit tests

After VS is restarted and NCrunch able to pick up changes, I got 3 unit tests failures. All of them with the similar exception, like:

    System.Exception: System.Exception : ConfigurableBootstrapper Exception
    ---- Nancy.RequestExecutionException : Oh noes!
    -------- Nancy.ViewEngines.ViewNotFoundException : Unable to locate view '404'. Currently available view engine extensions: sshtml,html,htm,cshtml,vbhtml
    at Nancy.Testing.PassThroughErrorHandler.Handle(HttpStatusCode statusCode, NancyContext context)
    at Nancy.NancyEngine.CheckErrorHandler(NancyContext context)
    at Nancy.NancyEngine.HandleRequest(Request request)
    at Nancy.Testing.Browser.HandleRequest(String method, String path, Action1 browserContext)
    at Nancy.Testing.Browser.Get(String path, Action1 browserContext)
    at IdeaStrike.Tests.IdeaStrikeSpecBase1.Get(String path, Action1 browserContext) in D:\Development\Projects\Ideastrike\tests\IdeaStrike.Tests\IdeaStrikeSpecBase.cs:line 85

That was a mystery. All of those tests were fine, but suddenly stopped to work in 0.10.

The reason was very interesting. Those tests for views were actually never run. It's been unseen for 2 reasons: Nancy 0.9 failed silently about missing view, IdeaStrike unit tests never tested view content.

To make those run, the IdeaStrikeSpecBase have to be setup with IRootPathProvider. IRootPathProvider, provides the path root for modules, so views could be located, based on default conventions. I finished up with this implementation:

public class CustomRootPathProvider : IRootPathProvider
{
    public string GetRootPath()
    {
        return Path.GetDirectoryName(typeof(IdeastrikeBootstrapper).Assembly.Location);
    }
}

This root provider is used with spec base class configuration:

public IdeaStrikeSpecBase()
{
 Bootstrapper = new ConfigurableBootstrapper(with =>
 {
  with.Module<TModule>();
  with.Dependencies(_Users.Object, _Ideas.Object, _Features.Object, _Activity.Object, _Settings.Object, _Images.Object);
  with.DisableAutoRegistration();
  with.NancyEngine<NancyEngine>();
  with.RootPathProvider<CustomRootPathProvider>(); // <- Here
 });
}

So, we are saying that root is the same folder as IdeaStike assembly. The problem is that location depends on actual Test runner. In case of NCrunch, it would be some deeply hidden folder in user\AppData\Local\Temp, in case of ReSharper runner it would be temp ASP.NET folder. That's a little annoying.

Tests were still red, since neither NCrush nor ReSharper is copying actual views into target folder. Fortunately, I found this great article, explaining NancyFX view testing gotchas. I ended up with just saying “Copy if newer” for views under test, so they are in the same folder as target binary. That's not cool, but currently I see no other option.

After those changes, tests became green, so I run the application. No surprise, it failed to start.

Fixing runtime errors

At the first run, I got nothing more as bunch of Razor compilation errors:

    Error Compiling Template: (51, 18) The name 'Url' does not exist in the current context)
    Error Compiling Template: (61, 33) The name 'Model' does not exist in the current context)
    Error Compiling Template: (68, 26) The name 'Model' does not exist in the current context)
    Error Compiling Template: (78, 7) The name 'Model' does not exist in the current context)
    Error Compiling Template: (83, 31) The name 'Model' does not exist in the current context)
    Error Compiling Template: (88, 14) The name 'Model' does not exist in the current context)
    Error Compiling Template: (103, 14) The name 'Model' does not exist in the current context)
    Error Compiling Template: (118, 14) The name 'Model' does not exist in the current context)
    Error Compiling Template: (132, 25) The name 'Model' does not exist in the current context)
    Error Compiling Template: (234, 19) The name 'Url' does not exist in the current context)

The proposed solution that NancyRazorViewBase should always be specified with generic parameter.

    // in 0.9
    @inherits Nancy.ViewEngines.Razor.NancyRazorViewBase

    // in 0.10 
    @inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<dynamic>
     

OK, that helped a little, but right after that error saying System.Web.IHtmlString type is not references:

    Error Compiling Template: (83, 1) The type 'System.Web.IHtmlString' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.)

NancyFX is being developed to be independent of System.Web. It was not totally true for 0.9 with Razor support, but it has been fixed in 0.10. That of cause, brings some surprises. So, if your application is using Razor view engine, you need to do following:

  • update all usages of IHtmlString to use Nancy.ViewEngines.Razor.IHtmlString
  • update all usages of McvHtmlString to use Nancy.ViewEngines.Razor.NonEncodedHtmlString
  • let the razor engine know about system.web (in case if HttpContext is used in layout)
<razor disableAutoIncludeModelNamespace="false">
    <assemblies>
        <add assembly="System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </assemblies>
    <namespaces>
        <add namespace="System.Web" />
    </namespaces>
</razor>

Changing the Partial views

After these steps were done, application finally started. But some page gave runtime exception:

Server Error in '/' Application.

Sequence contains no elements

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: Sequence contains no elements

My dump analisys showed, that it works fine as soon as I removing partial views:

    @Html.Partial("Shared/Templates/upload.html")
    @Html.Partial("Shared/Templates/download.html")

The reason turned out to be really simple. All templates in IdeaStrike project were using .html extension. For 0.10 it required to be .cshtml. So, renaming from .html to .cshtml made the IdeaStrike up and running again.

Speeches Hat-Trick For This Weekend

It's been great weekend. It started Friday evening with Kiev ALT.NET meet-up which been in idle for a while and everybody missed that. There was a three planned speeches: by @_TKL on Continuous Testing (NCrunch, MightyMoose), NancyFX framework by me and FubuMVC by @skalinets.

I would not say I got great experience on Nancy so far. But I'm pretty exited on frameworks features and super-dupper-happy-path. Almost all knowledge I gathered thought hacking of IdeaStrike, listening to Herding Code episode and reading documentation. But anyway, I believe it turns out to be good introductory presentation.


Right after my Nancy talk I moved to central train station, since I was about to visit Ciklum .NET Saturday in Dnepropetrovsk. .NET Saturday's is just great initiative by Ciklum company. I's free event, everybody welcome, content is usually nice. I highly recommend to follow their events.

I had two talks there. First one about "Continuous Delivery" that I did on Agile Base Camp 2012. Besides of the talk I also did a quick and improvised demo of Candidate application. Even if I had some technical issues the demo went fine.

Another one is for logging and tracking unhandled exceptions in ASP.NET / ASP.NET MVC application based on ELMAH. I showed some basics features as well easiness of ELMAH integration to ASP.NET MVC with ELMAH.MVC NuGet package. It was very light and funny talk, especially discussing Troy Hunt's ELMAH attack.

I also really much enjoyed Anton Samarskyy talk on jQuery deffered objects and Vitaly Koval did great hacking session of WinRT.

I some of you been listening to me on those speeches and still have any questions or concerns, feel free to contact me by comments or through twitter. See you next time!

Foundation Framework for Web Sites Production

The Foundation framework is something I discovered by accident browsing through some github project pages. I really liked how one of the sites looked on my iPad. I was also pleased to see how HTML crafted, using clear names for classes and HTML5 semantics. As it turned out, that site was using Foundation Framework by Zurb - design agency in San Francisco Bay area.

I checked out the documentation and Foundation appears to be pretty solid framework for web applications prototyping and production. It contained everything needed: Grid Layouts, Buttons, Forms and UI. And of cause, all of that is open source hosted on github.

During my preparation of project page for candidate I decided to try it out. I was really happy of the experience.

Object Oriented CSS

Have you heard of OOCSS? Initially I thought that idea is almost non-sense, but I'm changing my opinion now. I think Foundation fully conforms to OOCSS style of development - each behavior or UI you want to apply to HTML element is done by applying of corresponding class.

Great thing is that you see how it's done in framework and do same in your code as well. I remember the times I started to do HTML/CSS and I higly used #id's, applying the styles by element Id. The code was awful and styles applied on #id's were not re-usable at all, so if I needed the same margin/padding or text color I have to copy the section of CSS and move it into next #some-id { ... }.

With OOCSS you rely on classed. Here some example,

<div class="container darker-grey light-border small-padding">
 <div class="row smaller-fonts">
  <div class="four columns"></div>
  <div class="four columns"></div>
  <div class="four columns"></div>
 </div>
</div>

Look, no #id's - classes only. With classes I control: position, colors, borders and margin/paddings. This is really cool, since all of that classes are easy reusable in any part of page.

Grid system

Grid systems is really cool concept and allows to develop much faster. Instead of writing own CSS that would position elements, you stick to Grid System. The classical grid system is probably 960.gs. It's really great and blown my mind as I first time tried it, but currently it little outdated: 960px wide is small for modern monitor and it is not adaptive. Modern web design demands site looks great both on PC and Mobile devices, adapt it's content based on screen size.

Here is a quote of Foundation documentation.

The grid is built around three key elements: containers, rows, and columns. Containers create base padding for the page; rows create a max-width and contain the columns; and columns create the final structure. Everything on your page that you don't give a specific structural style to should be within a container, row and column.

You develop the layout based on planned number of row and columns that compose the row. Again, code is done in OOCSS way with clean classes names, so you write almost "plain" English in HTML.

<div class="container">
  <div class="row">
    <div class="eight columns">
      Eight columns
    </div>
    <div class="four columns">
      Four columns     
    </div>
  </div>
</div>

After design is ready, you just open it on iPad and.. Surprise, it looks great there! Text and columns are adaptively fits the screen size.

Buttons and Forms

Buttons and Forms are same important as Colors and Fonts in your web site. You can't get good site appearance if buttons and forms are ugly. I usually spend a lot of time on "beautifying" those, but still unhappy with results at the end of the day. With Foundation it's easier. You got nice styles from the box.

Forms:


foundation forms

Buttons:


foundation buttons

Recently, I've submitted green button style, so I hope it will be available soon.

UI stuff

Besides those primary things you got nice bonus. That's different UI elements commonly used through different web sites. It includes: Alerts, Labels, Warnings, Tooltips, Tabs etc.

For candidate site I successfully used Pagination UI element.


foundation ui

Conclusions

Currently Bootstrap from Twitter is obvious leader in that niche. But I really think Foundation will get it's place. Easy to use, great documentation repository is very active, so we might expected further great features in Foundation.

And by the way.. To payback Foundation, I got idea for small product: inspired by Bootswatch I'm about to create bunch of "ready-to-use" Foundation themes, that you just download and start to use immediately. It will work great for people who are about to create one page product presentation or simple blog. Hope it will go fine and I release it soon.

Candidate v.0.0.1rc - Released

You may notice that I stick to original project name at the end of the day, even thought I wish to change it. That happened for 2 reasons: first, I really get used to candidate and it's good enough project name.. second, I spent some hours of brainstorming but found out nothing better that existing.

This weekend I also finally concluded project web site and hosted it as github pages - http://alexanderbeletsky.github.com/candidate/. So, ladies and gentlemen - please welcome, Candidate v0.0.1rc is ready to be shown to the world.


candidate web site

What's the goal?

Thinking about nearest "competitors" in this area I would re-call Octopus by Paul Stovell. Octopus uses Build Server / Tentacle (Agent) architecture principal, while Candidate is simple Agent architecture - you host it the same machine as your environment is (stage, production) providing both integration and deployment functionality.

My goal is stabilize and improve Candidate through this year, so it fulfill different requirements including performance and scalability. I switched all my projects to use Candidate now, so it would give me some initial feedback.

Nevertheless, Candidate may turn out to be a powerful deployment application I'm still thinking about "Deployement as Service" product like AppHabor and Heroku or Travis-CI as it was original idea.

What's next?

Following the principle "Build product X to learn technology Y" I'm going to proceed with Candidate to shape following areas that I wish to improve now:

  • Processes, threads, synchronization in .NET
  • Async, Tasks library
  • Robust applications architecture
  • Different deployment scenarios for .NET platform

Technological stack is still ASP.NET MVC3 / C#/ jQuery. Currently application requires to be hosted on IIS, with application pool Process Identity equals to Admin. This does not suite all users. So, I'm looking forward to create self-hosted version using NancyFX.

I also try to target UI / UX issues as much as possible.

Wanna join?

I would be happy to. I'll much encourage you to download application, install it, perform simple testing (scenario could be found here). Let me know what you think, that would be great initial contribution. As for any github hosted project, you are absolutely free to fork, hack it out and send pull request.

Thanks for you help.

Few Things I Learned From IdeaStrike Project

First of all, I have to say Code52 team is doing just amazing job. It's very original initiative, cool ideas and highly productive team. Each time they announce next thing to build I have "product envy" inside me. Check out their blog or github account to be updated.

My attention this time was attracted by IdeaStrike project. It suppose to be analog of uservoice for OS community. As always, first release was very fast and I was very curious what's inside. So, I cloned the repo and did small hacker session. I've spend some hours with it and would like to share some initial thoughts.

Build script is small and clean

Project contains build.cmd as I run it downloaded all dependencies by NuGet, build web site and run all unit test. Basically, all you need for any small project. Then I looked inside, it turns out like:

@echo Off
set config=%1
if "%config%" == "" (
   set config=Debug
)

%WINDIR%\Microsoft.NET\Framework\v4.0.30319\msbuild build.proj /p:Configuration="%config%" /t:AppHarbor /m /v:M /fl /flp:LogFile=msbuild.log;Verbosity=Normal /nr:false

At the time I first looked in it also contained small script for running tests, but later they moved that to .csproj as separate target.

Use NuGet without committing to SCM

I was amazed, how fast git repository was cloned and as I checked out packages folder with all dependencies were just empty. How can it be? As I said in #1 as I run build script they just automatically downloaded for me. As it turns out, this is feature of NuGet that allows to work with dependencies, without real necessity of committing those to SCM. This is very cool, especially if you have big project, so cloning and branching appears to be prolonged operations. The implementation details is on nuget site.

Deploy application DB at first launch

IdeaStrike is based on EF 4.3 Beta 1 currently, using Code First approach. EF Code First also includes very cool feature called Migrations. This is something I really lack first time I tried EF Code First approach. You define the Context, DbConfiguration class and migration scripts (that's also, just a C# code), so at application bootstrap you call the code like:

private static void DoMigrations()
{
    var settings = new IdeastrikeDbConfiguration();
    var migrator = new DbMigrator(settings);
    migrator.Update();
}

The database and schema will be automatically deployed on first application run.

Social login with Janrain

As I clicked to Sign In button, I've been showed nice dialog to select my existing social account to login.


social login

JanRain is very cool solution. I remember I tried to investigate something like that for ASP.NET MVC and was really disappointed, because I actually found nothing. For all of existing solutions you have to read OAuth spec and write own code. JanRain does all dirty job for you. As user authorized you receive POST on given URL, with a special token. By given token you request the details about the user, like name and email and so on (see LoginModule.cs). It has fair plans for hobby projects and bloggers.

Bootstrap your CSS and HTML

Bootstrap neither new nor very unique, at least I know several good CSS/HTML frameworks. But IdeaStrike proved one more time - it has no sense to invent the wheel. Take advantage of results produced by people who are smarter than you, that's the rule of pragmatic programmer.

Dear future me, please never ever again start your application with HTML/CSS from scratch, you can build nothing more as shit. Bootstrap is great for different kind of projects and prototypes. Taking into account number of watches and forks on github I expect even more improvements and features in future.

Nancy as web framework

I've heard about Nancy many times, but it's only now I got a change to touch it. For me it's just alternative reality comparing to ASP.NET MVC. It is not hard to understand what the code does. It is not hard to apply changes there. You are very explicit of what you are doing, by specifying HTTP verb, route and action as lambda. First class citizen in Nancy is Module, all module's logic is placed inside the constructor:

public FeatureModule(IIdeaRepository ideas, IFeatureRepository features, IUserRepository users)
    : base("/idea")
{
    _ideas = ideas;
    _features = features;

    this.RequiresAuthentication();

    Post["/{idea}/feature"] = _ =>
    {
        int id = _.Idea;
        var feature = new Feature
                        {
                            Time = DateTime.UtcNow,
                            Text = Request.Form.feature,
                            User = Context.GetCurrentUser(users)
                        };
        _features.Add(id, feature);

        return Response.AsRedirect(string.Format("/idea/{0}#{1}", id, feature.Id));
    };
}

Nancy application could be hosted as ASP.NET / WCF / Self hosted runtimes, it supports different View Engines (including Razor). In short, it is great ALT.NET tool and will find a lot of applications in different projects. More details on Nancy github account.

Conclusions

IdeaStrike is great example of open source project you can learn something from. I would not say I got only positive impressions, moreover there are some obstacles that block me from my normal development flow. I'll try to share my observations on next blog posts.

NancyFX, probably be my next web framework to learn, since I need to move out of ASP.NET MVC which is comfortable zone for me now. I'll be keep looking for IdeaStrike and hope to do some pull requests as well.

New Tools in My TDD Arsenal

Recently my TDD arsenal has been enhanced with 3 new cool tools, which I'm about to share with you. More precisely it one tool and two frameworks. Let's go for it.

NCrunch

NCrunch is just amazing extension for Visual Studio created by @remcomulder. It automatically detects all your tests and re-running those as soon as source code changes happen. Forget about manual test re-running, it's just waste of time. You even do not need to press Ctrl + S, just continue coding as you usually do.

Initially I had big doubts about such kind of tools, but NCrunch changed my mind. It supports major unit test frameworks NUnit, XUnit, MSpec etc. Besides of that it allows to collect code coverage metrics (and show it just in VS editor), run tests under debugger, supports multi-core systems etc.

In short, NCruch is something that makes your TDD very smooth, allowing to focusing on important things and forgot about some routine.


ncrunch

NSubstitute

I stick to Moq for quite awhile and saw no reason to switch it.. Till I saw NSubsitute. I hardly could imagine someone who staring "yet another mocking framework project", it looks like absolute non-sense.. But those guys proves me wrong.

Well, what's so new there? First of all it have very clean API. No more new Mock() or MockGenerator.GenerateMock(), creation of test doubles are nothing more as Substitute.For<IEntityToMock>(). Mocking properties, multiple return values, events etc. in very easy fashion. Check out their getting started materials for further info.

Best feature as for me, that by using extension methods they got rid of lambdas for setting up mocks. It makes test code more readable and clean. See this small gist there I placed just some Moq and NSubstitue tests together.

I would not say that Rhino or Moq is now much worse that NSubstitute.. No, I would just say NSubstitute is a little better. Even same functionality, with less amount of code is already big argument for me.

[Test]
public void should_send_an_email_if_users_signs_up_nsub()
{ 
 // arrange
 var emailService = Substitute.For<IEMailService>();
 var controller = new LoginController(emailService);

 // act
 controller.SignUp(new SignUpModel { Email = "a@a.com", Password = "xxx" });

 // assert
 emailService.Received().SendEmail(Arg.Any<EmailMessage>(), "current");
}

FluentAssertions

Again, for years I followed classic NUnit's Assert.That() method. I also played a bit with SharpTestsEx, but FluentAssertions by @ddoomen is going to change that.

FluentAssertions are based on extension methods and allows you to get rid of Assert.That call and just wrote your assertion directly to object. Here some example:

{
    // NUnit.Assert style..
    Assert.That(result, Is.EqualTo(3));

    // FluentAssert style..
    result.Should().Be(3);
}

This is very simple example. The power of FluentAssertions arise then you need to have either multiple assertions or assertions on complex objects. Multiple assertion could be combined by And, like:

{
    "somestring".Should().Contain("some").And.HaveLength(10);
}

It also provides great support for working with Collections, DateTimes, Guids, Exceptions, XML etc. Project is hosted on codeplex, here is documentation. Easy start, easy go.

Conclusions

Now, I'm sharpening my axe on those tools and have very nice impressions so far. Special thanks goes to @skalinets who introduced me with those tools.