Sunday, May 31, 2009

Flexigrid

Did you ever wanted to have an all-in-one solution for grids in your application? One that does Ajax refreshes, supports sorting, filtering and pagination out of the box. How about an API that's actually usable?

I've got some good news for you - it's there! It is called Flexigrid and is in fact a plugin for jQuery.

For the purpose of demonstration I've put together an example that does pretty much everything I've mentioned above.

FlexigridExample.zip

Here are some points worth noticing in the provided example:

1. The CSS in Internet Explorer is trashed. I don't know how to fix it.
2. The real power that feeds the data is to be found in BookController and is well documented.
3. Since there's not much documentation for Flexigrid itself I suggest taking a look at the list of options in flexigrid.js.

Enjoy this fantastic library!

Wednesday, May 20, 2009

Creating custom controls in ASP.NET MVC

Today I've been going over the process of creating a simple ASP.NET MVC control. At the first glance it might seem unnecessary since we have the HtmlHelper class. However the main difference is that MVC Controls have superior design time support!

Let's get down to business, shall we?

First you need to import Microsoft.Web.Mvc.dll assembly to your project.
Next you create a class that inherits from Microsoft.Web.Mvc.Controls.MvcControl. I put my controls into Controls folder in my solution so my controls are in the namespace (for example) MvcApplication1.Controls.
The actual creation of the control is really simple: you override the Render method and use the writer object to pass on some text to the rendering pipeline.
Here's a ready-to-use example:

using System;
using System.Web.Mvc;
using Microsoft.Web.Mvc.Controls;

namespace MvcApplication1.Controls {
public class Status : MvcControl {
public String Key { get; set; }

protected override void Render(System.Web.UI.HtmlTextWriter writer) {
TagBuilder tag = new TagBuilder("span");
String data = String.Empty;
if (!DesignMode)
data = ViewData[Key] != null ? ViewData[Key].ToString() : "";
else
data = "#" + Key;
tag.SetInnerText(data);
writer.Write(tag.ToString());
}
}
}

Remember to compile your project before moving on to the next step!!!

Next you need to do one thing to get your controls to the page:

Register your namespace with prefix for ASP.NET WebForms engine by adding the following line to your Web.config file in section system.web/pages:

<add tagPrefix="custom"
namespace="MvcApplication1.Controls"
assembly="MvcApplication1"/>

From now on (after a few moments when VS/VWD thinks if your control is worth using :D) you'll get the IntelliSense support for your new control with all the public properties it has.

Here's an example usage of the control described above:

<custom:Status runat="server" Key="Example" />

As you can see this control is rendered by the server (runat="server") and has one property (or rather an attribute) called Key. Taking a look at the code reveals that the actual meaning of this field is that it serves as a key in ViewData map.

Bye!

"Error creating control" in VS and VWD

There's a nasty bug in VisualStudio 2008 SP1 and Visual Web Developer 2008 SP1 that makes the usage of custom controls virtually impossible. Everytime you try to set a property (via Properties or directly in code) you'll get a gray error message saying that the value of your property cannot be set.

This is a known issue and there's a simple remedy to that. Close VS/VWD, install this hotfix, start VS/VWD again and you're good to go.

http://code.msdn.microsoft.com/KB961847/Release/ProjectReleases.aspx?ReleaseId=2646

Happy coding!

Monday, May 11, 2009

JSF Nonsenses

Hi,

As you have probably noticed I'm not the biggest JSF supporter out there. In fact JSF has been driving me mad for quite some time now and I've decided to take revange.

So here it is: JSF Nonsenses

Jump in from time to time if you want to - if you don't that's fine by me. I just need a place where I can publicly discredit the ideas behind declarative-only and I'll do it all for you approach.

If you feel the same way I do please share! If you don't and you think I'm nuts please share your opinion as well.

MVC RuLeZ!

Colose encounters with J2EE/JSF

Hi,

One of the projects I work on involves heavy JSF usage. Along with other tools to make our life "easier" (Facelets for example) the project runs quite smoothly. Right up to the point where a little bit more "custom" solution is required...

