Friday, July 27, 2012

What’s new in Rotativa 1.3

What’s new in Rotativa 1.3

The long awaited update of Rotativa has finally arrived. It has important new features, that meet users requests and add new ways of creating Pdf files.
Adding to that I finally managed to add badly needed test automation and I hope I can add more tests in the future.
I should give credit to Scoorf for submitting a nice pull request on GitHub that holds much of the new features. Merging his pull request gave me the opportunity to learn about branches in Git, I should say it’s really easy and very effective and safe way to manage collaborative code.

New features are:

ViewAsPdf action result

This basically works as the normal View action result but you get the Pdf instead of the Html response.

Basic syntax is:
     public ActionResult TestViewAsPdf(string id)  
     {  
       var model = new MyViewModel {DocTitle = id, DocContent = "This is a test"};  
       return new ViewAsPdf(model);  
     }  
It’s even easier then ActionAsPdf, plus you won’t have problems  with authentication and cookies.

UrlAsPdf action result

This action result enables to return any URL as pdf.

Pdf properties

Now are available new properties for the Action results. Those new properties are:
  • Margins
  • Size
    • Paper size (as in A3, A4 etc)
  • PageWidth and PageHeight
  • PageOrientation
  • Post
    • To set Post values
  • and a handful of others…

Friday, February 24, 2012

Rotativa, how to print PDF in Asp.Net MVC

Creating PDF docs in Asp.Net MVC is a fairly common functionality requested by LOB applications. Usually either for reporting or, more often, for having printable friendly documents (invoices, receipts etc).
I used to design a Crystal report document and then evaluate it and convert it to PDF in the web application. The desing part was ok but what really bothered me was the problems that often came up with deploying it. Version mismatches could really turn it into a nightmare.

Lately I came up using the excellent wkhtmltopdf tool to convert html content to PDF. It uses the WebKit engine (used by Chrome and Safari) to render html. The nice thing about it is that I can leverage my knowledge of html and css to obtain a good looking PDF and also it's quite fast.

It's a exe file not a dll library (a dll actually exists but it's not managed code and it has some problems related to usage in a multi-threaded app). It has to be executed from the Asp.net app spawning a process, so it requires some unusual coding from a web development perspective. Another downside is that it requires some work to set it up: copying exe and dll files in the web app solution, make sure they get copied over when building and publishing and some config stuff needed to make it access authenticated actions.

It seemed to me a perfect candidate to become a Nuget package. It was fairly easy to build it, except some quirks when trying to modify package properties.

I've named it Rotativa /rota'tiva/, which is Italian for rotary printing press.

With Rotativa all it takes to print a PDF is:
1. Install using the Nuget package manager (search for Rotativa) or, more conveniently, with the package manager console, typing:

Install-Package Rotativa
2. Writing a controller action to return a view representing the desired PDF output. This means just coding as usual to serve the data as regular MVC action. Such as:
public ActionResult Invoice(int invoiceId)
{
    var invoiceViewModel;
    // code to retrieve data from a database
    return View(invoiceViewModel);
}
Nothing special here, just an action. Perhaps the view used will have a different master page (or layout page) then the other views of the web app (or you’ll define a separete css for the print media, see http://www.w3.org/TR/CSS2/media.html). You can test and “preview” the results just using the browser, since we are working with a regular action, returning a html result.

3. When the html is ok you can just setup a special action returning a custom ActionResult: ActionAsPdf. The code will look like this:
public ActionResult PrintInvoice(int invoiceId)
{
  return new ActionAsPdf(
                 "Invoice", 
                 new { invoiceId= invoiceId }) 
                 { FileName = "Invoice.pdf" };
}
ActionAsPdf requires a string parameter with the name of the action to be converted to PDF. It can accept a parameter with other route data, in this case we are defining the invoiceId parameter. You can specify the name of the file being returned using the FIleName property.

To link to the PDF file in other views you just have to insert a regular link to the PrintInvoice action. Something like

@Html.ActionLink(“PrintInvoice”)
if you’re working with razor views.
And voila, that’s all and it just works, even if the printed action is protected with forms authentication.

Source code is on GitHub https://github.com/webgio/Rotativa.

Sunday, May 16, 2010

NHibernate sessions in ASP.NET MVC



If you've used NHibernate in a ASP.NET MVC project you probably encountered the problem of keeping session open in the view in order to retrieve model data. (even though if session is open then the view actually makes calls to the db under the hood, which maybe is not very MVC. But that's another story)

The idea is to pass controllers an instance of a NHibernate session which is unique for the http request.

