# Tuesday, July 31, 2007

There seems to be quite a bit of demand for a preview of ProMesh.NET, my MVC-type Web Application Framework for .NET 2.0. To satisfy this demand I decided to publish a small demo application (with source code) on CodePlex, which includes the current compiled ProMesh.NET assemblies

The demo application contains a web project and a unit testing project. The web application shows the following features:

  • Mapping of client variables to method parameters (even url parameters mapped to objects)
  • The template rendering engine
  • Form handling and validation (very basic in this application)
  • Typed session properties
  • Application configuration

The unit tests included in the example contain 2 tests:

  • Checking if access to unauthorized pages is properly blocked
  • Login procedure of a registered user

I expect the first public release (including source code) to be released sometime on wednesday or thursday.

Direct link to the download page on CodePlex: Click here

Update: In the meantime, the first beta has been published.

kick it on DotNetKicks.com
Tuesday, July 31, 2007 9:50:18 PM (W. Europe Daylight Time, UTC+02:00)  #    Comments [12] -
 |  | 
# Sunday, July 29, 2007

Today the ProMesh.NET project has been made public at CodePlex. ProMesh.NET is an MVC-based Web Application Framework for .NET 2.0

Features include:

  • Lightweight (<100KB) framework running on top of the ASP.NET HTTP pipeline (HttpHandler)
  • Controller-based for easy implementation of the MVC pattern
  • Flexible template-based rendering engine (using pure HTML files)
  • Very powerful integrated testing framework for running unit tests on your web pages
  • Easy mapping of parameters and other client data to typed method parameters and class members
  • Flexible and easy to use form generation and postback handling
  • Pluggable localization module (use translations from resource files, database, …)
  • Integrated logging and profiling
  • Optional integration with the CoolStorage.NET ORM library
  • Built for performance

The CodePlex project currently contains a short walk-through of the features. The code hasn’t been published yet, but will be available sometime next week.

A support forum is available for all questions about the framework.

kick it on DotNetKicks.com
Sunday, July 29, 2007 12:41:24 PM (W. Europe Daylight Time, UTC+02:00)  #    Comments [10] -
 | 
# Thursday, July 26, 2007

These days, every blogging developer seems to have a list of tools and libraries they are using on a daily basis. In many cases those posts were pretty useful to me, as I got to know a few tools I didn’t know.

So, here’s my list…

Development tools

Libraries / Frameworks

kick it on DotNetKicks.com
Thursday, July 26, 2007 11:32:32 PM (W. Europe Daylight Time, UTC+02:00)  #    Comments [2] -
 | 
# Wednesday, July 25, 2007

Yesterday, an old article resurfaced on dotnetkicks.com about validating Guid strings. Several comments hinted at the possibility of letting the .NET Framework convert it to a Guid and catching the FormatException.

WTF !?

I must be very old-fashioned, because I am still very religious about (mis)using exceptions in normal application flow. Exceptions were designed for exceptional events, not for validating user input.

Even Microsoft picked up on that, because they added a bunch of TryParse() methods in version 2.0 of the .NET Framework.

In case you’re wondering if there are any REAL reasons for avoiding exceptions in the normal flow of an application, I’ll give you two:

  1. Exceptions are slow. Let me repeat: SLOW.
  2. When you are in a debugging sessions, and you told the debugger to break at the first exception (which is often done), you will know what I’m talking about.

 

kick it on DotNetKicks.com
Wednesday, July 25, 2007 8:31:38 PM (W. Europe Daylight Time, UTC+02:00)  #    Comments [0] -
 |  | 
# Tuesday, July 24, 2007

In one of my previous posts, I talked about the separation between code and markup, which caused quite a bit of turmoil, especially from the PHP and MonoRail enthusiasts.

Maybe I didn’t make myself perfectly clear. I don’t have a problem with embedding view logic inside HTML markup, as long as it stays within reason. On the other hand, application logic should be kept on the server (in the controller or deeper layers).

I mentioned MonoRail (combined with the NVelocity engine) because I saw two things I didn’t like:

  1. The scripting language goes too far
  2. The templates are not designer friendly

1. The scripting language

