Alexander Beletsky's Development Blog: 2011-03

ASP.NET developers disease

We are trying to hire web developer with using of Microsoft technology stack of ASP.net/C#/WebForm/MVC etc. So, I do a lot of interviews nowadays and I found out interesting fact. I've noticed symptoms of disease many of many ASP.net guys suffer from. The people who spent a lot of time with WebForms do not really understand web.

Originally WebForms has been designed in perspective to be conveniently used by Visual Basic developers. Ones who created UI in designers and put logic of application into control event handlers methods. That was a good idea because that gave possibility to many people switch to web world, without too much harm their own world of Windows programming (controls, events, handlers). That worked great, because all you have to understand is Page Lifecycle, be aware of controls that Microsoft created for you and be able to drag the control in WYSIWYG editor. The application created in this way was mostly fine, because it was to far to Web 2.0, AJAX, CSS and HTML revolutions.

Time has passed. Web stepped far away from a form with submit button and tables for everything, into rich UI applications with complex client side code, styled up with CSS. WebForms issues appeared really seriously, because it too much abstracted you from "what is actually going on". WebForms controls generated HTML for you, did a server side validation. You could tweak a bit of CSS on server side as well. But this is a not controlling of your application, but rather adapting to "old-school" type of web development with ASP.net. With such level of abstraction you were loosing many important things.

I'm not saying the WebForms is bad. I'm saying that WebForms did a bad joke for the people who do not try to see bit far from their nose. If you open your eyes you would see how much web development is far from ASP.net WebForms development. Modern web development is actually started on client side. You have to understand HTML/JS really fine to be efficient in it. You might not be a designer, but you have to be able to style up your HTML from UI mock in PSD. You should understand REST and AJAX to make go from web page to web application. And the issue is - the most ASP.net developers do not know these things. They think that knowing a C# or VB.net is much OK for creation of web application and the rest is work for front-end developers.

Guys, seriously.. If you think: "HTML is easy, I could do learn in 5 mins", "CSS is nothing to do..", "I don't want to do javascript" - you have those symptoms. Please try to markup the page that might look like that, enchase it with CSS3 transitions and shadows. Try to use jQuery for handling UI. You will see it is not easy at all. And basically your knowledge on C# does not make help you. I would say, it might be even harder than you application server side. Knowing how to do things with WebForms is great, but you should not be only limited to WebForms.

So, if your CV says you are web developer, please make sure that your are really able to do web products. Do not limit your self to "client-side" or "server-side" developer, it does not work any more. Be universal, look for the same things in a different angles, that helps sometimes.

The best day in developer's life

What would you think is best day in developers life? First day of work, last one? Salary or bonuses payment day? All of those are pretty nice, indeed. But I think best day in developers life is day of Release. It is exactly the time than the code you created recently is being pushed out to production and customers are able to see what changes you've done so far. Release makes a really good feeling of "work is done". In reality, of cause, work is not stopped but only begins after release date - production issues, customer reports, change requests.. all of these still need to be handled.

Releasing a product is big job. And it is not all about coding. First of all it is well coordinated effort of all groups - Development, QA, Documentation, Product Owners, Stakeholders etc. I've seen situations than products that could be potentially released just stuck on a line, because of lack of coordination. Here the team atmosphere plays important role. If team sees the goal, agreed on method and tools and communicated well it is a big step to success.

Yesterday, Report Designer product that is part of E-conomic has been released. It took about an year to make this happen. Huge amount of work were done, many lessons learned. We've passed long way from Specification till Integration code to main branch. I personally that it was just too long. With current experience I see a lot of value in frequent releases, so I tend to just-in-time production practices as Kanban. I world of web products, feedback is extremely fast and cost of defect is not so high, comparing to desktop.

It is good time to exhale, inhale fresh air and self-motivate for next interesting projects. We are now looking forward to make a better codebase, better processes and better products as result. Let's start new adventure!

Clean tests with SharpTestsEx

I was recently been adding NUnit to one of mine project's and noticed one interesting framework in Nuget channel. It is SharpTestsEx by Fabio Maulo and its primary goal is to make your assertions shorter, cleaner and easy to read.