First off we need to "hijack" the ASP.NET mechanism that instantiate controllers to pass something in the constructor. In this case it's a ISession, but it could well be a repository class (which in turn gets a ISession passed in its constructor).

So a controller would look like this:

public class MyController : Controller
{
ISession session;

public MyController(ISession session)
{
this.session = session;
}
...

I've used Unity dependency container to manage the instantiation of controllers. ASP.NET MVC lets you do that by overriding the DefaultControllerFactory.

This is as simple as writing this class:

public class UnityControllerFactory : DefaultControllerFactory
{
UnityContainer container;

public UnityControllerFactory(UnityContainer container)
{
this.container = container;
}

protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
IController controller = null;
if (controllerType != null)
{
controller = this.container.Resolve(controllerType) as IController;
}
return controller;
}
}

To make the session instance "singleton" for the http request, all I need to do is write a simple custom lifetime manager for Unity:

public class PerRequestLifetimeManager : LifetimeManager
{
public const string Key = "SingletonPerRequest";

public override object GetValue()
{
return HttpContext.Current.Items[Key];
}

public override void SetValue(object newValue)
{
HttpContext.Current.Items[Key] = newValue;
}

public override void RemoveValue() {} // this does not appear to be used ...
}

Let's get everything together in Global.asax. We need to register NHibernate ISession in Unity configuration. It should inject it using a factory method (OpenSession method of a ISessionFactory instance).

Then we need to tell MVC to use our Controller factory, passing the Unity container of our ASP.NET MVC application.

To close open NHibernate sessions we add some code in the Application_EndRequest.

Here's the code to put in our Global.asax file.

protected void Application_Start()
{
this.container = new UnityContainer();
ISessionFactory sessionFactory = NHConfigurator.CreateSessionFactory();
// NHConfigurator is my helper conf class

container.RegisterType(
new PerRequestLifetimeManager(),
new InjectionFactory(c => {
return sessionFactory.OpenSession();
}));

ControllerBuilder.Current.SetControllerFactory(new UnityControllerFactory
(this.container));

AreaRegistration.RegisterAllAreas();

RegisterRoutes(RouteTable.Routes);
}

protected void Application_EndRequest(object sender, EventArgs e)
{
Object sessionObject = HttpContext.Current.Items[PerRequestLifeTimeManager.Key];
if (sessionObject != null)
{
ISession currentSession = sessionObject as ISession;
if (currentSession != null)
{
currentSession.Close();
}
}
}

Hope this helps!

Saturday, April 10, 2010

Unit testing and the "constructors do nothing" rule



Introduction

Writing unit testing code is really easy.

It's not much more then writing some client code of the library you want to test and add some assert calls to check (test) if the code works as expected. With C# now we have lots of alternatives to do that.

But is it just that?

Not really!

Let's briefly look at some principles behind unit testing.

A unit test, as the name implies, should test just a unit a code. If it doesn't and it ends up depending on some other code then it becomes hard to tell why the test passes or not.
Also, depending on other code makes test hard to make repeatable. Just think about testing a method that depends on some database operation, now you should take care of maintaing database state over repeated calls to your test suite.

Being able to isolate units of code not also makes it more testable but also easier to work with and to refactor.

This post will try to explain some good practices to make your C# code more testable.

Constructors should do nothing

This is an important rule you should enforce if you want your code to be testable. It also makes it easier to enforce SRP (Single Responsibility Principle).
In the class constructor, objects should not be created, because if they are then your class becomes coupled to them and you are not able to inject testing objects during unit testing.

Lets jump to some sample code.


public class StringsCalculator
{
public int Sum(string x, string y)
{
int xi = 0;
int yi = 0;
int result = 0;
if (int.TryParse(x, out xi) && int.TryParse(y, out yi))
{
result = xi + yi;
}
else
{
throw new ArgumentException("Strings passed are not integers.");
}
return result;
}
}


This class StringsCalculator contains a method Sum that takes two strings and tries to sum them if the can be parsed as numbers. It clearly does two things, converting strings to integers and summing them. So if we want to test does separatly we should separate the two responsibilities. Perhaps separating the string conversion in a class StringToNumber:

public class StringToNumber
{
public int Convert(string stringNumber)
{
int result = 0;
if (!int.TryParse(stringNumber, out result))
{
throw new ArgumentException("String is not a number");
}
return result;
}
}

Thus we can refactor our StringsCalculator class:

public class StringsCalculator
{
StringToNumber stringToNumber;

public StringsCalculator()
{
this.stringToNumber = new StringToNumber();
}

public int Sum(string x, string y)
{
return stringToNumber.Convert(x) + stringToNumber.Convert(y);
}
}