In my (humble) opinion, a template rendering engine should be limited to the following constructs:

  • Looping over a collection
  • If…else… constructs for showing conditional blocks
  • Embedded variables fed from the server

NVelocity adds macros and variable assignment. These features allow complete programs to be written inside the template, and this is not what we want (again, IMHO)

2. Designer friendly

An HTML template file that can’t be opened by an HTML editing application is worthless. The HTML in the template should also be valid.

The NVelocity templates used by MonoRail don’t satisfy these criteria.

The first problem is that the *.vm files are actually pieces of HTML, but they are not HTML documents because there’s no <html> and/or <body> tag. HTML designer application don’t like this.

The other problem is that the NVelocity templates are not valid HTML. For example, the following fragment is not valid HTML:

<table>
  <tr><th>Header</th></tr>
  <tr><td>Row1</td></tr>
#if (showThisBlock)
  <tr><td>ConditionalRow2</td></tr>
#end
</table>
 

The only markup allowed between </tr> and <tr> is comments. The solution is easy: embed control script inside HTML comments. That’s how I did it, so this is how the template looks using ProMesh.NET:

<table>
  <tr><th>Header</th></tr>
  <tr><td>Row1</td></tr>
<!--$(if showThisBlock)-->
  <tr><td>ConditionalRow2</td></tr>
<!--$(endif)-->
</table>

 

I hope this post clarifies my earlier post. Don’t get me wrong: I really like MonoRail. In fact when I first read about it, I was surprised by the similarity to what I had written for my own use. I suppose this means I agree on a lot of things with the MonoRail developers :-)

kick it on DotNetKicks.com
Tuesday, July 24, 2007 10:16:33 PM (W. Europe Daylight Time, UTC+02:00)  #    Comments [4] -
 |  | 
# Saturday, July 21, 2007

In the early days of web development, it was common procedure to write application logic inside your HTML pages using ASP, JSP, PHP, Perl or another early web application development framework.

Of course, back then we didn’t know any better, but don’t you just cringe when you see something like this:

<table border="1">
<%
For I = iRecFirst To iRecLast
	Response.Write "<tr>" & vbCrLf
	
	For J = iFieldFirst To iFieldLast
		Response.Write vbTab & "<td>" & arrDBData(J, I) & "</td>" & vbCrLf
	Next	
	Response.Write "</tr>" & vbCrLf
Next ' I
%>
</table>

I know, this looks pretty simple, and the learning curve to write applications in this way is not that steep, but maintaining something like this is a nightmare.

Microsoft saw the need for something better and came up with ASP.NET. Indeed, at first glance it was a big improvement, urging developers to cleanly separate code from markup by providing the "code behind" way of programming. But if you look closer it was still a mess. Let me tell you why:

  • There’s a (very limiting) 1–1 relationship between the view and the code. Every .ASPX has one code behind file, and one code behind file has one .ASPX file. Implementing the MVC pattern is impossible without resorting to a bag full of tricks.
  • Events are wired in the markup, so the markup is controlling the application, not the other way around
  • The .aspx files are "kind-of" HTML files, but when you try to edit them in a standard HTML editor (Dreamweaver for example), you're in for a (nasty) surprise. ASP.NET tags are not recognized correctly, let alone rendered. So you are pushed into using the Microsoft ASP.NET markup editor, which is the crappiest piece of junk ever built.
  • Properties influencing the behavior of user interface elements are defined in the markup. And the other way around: properties defining the look of your application sometimes have to be defined in code
  • Standards based HTML/CSS? What standards based HTML/CSS?