Here's the problem: I need to call a method that will return some JSON that will be evaluated by the client and according to the result of this operation some action must be taken.

As long as I wanted to use the rather limited XMLHttpRequest capabilities provided by Ajax4JSF I was fine. Now I need a little bit more control and flexibility over the actual flow and I am screwed. I'm not really interested in the whole life-cycle of a JSF page and all that statefull crap JSF provides - all I need is a simple call to a method and return a little bit of text - that's all.'

As it turns out there's no such thing in JSF directly. There's however a big honky machine to actually hide the inner-workings of JavaScript from an evil developer like me. I can almost hear the creators of Ajax4JSF saying "no no.. that's not allowed here, nigther is that, and oh that's because of X and that's because of Y and that's because it is like that, period". I'm sick and tired of this miserable framework.

So far I've not found a single thing that I like about JSF (MyFaces and RichFaces in particular). To start a project it's a configuration nightmare (if you want to have more than one JSF component library like RichFaces and Tomahawk), some things are simply incompatible. RichFaces force me to use stone-age version of jQuery (my favorite and better with each release), mix things that don't really come together (like server-side code being "executed" by so called "client actions" for example). The whole beautiful stateless nature of the web is gone with the wind in the worst possible style. And last but definitely not least: there's no control whatsoever over the generated markup, nada, zipp, null... And let me tell you, it's huge!

Apparently the designers of RichFaces (and other JSF-related frameworks) didn't follow the news and in their urge to "define" every damn thing they have forgotten that it actually matters in what way you define the layouts. Doing it with multi-level tables is probably the worst thing ever about the generated HTML, but it's definitely not the only horror you'll see. Constant 'styles' and 'script' blocks, intrusive JavaScript and the like are just standard. How can something so bad have become so popular? Oh I get it: scriptlets are the source of all evil in the enterprise application world... How could I forget that mantra. Apparently URLs are also evil as they have no damn meaning in JSF. Posting a form (or however its called in the JSF world) reveals the concept of ghost-urls: you're seeing the next page but the URL didn't get the memo yet. Whatta heck???

No wonder that MVC-based solutions (Spring MVC, ASP.NET MVC, Rails, Django) are getting more and more popular. Those are actually non-hammer tools to get the job done without 100-line exception messages getting in a way.

And don't get me wrong: I'm sure JSF is good for something - it just so happens it's not the creation of highly interactive, flexible and customizable Web 2.0-enabled enterprise applications.

"Close encounters my a..." - John Criton / Farscape.

RadioTaxi Manager gone OpenSource!

RadioTaxi Manager is one of those projects that I've created a long long time ago and that has actively been in use for the better part of last 10 years. Finally after all those years since I really can't find the time to develop it further (and there are new requirements coming!) I've decided to go OpenSource.

The code base is full of legacy stuff, polish variable/procedure/table names and a lot of other stuff that will most probably make it very hard to work with.

Anyways, here it is: http://code.google.com/p/rtmgr

I've also setup a mailing list for this project at http://groups.google.com/group/rtmgr-dev.

Should you be willing to participate drop me a note. I'm sure we can work something out!


Have fun!

Tuesday, May 5, 2009

ASP.NET MVC scaffolding

I've been recently playing a bit with the scaffolding capability in ASP.NET MVC tooling support. Being it all nice and dandy it's still a lot of work to be done to get a simple CRUD operation working with any kind of data access technology.

As a result I've started a code/view generator that will allow for bottom-up scaffolding of all the typical layers in a web application: the entity being persisted, data access using NHibernate, a pass-through service layer, the controller with default CRUD actions and a set of views supporting the controller.

As a matter of fact the basic idea behind this tool came from the Rails framework. In there you can scaffold an entity with a page and all the default CRUD behavior from the command-line. This tool will allow for exactly that but instead of taking advantage of the field:type syntax it'll use the NHibernate hbm.xml mapping files to describe an entity and its relation to other entities.

Here's a link in case you want to monitor the progress or (if you feel up to it) to join the effort:

MVCGen

Have fun!

EDIT on 21/06/2011: since I'm no longer working with .NET (or ASP.NET MVC for that matter) the project is dead.