To start with, it is just enough to install it through Nuget (or manually) and add using statement in your test class file.

using SharpTestsEx;

SharpTestsEx would add number of Should() extension methods. Suppose you have such case:

[Test]
public void Compile_Div_EmptyDivElement()
{
    // arrange
    var compiler = new Compiler();

    // act
    var result = compiler.Compile("div");

    // assert
    Assert.That(result, Is.Equal("<div></div>"), "expected and actual results are different");
}

With SharpTestsEx I'm changing assert part of test to,

[Test]
public void Compile_Div_EmptyDivElement()
{
    // arrange
    var compiler = new Compiler();

    // act
    var result = compiler.Compile("div");

    // assert
    result.Should().Be.EqualTo("<div></div>");
}

Note, it is much shorter and mostly plain English sentence: result should be equal to something.. The assertion message for this test is really clean and sufficient, so you mostly won't needed and custom messages.

If I go further and implement extension method for this Compile method:

static class CompilerTestExtension
{
    public static string Compile(this string expression)
    {
        var c = new Compiler();
        return с.Compile(expression);
    }
}

My tests case would be just one line of code:

[Test]
public void Compile_Div_EmptyDivElement()
{
    // arrange / act / assert
    "div".Compile().Should().Be.EqualTo("<div></div>");
}

This is very short and clean notation of test case, even not technical person could read this. You can do more complicated assertions, combining them by And / Not conditions. There are also bunch of useful extensions for strings, sequences.

Additional information on project site as well as author blog.

My first year in company

It is exactly one year since I joined E-conomic company. Last year I decided to switch the jobs and now I realize how much right decision it was. I got invitation on interview to E-conomic where I did a small test application and presented it to management and technical guys (hi Jakob, Chistian). I had no great experience with ASP.net that time, since last years of my career I spent mostly on management tasks. Really, I just reviewed the code that I created year ago and I see it was not so good :). Fortunately, those guys believed that I could bring a value and offer me a job. Since then I try to do my best to proof - you were not wrong.

I see work in company as kind of mariage. Indeed, to feel happy in marriage you should have really strong match and level of trust to each other. Probably it is E-conomic where I first time understood how important trust between development and management. I forgot what the "estimation negotiation", I forgot the problems with unrealistic project plans created by management and business guys. A lot of problems that only created conflicts on my previous jobs here done in very smooth way.

We are developer-centric company. Developers have power here, developers take a decisions, developers do the job. But with great power comes great responsibility. Developers are totally responsible for product quality. Here I saw a new model, not "Software Developer" but rather "Product Developer".. Product developers job is not ended on coding and integration changes, but it is more for verification, configuration management and release. We don't have testers here, but we have QA. And developers are part of QA also.

I enjoy the environment here very much. Even we are highly distributed team I feel the shoulder of each team mate. We working closer, doesn't matter we are sitting >1000 km from each other. That was not so fast to get used to work like that, but now I have no problems at all. I really think that I'm part of the rest of team who mainly located in Copenhagen. This environment constantly inspires: I was happy to restore my blog (that I created in 2008, but then froze it), I do a lot of reading to understand modern web development, I released my small product.. and have bunch of new ideas and feel only lack of time to work on all of them :).

I would also say that Ciklum as Ukrainian representative does job very well. Basically 2 major things that I see Ciklum is great: it handles all infrastructure and finance very professional and does not interfere to work directly with client. If I have some kind of problem I know Ciklum would solve it, this is also good level of trust and I appreciate that.

Yes, I'm just happy developer now. And I really much correlate this with E-conomic. Last year decision changed my life. I think I found the place where I like to work, even if I spend more than 7 years for that.

Integrating ELMAH to ASP.NET MVC in right way

I've received great feedback on this article. Thanks all of you guys, who provided valuable comments and pull requests on github. I edited the article several time, hope its up-to-date now. You can go directly to code repository and get the test application, to see what it is all about. In case if you having some issues, just let me know.