To this day, Microsoft is still holding on to this broken development model for web applications. The Java guys had more inspiration in that area: Struts, Tapestry, Wicket, SiteMesh, Spring, ... Good ideas that quickly turned into Enterprise Frameworks that were too intimidating for the average developer (isn't this a pattern for all Java-based technologies?)

Light at the end of the tunnel?

Maybe... Ruby On Rails, MonoRail, DotNetNuke, Spring.NET,... Cool stuff, VERY cool stuff.

Which brings me to the point of this article. While I didn't have time to look at all these frameworks, I am still a little annoyed by the lack of separation between the actual presentation (markup) and the code controlling the presentation. The other day I was checking out MonoRail and was browsing through the samples, and I saw a lot of ASP-like stuff in the HTML markup. Loops, method calls, the whole shebang. I felt thrown back to the nineties.

Maybe I am too religious about this, but I like to see a 100% separation between code and markup (presentation). My requirements for a web application framework are pretty clear:

  • Pure HTML files should be used. By pure HTML, I mean that you should be able to give the HTML files to a web designer and let him/her make changes without having to worry too much about these funky codes that these weird developers have put in.
  • No hints of any application logic or code in the presentation markup
  • The learning curve should be gentle, without losing flexibility and performance
  • MVC has to be easy to implement (or included in the framework), without forcing developers to use it. 90% of web developers don’t use MVC, so don’t force them to use it if they feel more comfortable developing in other ways
  • Easy Ajax integration (I am not talking about black box toys like Microsoft Ajax)
  • Performance! Performance! Performance!

In a few days, ProMesh.NET, the web application framework I have been building, using and improving for the last 4 years will be published as Open Source on CodePlex. It’s a lightweight , fast web application framework that satisfies the requirements listed above and I look forward to sharing it with the .NET community.

kick it on DotNetKicks.com
Saturday, July 21, 2007 11:26:31 PM (W. Europe Daylight Time, UTC+02:00)  #    Comments [6] -
 |  |  | 
# Tuesday, July 17, 2007
Ever wanted to get a list of all types (classes) that inherit from a specific base class or implement a specific interface? If the answer is yes, read on, because it is actually very simple.

In .NET, if you want to check if a type is a base class or interface for a given type, you simply call the method IsAssignableFrom() on the base type or interface. To get get all types satisfying this criteria, simply look at all the types in all assemblies...
public static List<Type> FindCompatibleTypes(Type baseType)
{
   List<Type> types = new List<Type>();

   foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
   {
      foreach (Type type in assembly.GetTypes())
      {
         if (type != baseType && baseType.IsAssignableFrom(type))
            types.Add(type);
      }
   }

   return types;
}
I told you it was simple :-)
kick it on DotNetKicks.com
Tuesday, July 17, 2007 8:50:48 PM (W. Europe Daylight Time, UTC+02:00)  #    Comments [0] -

# Sunday, July 15, 2007
Let me first give you some background information:

Number formats are different in different parts of the world. North Americans use the "." to separate the decimal part of a number. Europeans use ",". The "thousand separator" is the other way around.
The .NET framework will automatically select the correct notation depending on your geographic location, but this is where it all goes wrong for Europeans (and sometimes even for non-Europeans).

First of all, many websites are targeted at the whole world, so they run into trouble when people are required to enter a number with a fraction part.
Another problem is that in the beginning of the personal computer age, there was no such thing as "regional settings", so computer users grew accustomed to entering a "." for decimal numbers. And old habits die hard, so European software developers are in trouble.

So I decided to allow both forms of input in all my applications. This makes everybody happy and causes less frustration (and support headaches for me).

In .NET, you implement this with a simple call to the Parse() or TryParse() method:
double.TryParse(stringValue.Replace(',', '.'), NumberStyles.Any, 
NumberFormatInfo.InvariantInfo,
out doubleValue)
double.Parse(stringValue.Replace(',', '.'), NumberStyles.Any, NumberFormatInfo.InvariantInfo)

There is one slight drawback: users are not allowed to enter thousand separators, but I have never encountered anyone doing this.

kick it on DotNetKicks.com
Sunday, July 15, 2007 2:01:49 PM (W. Europe Daylight Time, UTC+02:00)  #    Comments [1] -

# Saturday, July 14, 2007
We all know the Convert.ChangeType() for changing a value from one (unknown) type to another (unknown) type at runtime, but this method doesn't handle nullables and enums very well.

What may seem a simple task isn't that simple after all. You have to differentiate nullables, value types and enums. I threw in a little recursion, and I came up with this:
public static object ConvertType(object value, Type targetType)
{
if (value == null)
return null;

if (value.GetType() == targetType)
return value;

if (targetType.IsValueType)
{
if (!targetType.IsGenericType)
{
if (targetType.IsEnum)
return Enum.ToObject(targetType, value);
else return Convert.ChangeType(value, targetType);
}

if (targetType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
Type realType = targetType.GetGenericArguments()[0];

return ConvertType(value, realType);
}
}

return Convert.ChangeType(value,targetType);
}