This looks better, but what about our test:


[Test]
public void SumStringsNotTestable()
{
StringsCalculator ss = new StringsCalculator();
int result = ss.Sum("2", "3");
Assert.AreEqual(5, result);
}

It tests our class but this way it tests also the StringToNumber class so it's really not "unit" testing.

So let's follow the "Constructors do nothing" rule by doing some refactoring.

First off let's separate the two classes by putting a interface between them. In Visual Studio we just need to right click on the StringToNumber class and choose Refactor->Extract interface... and name it IStringToNumber. Next we need modify the StringsCalculator constructor to accept a parameter of type IStringToNumber.

public class StringsCalculator
{
IStringToNumber stringToNumber;

public StringsCalculator(IStringToNumber stringToNumber)
{
this.stringToNumber = stringToNumber;
}

public int Sum(string x, string y)
{
return stringToNumber.Convert(x) + stringToNumber.Convert(y);
}
}


Mocking objects

Now we can refactor our test to instantiate the StringsCalculator object by passing to its constructor a mock object and not a StringToNumber object. To do this we can use one of the many mocking frameworks for .Net around. I chose the very nice RhinoMocks library, that's easy to use and quite complete. So here's our new test:

[Test]
public void SumStringsTest()
{
MockRepository mocks = new MockRepository();
IStringToNumber stringToNumber = mocks.StrictMock();
StringsCalculator ss = new StringsCalculator(stringToNumber);
Expect.Call(stringToNumber.Convert("2")).Return(2);
Expect.Call(stringToNumber.Convert("3")).Return(3);
mocks.ReplayAll();
int result = ss.Sum("2", "3");
Assert.AreEqual(5, result);
mocks.VerifyAll();
}


As you can see we can instruct the RhinoMocks repository to expect two calls to the IStringToNumber. In this way we are able to verify the inner workings our our class, thus doing what is called white-box testing by calling the VerifyAll() method.

Constructor injection

Having all collaborators classes passed in as constructor parameters enables us to decouple nicely and test in isolation. But what about the client code using this classes, do they have to do lots of code to instantiate all those collaborators? Well there's a nice solution to this, dependency injection containers. In my case I'll use Unity, by using it to instantiate objects, client code doesn't have to prepare alkl those collaborators, Unity will take care of it, even in cascade if those collaborators themselves have other collaborators as constructor parameters. We can implement a integration test to verify our classes together:


[Test]
public void SumStringsUnityTest()
{
UnityContainer container = new UnityContainer();
UnityConfigurationSection section =
(UnityConfigurationSection)ConfigurationManager.GetSection("unity");

section.Containers.Default.Configure(container);

IStringToNumber stringToNumber = container.Resolve();
StringsCalculator ss = new StringsCalculator(stringToNumber);
int result = ss.Sum("2", "3");
Assert.AreEqual(5, result);
}


Unity configuration code could be conveniently located in a helper class.

So, to summarize, we can say: Constructors do nothing, all collaborators are passed in as parameters so we can mock them in unit tests and let a dependency injection mechanism compose them in client code.

Monday, November 2, 2009

Separating layers and components with constructor injection and Unity

Ok, so how did I come to using Unity and constructor injection?

Anyway ... what is Unity? And what is constructor injection?