UPDATE: Elmah.MVC is now released as NuGet package. No need to read that long blog post, just install it.

Many of you know what the ELMAH is. If you still don't, go and read here. If you care about monitoring and stability of your web application, you definitely consider to integrate ELMAH. There are number of guidelines of how to do that. But all of them do not answer several question that MVC developers really concerned about:

  • How to add ELMAH as usual MVC controller?
  • How to add ELMAH handler to make it secure (accessible only by authorized users) in MVC application?

I'll shed the light on both questions here.

Quick start up

The best way to add ELMAH is to use Nuget package manager (really recommend to install it). I'm using Nuget UI, so just right click on MVC application, select "Add Library Package Reference and pickup ELMAH from nuget feed. Nuget will add reference and update web.config with all required configuration. Basically you can start you application with http://localhost/yourapp/elmah.asx and see main ELMAH page.

Changing the configuration

ELMAH have been developed in ancient history times of Web Forms. And it is nicely done to be as much universal as possible and implemented as HttpHandler. In web.config you have several line of configuration that register this handler. This is of cause works, but URI like this http://localhost/yourapp/elmah.asx is absolutely not MVC way. So, what you have to do is simply comment out those configuration.

<system.web>

        <!--<httpHandlers>
            <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
        </httpHandlers>-->
  
    </system.web>

<system.webServer>

    <!--<handlers>
    <remove name="Wild" />
    <add name="Elmah" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
    </handlers>-->
 
</system.webServer>

Add ELMAH controller

Now add ELMAH controller to application, the code of controller is:

public class ElmahController : Controller
{
    public ActionResult Index(string type)
    {
        return new ElmahResult(type);
    }
}

And setup a proper routing for it:

context.MapRoute(
    "Admin_elmah",
    "Admin/elmah/{type}",
    new { action = "Index", controller = "Elmah", type = UrlParameter.Optional }
);

Implementation of ElmahResult action result

He we go for most interesting part. I've checked the sources of ELMAH and reused Elmah.ErrorLogPageFactory to handle those custom HTTP request. After several modifications and pull request by seba-i the code is:

class ElmahResult : ActionResult
{
    private string _resouceType;

    public ElmahResult(string resouceType)
    {
        _resouceType = resouceType;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        var factory = new Elmah.ErrorLogPageFactory();

        if (!string.IsNullOrEmpty(_resouceType)) {
            var pathInfo = "." + _resouceType;
            HttpContext.Current.RewritePath(PathForStylesheet(), pathInfo, HttpContext.Current.Request.QueryString.ToString());
        }

        var httpHandler = factory.GetHandler(HttpContext.Current, null, null, null);
        httpHandler.ProcessRequest(HttpContext.Current);
    }

    private string PathForStylesheet() 
    {
        return _resouceType != "stylesheet" ? HttpContext.Current.Request.Path.Replace(String.Format("/{0}", _resouceType), string.Empty) : HttpContext.Current.Request.Path;
    }
}

Try it now

Just build the application and put http://localhost/yourapp/admin/elmah and you will get nice looking ELMAH main page.

Making it secure

AuthorizeAttribute works great for this purpose. Just put this attribute to ElmahController and now it is only Admin, who has access to it.

[Authorize(Users = "Admin")]
public class ElmahController : Controller
{
    // ...

In my applications I use a custom attribute, but the idea still the same.

Allow to view results remotely

You definitely want to see results remotely, with no need to access hosting machine. By default, ELMAH do not allow this, but it is easily changed in configuration:

<elmah>
  <security allowRemoteAccess="yes" />

Code example

Full code example is located in my github repository here.


UPD:

I've moved out controller to separate github repostitory. You are free and very welcome to use/fork/pull changes :). Fork & fix it here - https://github.com/alexanderbeletsky/elmah.mvc.controller.

UPD2:

Instead of placing just controller I create test MVC3 application that clearly show how to use Elmah in MVC. Plese check code and README here.

UPD3:

Code placed in this blog post has been changed drastically by latest feedback I recieved for this controller. Please better refer code on github or latest blog post here Latest version of ELMAH MVC controller