This method will do all conversions correctly, but it will return null if you pass null for a value type. This can be fixed with an extra if() check, but I thought it wasn't necessary.



The best use for this method is for converting values read from a database to a variable that is compatible with the database type, but not exactly the same.
kick it on DotNetKicks.com
Saturday, July 14, 2007 10:59:34 AM (W. Europe Daylight Time, UTC+02:00)  #    Comments [1] -

# Friday, July 06, 2007
Without going into specifics, ProMesh.NET is a web application framework based on the ASP.NET HTTP "Pipeline".

Key points about the framework:
  • Avoids the ASP.NET page model, so no ASP.NET web controls
  • Links directly into IIS using Http Handlers and Http Modules
  • Template-based system with master templates and content templates. Very powerful built-in or runtime server-generated macros can be embedded in the templates.
  • Templates can be created using off-the-shelve HTML editors like Dreamweaver and Microsoft Web Expression
  • Full control of the rendering process
  • Integrated with CoolStorage.NET
  • Built-in logging and statistics
  • ...
The project will probably be released as open source at CodePlex.

This framework has been used for internal projects for several years, including some high traffic sites like www.autosport.be and www.cartoonbase.com, so stability and performance have been proven. Now it's time to release it in the open...

kick it on DotNetKicks.com
Friday, July 06, 2007 10:35:52 PM (W. Europe Daylight Time, UTC+02:00)  #    Comments [3] -
 |  |  | 
# Thursday, July 05, 2007
I am often asked the question "Can't I just RUN my Windows Service without installing and starting it?".

My answer usually is "No, you can't but..."

There is a "but": you can create your Windows service as a hybrid application, so it will run as a console application also.

Why would you want to do that?
  • It makes debugging from within Visual Studio a breeze
  • Sometimes you want to show some debugging information in a production environment by using Console.WriteLine()
  • You don't always want to install your service to see if it runs in a particular environment
Turning a .NET Windows service into a hybrid application is actually very simple. In your Main() method you add the following:
static class Program
{
static void Main(params string[] parameters)
{
if (parameters.Length > 0 && parameters[0].ToLower() == "/console")
new MyService().RunConsole(parameters);
else ServiceBase.Run(new ServiceBase[] { new MyService() });
}
}
Then, in your service class, add a RunConsole() method:

   public void RunConsole(string[] args)
{
OnStart(args);

Console.WriteLine("Service running... Press any key to stop");

Console.Read();

OnStop();
 }

That's all there is to it. To run your service as a console app, just specify "/console" as the first paramter when running the .EXE.

UPDATE: all of this, and more is now all wrapped in a .NET library (open-source) named Vici WinService.

kick it on DotNetKicks.com
Thursday, July 05, 2007 1:40:15 PM (W. Europe Daylight Time, UTC+02:00)  #    Comments [4] -

# Sunday, July 01, 2007
I just added support for SQLite 3 to CoolStorage.NET (my object mapping library). I used the open source System.Data.SQLite ADO.NET provider which seems a pretty solid product.

Adding support for this "lite" database was pretty straightforward.

The only problem I had was with quoted identifiers: identifiers can be quoted using double quotes, but they behave a bit strange. For example, if you want to quote the identifiers in this query:

SELECT c.CustomerID, c.Name from tblCustomer c

you would write:

SELECT "c.CustomerID", "c.Name" from "tblCustomer" c

This doesn't work. You get the following result set:

c.CustomerIDc.Name
c.CustomerIDc.Name
c.CustomerIDc.Name
c.CustomerIDc.Name
c.CustomerIDc.Name


To fix this, you need to change it to this:

SELECT "c"."CustomerID", "c"."Name" from "tblCustomer" c

Other than that, I'm pretty impressed (so far)

Technorati Profile

kick it on DotNetKicks.com
Sunday, July 01, 2007 8:20:52 PM (W. Europe Daylight Time, UTC+02:00)  #    Comments [0] -
 |