Unity is a "dependency injection container", (see http://www.codeplex.com/unity/ for details) in other words it is a library that implements mechanisms to easily apply the dependency injection pattern.

Dependency injection is a design pattern that aims at separating concerns, like separating from the application specific domain all the other concerns that are not strictly "about" the topic of the application (usually are more low level).

For example in a mp3 player app, connecting to the db to save personal info about the songs is not something that affects software functionality. If the db app changes or we decide that the info is going to be stored to a remote server through a web service, the functionality will still be there and the app will continue to work. It would be different if we wanted our app to be able to play iTunes audio books. That would be a change that affects the domain of the app.

So it's a valuable thing if we can separate those facet of the application from the "real" functionalities. This separation enables us to think more clearly and in a clean way about our application and not mix different concerns. Also we can unit test better and apply test driven development.

Let's see a sample app. What about the dear old "Hello world" functionality?! This app is about... saying hello... to you.. :-)
By the way this time we are going to log who are we saying hello to. How and where we are going to store this info is not really relevant to the app. We just want to able to call a method like Log(message), in the HelloWorld method of our class (MainClass, forgive the ugly name..):

public class MainClass
{
ILogger logger;

public MainClass(ILogger logger)
{
this.logger = logger;
}

public string HelloWorld(string name)
{
string helloMessage = "Hello " + name + "!";
this.logger.Log("Said hello to " + name + "!");
return helloMessage;
}
}


HelloWorld calls the Log method of an object implementing the ILogger interface. This way our application is not dependent upon a specific logger implementation, hence the inversion of dependency, the main application doesn't depend on the loggr, it's the logger that depends upon the ILogger interface defined by the app.

Here's a quick look to how the visual studio solution looks like:



It's very simple, we have a UnitySampleUse project, a MyLogger project and the test project UnitySampleUse.Test.

UnitySampleUse contains the ILogger interface. MyLogger contains MyLoggerClass that implements ILogger, so MyLogger contains a reference to UnitySampleUse. If we where going to instantiate a MyLoggerClass object inside UnitySampleUse we would have to reference and we don't want to make ou app depent upon MyLogger (plus we would have circular reference).

So who's going to glue things together and pass a ILogger object to UnitySampleUse?
Guess what... Unity!

The client of our code, in this case the test project (it could be a UI project or another library), uses Unity to instantiate ILogger, automagically composing our MainClass, passing in the constructor the concrete object implementing ILogger.
The mapping of the interface to the class implementing it can be stored in the xml config file (app.config), that, in our case, will look like this:


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration, Version=1.2.0.0,Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</configSections>

<unity>
<typeAliases>
<!-- Lifetime manager types should be inserted if you need lifetime managers -->
<typeAlias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager,Microsoft.Practices.Unity"/>
<typeAlias alias="external" type="Microsoft.Practices.Unity.ExternallyControlledLifetimeManager,Microsoft.Practices.Unity"/>
<!-- User defined type aliases -->
<typeAlias alias="ILogger" type="UnitySampleUse.ILogger,UnitySampleUse"/>
</typeAliases>
<containers>
<container>
<types>
<type type="ILogger" mapTo="MyLogger.MyLoggerClass, MyLogger">
</type>
</types>
</container>
</containers>
</unity>

</configuration>


The test will need to instantiate the MainClass using Unity:


UnityContainer container = new UnityContainer();
UnityConfigurationSection section =
(UnityConfigurationSection)ConfigurationManager.GetSection("unity");

section.Containers.Default.Configure(container);

MainClass mainClass= container.Resolve<MainClass>();


Unity will take care of calling the constructor MainClass(ILogger logger), injecting a MyLoggerClass object. Thus the type of dependency injection applied is constructor injection.

UnitySampleUse project can be compiled without having the source code of the logger and the client code (the unit test, or the ui etc) can switch logger implementation without change in the code by just referencing the new logger and updating the config file.

This way we can separate layers, for example our data access layer, very easily and at the same time keep all our application logic in one project.

Separating layers and building by components has never been easier.

This post is published on

Wednesday, March 25, 2009

Prototyping with Pencil and Powerpoint

I found myself in the need to produce functional specifications for a module of a web application. The requirement was to build some fake screen shots to give the client a representation of how the software will look like.

In past projects I tried paper prototyping, with mixed results and visio diagrams.

This time I looked around to see if some other tool was available and I found Pencil. It's a Firefox 3 plugin that uses the Mozilla Gecko engine. I should say I was really delighted by the simplicity of the tool and the power it gives in building prototypes/wireframes. I was able to be productive in minutes and the results were really good.

You can draw UI shapes, align and resize them with ease and organize them in pages. Then you can batch export those pages as PNG images in a folder. All with just a few clicks.

After doing this I built some detailed spec about how those pages interacted one with each other in a Word document. I was putting every single atomic user interaction down, in numbered points, using the point numbers as anchor reference between points.
As I was doing this I thought it would be more effective if I could just make this really interactive. So it came to my mind I could simply use PowerPoint to achieve this.

It turned out as simple as this:
- I used a print screen of the web app that is going to host the new module, cleaned the body with Gimp and keep just the background color and header and footer.
- Put this image as the background for the PowerPoint slides.
- Insert each PNG image obtained from Pencil in single slides.
- Draw a transparent rectangle (no fill, you can make them transparent by default) on the areas where you want to enable the user to click, for example on a button of a Pencil generated png image.
- On this rectangle, right mouse click and choose "Action settings".
- On the "Action settings" pane you can choose the "Hyperlink to:" option and define the slide to send to upon clicking the rectangle. On the Mouse over tab check the Highligh mouse over option.
- You can also disable clicks on the slide to take viewer on the next slide by selecting 'Slideshow' menu, 'Slide transition' and then uncheck the option that says 'Advance on mouse click'. You can also add a smooth transition and apply on all slides,

This method is really basic and simple, but the results can be quite realistic.