I start working with ASP.NET 3 month's ago now. I began with great
expectations that the framework is well designed and helps the developer
to build maintainable and flexible web applications.
After these 3 months to work with, I start to understand that ASP.NET is not what I expected.
In my point of view, ASP.NET is not encouraging the developer to do good work.
I will give you further explanations.
First, I will tell you who is a web developer for me :
- A web developer must understand HTTP works and how
a web form works with POST and GET. Basically, he must
understand what make a web app different than a desktop app.
- A web developer must have a total and simple control on the generated HTML and have
to know HTML language perfectly. This is particuraly important if he needs tricky
user interface.
- A web developer must follow the MVC pattern or something approching and must
have the will to split the the business logic and the user interface.
- A web developer must know CSS and Javascript, or at least what is possible to do
with.
I was thinking that if the man fails in one of these areas, the result of his work
will just be a unmaintainable piece of crap.
It's because I didn't know Visual Studio 2005 and ASP.NET 2.0. These tools can acomplish an
impressive hat trick by transforming a VB6 drop & click programmer
in a new web 2.0 programmer. Great, the boss will be
happy. No need to hire a new expensive web expert, he can just stick with the old who
don't like to learn new things.
From a web developer point of view who has never actually used ASP.NET this
must sound a little bit exagerated. But this is not. You can really "write"
an ASP.NET 2.0 web app without typing any line of C# and knowing barely what HTML is.
Assuming you have a SQL Server database you
just have to run a bunch of wizards, tune big XML files, dropping some controls, DataViews,
and DataSources here and there, linking them with other wizards
press the "play button" and it's done, you have a CRUD web app.
But wait, the things that I have mentioned before are now completly pointless.
A ignorante guy can make a web app in minutes without knowing anything about web
devlopement ! I am a liar, no need to know all these things.
So, let's take a look in ASP.NET 2.0 and begin to write something. Not
just a oversimplified demo app that evangelists love to show. I am thinking
about a real life application full of little tricky things.
Real life web apps have something different that demos doesn't have.
They have to deal with unexpected complicated stuff that
the customer want.
That's exactly where the ASP.NET 2.0 framework sucks.
Despite the huge Object model cluttered with hundred methods and attributs
this framework can't deal with basic things.
DataGridView are DetailsView are good example of this. These objects are
leaky
abstractions that are used to generate HTML for the user.
It's the root of this framework to don't let the user
control how the HTML is rendered. Or in a so complicated way that is impossible
to work with.
In a framework that would let the user do what he want,
there would be a powerfull templating language that let him control
the output of the object. But better. If there is a powerfull templating
language, what is the meaning of these objects ? Stop using them and just push
the data to the templating language to render custom HTML. No, it's too
simple like that, and wait, you have to give objects to the VB6 programmer
and let him believe that these objects have a state like in desktop development.
To acheive this object state illusion the framework use tricks called
"THE CALLBACK" and "THE VIEWSTATE". You can recognize an ASP.NET programmer miles
away because they are speaking religiously about these cuting edge technologies.
In fact these two things are just ugly intrusive javascript and a hidden field
with serialized data in it.
To illustrate some things that I found very annoying, I need a simple example. So, I take the praised
ASP.NET 2.0 DetailsView object.
Basicaly a DetailsView is an object that can do CRUD on a database row. In other terms, you
can Create, Read, Update, and Delete a database row with that. That's cool !
So take a simple data model : a customer that have an email an username
and a password :
<asp:DetailsView ID="UserDetailsView" runat="server"
AutoGenerateRows="False" DataKeyNames="idUser"
DataSourceID="ObjectDataSource1">
<Fields>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<%# Eval("email") %>
</ItemTemplate>
<EditItemTemplate><InsertItemTemplate>
<asp:TextBox runat="server" id="email"
Text='<%# Bind("email") %>'></asp:TextBox>
</InsertItemTemplate></EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Username">
<ItemTemplate>
<%# Eval("username") %>
</ItemTemplate>
<EditItemTemplate><InsertItemTemplate>
<asp:TextBox runat="server" id="username" Text='<%# Bind("username") %>'></asp:TextBox>
</InsertItemTemplate></EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Password">
<ItemTemplate>
<%# Eval("password") %>
</ItemTemplate>
<EditItemTemplate><InsertItemTemplate>
<asp:TextBox runat="server" id="password" Text='<%# Bind("password") %>'></asp:TextBox>
</InsertItemTemplate>
</EditItemTemplate>
</asp:TemplateField>
</Fields>
<AlternatingRowStyle CssClass="even" />
</asp:DetailsView>
I think that "AlternatingRowStyle" show us something important here. How
i am supposed to do if you want to change the CSS Class each 3 lines ? It's
just impossible. Or maybe there is a "AlternatingRowStyleOneTimeOnThree". You see
the point ?
Imagine now that you want change a simple behavior. You want that in a special "edit mode" case the user cannot edit his username and that nothing show (based on a real customer demand).
My first idea was to use the inheritance (a masterpage) and make two pages. One for editing, the other
for creating. Both from the same master page. But I learn that is impossible to make inheritance
in controls. So I have to hide programmaticaly the "TemplateField".
My second idea was very in the ASP.NET style of doing presentation trick (this code is probably false, it's just an example):
<asp:TemplateField HeaderText="Username" Visible="<%# UserDetailsView.Edit!=true %>">
Sorry you cannot do that either, no code is possible in the "Visible" attribut of "TemplateField".
Finaly I find that the only thing
that is actually possible is to write ugly code in the "code behind" :
...
UserDetailsView.Fields[1].Visible = false;
...
A presentation trick in the controler with a hardcoded value ? Who want to write this these days ? And that's not
all : Add a row before the username and the code is broken. "AlternatingRowStyle" attribut
break the presentation by producing stupid behavior because the two remaining lines have the same CSS class.
At the end, my application was full of silly things like these. This is not fun and maintainable at all.
So ... The C# code is full of presentation tricks, it's very easy to put data access object directly
in the view : I failed completly to see how the MVC pattern is encouraged is in all this junk
code.
Where is the consistent, unified, and secure
templating language in there ? It's a common need for every one that make websites with
web designers.
Why there isn't a default one ?
For me ASP.NET 2.0 is a good RAD tool for generating DataGridView but not to do
big real life websites with a clear MVC architecture. It's also a very bad
choice for a newbie in web development. Sure he can start to make web
apps very quickly, but he won't learn anything about HTML, JS, CSS and continue
to code in VB6 style by clicking on controls and fill all the Control_OnClick methods.
I am not sure this methodology helps the developer to produce clean reusable code.
If I have to choose for something these days,
I will clearly go for a framework with clear MVC fundations like Django
or Ruby On Rails. These frameworks use interpreted programming
languages like Python and Ruby that have advantages in term of development. It's
a lot more valuable for me that hypothetical additional performances that were never proved or even needed.
I would be very happy if someone here can explain me
what I am doing wrong with this framework and if I missed some important things that could
help me to understand.