Resharper – custom snippet

I think all Resharper users are familiar with snippets. Resharper’s snippet basically generates a piece of code, which can be easily customised on the fly. All you have to do is to press TAB, and you can change names of auto-generated variables etc. Today, I decided to write my own snippet, which would make writing unit tests easier. In general, all my unit test methods looks like this

So let’s write a snippet which will generate a method with this signature. In order to do this, let’s choose a Templates Explorer… from the Resharper’s menu.
TemplateExplorerFirstWindow
In the opened window, click on the plus sign and a new tab should appear.
TemplateExplorerFullView
Let’s name our snippet test and paste the following code into the main editor.

Now, we’ve created a simplified snippet for generating our test function signature. However, this snippet is not really useful as its user cannot navigate through the underscored chunks. In order to employ the Resharper’s navigation features, we have to slightly modify our snippet. Let’s replace the previous code with this one:

Please notice that now some parts of the method’s name are written between dollar signs. Thanks to this syntax, the Resharper is able to iterate over these parts and lets us change their names on the fly. $END$ is a special control sequence which tells the Resharper where to stop iteration over variables. At this moment, our snippet is fully functional and we can use it by typing the word test and hitting the TAB button.
custom snippet
SnippetFirstChunk

Resharper – custom snippet

ASP.NET MVC – Custom ActionResult – PDFResult

1. Introduction

In the company I’m currently working for, it is a common practice to use Devexpress XtraReports to create all kind of reports. Usually these reports are embedded into html page and used along with DevexpressReportViewer. However, lately I have been asked to open a report as a PDF file, without putting a viewer into a html page.

2. First approach

After some digging, I created an action in controller which looks like this

In the first step I made a new instance of a devexpress report(TestReport), then I called a method CreateDocument which is responsible for creating a content of report. Having prepared the report, I could use the ExportToPdf function which creates a stream in a PDF format. In the last step, I cleared Response property and added an array of bytes into it. I also had to change Response.ContentType to “application/pdf” in order to inform a browser that I’m sending a pdf file.

3. Second approach – custom ActionResult

The solution presented above works fine, however, I wanted to create something more reusable, that is why I decided to create a custom ActionResult – PDFActionResult. In order to create my own ActionResult, I created a PDFActionResult class which inherits from an abstract ActionResult class.

As you can see, all logic from the ExportToPdf action was transferred to the PDFActionResult class. Now, all you have to do to return Pdf from DevexpressReport is to return the PDFActionResult from your controller action.

custom ActionResult
Source code for this post can be found here

ASP.NET MVC – Custom ActionResult – PDFResult

Argument validation with attributes and Ninject interceptor

1. Introduction

It is a common practice to validate function arguments before running actual code. Usually validation looks like that:

There is nothing wrong with this code, however we have to repeat this piece of code in every function. Of course we could create some helper class for argument validation and call approperiate validation function before launching rest of the code

However I was searching for more universal way for argument validation. That is why, I decided to try function interceptors and attributes.

2. Creating validation attributes

Let’s start for creating base attribute

In the next step let’s create specialized attribute for “not null” validation. This argument may look this way:

3. Using IInterceptor interface

That was an easy part, now it’s necessary to somehow intercept function execution and run attribute validation.In .NET there is no build-in mechanism to do that,that is why I’ll use extension for Ninject – Ninject Interception. First of all we have to add to our project three dll’s

  • Ninject
  • Ninject.Extensions.Interception
  • Ninject.Extensions.Interception.LinFu

You can easily add them using NuGet. Having all necessary files added to solution,let’s create function interceptor – ValidaitionInterceptor.

As You can see ValidationInterceptor implements interface IInterceptorwhich belongs to Ninject.Extensions.Interception. This interface has one method Intercept which is called before function from registered component is called. In this function I iterate over all function attributes of type ArgumentValidationAttribute and call ValidateArgument function. If validation passess function invocation.Proceed() is called, which continues execution of original function.

4. Example of usage

In order to take advantage of benefits of function interceptors, we have to resolve objects from Ninject container. First of all I’ll create some examplorary classes

In the next step I’ll create simple wrapper for Ninject container and register CarRepository in Kernel

Please, notice that registration of CarRepository was used with option Intercept().With(). Thanks to this configuration,everytime we call method from CarRepository, Intercept method from ValidaitionInterceptor will be called first. Now, it is time to use our repository,we could do that this way

Argument entity in method Update from interface IRepository was decorated with NotNull, that is why our validation mechanism should throw exception.
CallingValidationInInterceptor
Argument validation

5. Validation of return value

Using IInterceptor we could go one step further and also validate return value of method. In order to do that, we have to slightly modify Intercept method in ValidaitionInterceptor class.

We also have to change attribute usage of ArgumentValidationAttribute to

Now, we can decorate function GetAll with NotNullattribute

Since this moment, everytime function GetAll returns null, the ArgumentNullException will be thrown.

6. Drawbacks

Unfortunately using attributes for argument validation have some drawbacks. First of all, argument validation is based on function interceptors, and as I mentioned before only function, which are in objects, which are resolved from IoC container can be intercepted. What is more, interceptors allow us to intercept only functions marked as virtual or inhirited from interface. Source code for this post can be found here

Argument validation with attributes and Ninject interceptor

ASP.NET MVC javascript debugging using WebStorm

I’ve been working with ASP .NET combined with Extjs for about half a year. All this time for debugging I’ve been using Firebug in Firefox, or Developer Tools in Chrome. Although both of these tools are great, I decided to try to use build in WebStorm debugger. According to documentation this debugger should work with Firefox as well as with Chrome. However I’ve only managed to run it correctly with Firefox. In order to start debugging session, we have to make some preparation. Let’s choose option Debug… from Run menu
rundebug
In opened small window, we have to choose first option – Edit configurations…
editconfigurationsmall
In the next window we can create new debugging profile. In order to do that, click on + sign in the left upper corner of window, then choose JavascriptDebug and Remote option.
runremotedebug
This is the final step of the configuration. The most important fields in configuration window is URL to open field, in which we specify adress of our site. In my case it is localhost on port 8142. We also have to specify mapping from remote host direcotry structure to our directory structure. Sample configuration is presented below
sampleconfig
We also have a possibility to choose browser in which our site will be displayed. I decided to choose Firefox because of the fact, that WebStorm debugger had some problems with Chrome. Now we can try to run debugging session by pressing ALT+F9. If we choose Firefox, in the very first time we run debugger, we will see this window
installExtension
which informs us that WebStorm must install extension to Firefox. Clicking OK button starts Firefox
AddOnInstalation
which warns us that external program is trying to install extension. Clicking continue button will finish configuration process, and WebStorm will successfully attach debugger.
debugging

ASP.NET MVC javascript debugging using WebStorm

Extjs 4.1.1 – how to fix row resize phenomena

Recently I’ve encountered annoying problem with extjs grid. After hiding columns with long wrapped text, height of rows was dramatically increased. Here are some screens presenting this particular situation. Grid with all columns shown;
problembeforeshow
and grid with one column hidden
row resize
The code responsible for rendering this grid is shown below

After some digging, it turned out that Extjs hides columns by setting their width to 0px. In order to solve this problem and preverse text wrap in cells, I implemented small workaround. Let’s start from creating CSS class which disables word wrapping in grid cells.

Next, we have to start listening for two events

  • columnhide
  • columnshow

In handlers of these events, we have to modify tdCls property of hidden column – add noWrap class, if column is hidden and delete this class otherwise.

From now hiding and showing columns works fine
fixedAfterHide
Entire code listing is presented below

Extjs 4.1.1 – how to fix row resize phenomena