In Depth: novella’s Architecture
So novella has been out for a week or so, and so far the reaction has been quite positive (yea!). A few of the people who are getting their hands dirty have been asking me questions about the architecture and how everything works.
So to help them and the others who are too shy to ask, here is a detailed look at the various layers that make novella tick. Before I begin, I should note that if you just want to use novella, you don’t need to know any of this. However, if you are taking things apart or you just like to know what does what, this might help.
Also, if you are a relatively new ASP.Net developer who doesn’t care at all about novella, this architecture is useful for building all sorts of sites, and is a good learning tool. I have used this architecture in one form or another on all the sites and desktop applications (with some tweaking) that I have built in the last year or so.
3 Layer Bean Dip
The architecture behind novella is basically your standard 3-tier. If you are into design patterns, then you may be interested to know that the architecture is a mix of the Command and Facade pattern. If what I just said doesn’t make a lick of sense, no worries, it will all click in a bit.
Lets take a deeper look at each level.
First Floor: UI
The UI Layer is definitely the biggest, but it actually has the “easiest” job. It performs 3 functions.
- Data Collection
- Data Validation
- UI Maintenance
The first function is Data Collection. This is exactly what it sounds like…capturing the input from the textboxes, dropdowns, and other controls. Next we have Data Validation. The purpose of this is to make sure that none of the data that gets back to the server is funky…i.e. a valid email address, or making sure that every block has a name. Finally, there is UI Maintenance. In the world of an ajax webpage, this is definitely where most of the time is spent. UI Maintenance is changing the interface based on what the user is doing. For instance, when you update a block a little blob fades in and tells the user that the update was successful. This would fall under the category of UI Maintenance.
So let start an example…Creating a new block.
On the page CreateBlock.aspx you can see all the controls that are needed to create a new block: a textbox for the name, and a textarea for the actual markup. When the user is done they click the “Save Block” button. This kicks off the data collection piece. In this case the system calls a javascript method ManualCreateBlock().
From here you can see that the app is collecting the data (i.e. name and markup), then checking to see that it is valid (i.e. making sure the block has a name and that it isn’t a duplicate) and then passing it back to the server. In the AddBlock method on the server all the data is packed up into a nice object and given to the Command.
Once the save is complete we do a little UI maintenance and tell the user that the save was successful.
Now it is time to go behind the curtain.
Second Floor: Command
The Command’s job is to take the data from the UI and prepare it to be stored. In novella’s case the data is prepared to go into the database, but in other applications I have used the command layer to put the data into Word documents, XML, etc. Once the data is processed, the command makes sure that it is ready for the UI to use. Most of the time this isn’t necessary (the data is already ready), but if you are doing stuff with serialization and the like, then the command might get a lot busier. In the case of novella, the command is quick and easy.
Lets continue with the example I started earlier.
As part of the UI layer the system bundled up all the data into a nice object called UIData.BlockData. This is a custom object I made that mirrors all the fields that make up a block in the database. The UI calls a method UICommand.AddBlock(). This method takes the object I need as a parameter and gets all the information ready to move to the database. In this case I build up a list of parameters to be used in the “dbsp_AddBlock” stored procedure.
Once the data is loaded into the parameter list its time to call the framework.
A little note about stored procs…
A lot of developers use inline sql when developing websites. In a lot of languages (i.e. PHP) you don’t really have a choice, but with ASP.NET you do, and I would highly recommend using stored procedures over inline sql. Why? Because it is a lot more efficient. With inline sql the database has to compile the statement before it can be run, while a stored proc is already compiled and ready to go. With small sites this isn’t a big deal, but if you are building a site with any sort of scalability in mind, its worth the extra bit of hassle to use stored procedures.
</soapbox>
Third Floor: The Framework
Here is where the real work happens (as a sidenote…if you want to make your life a whole lot easier when starting a new ASP.NET website with a SQL Server backend, copy the code UIFramework first thing…there is just a ton of useful code here).
The framework’s job is to connect to the database, run the stored procedure, get the data back into an object (when calling a select query) and give it back to the command.
To finish the example, the command called a method RunInsertQuery and passed us the name of the stored proc and a list of parameters. The framework takes this data, opens up a Sql connection, passes along the parameters, and executes it. Next, the system returns an output parameter so the UI can know which block was just added. Finally, the connections are cleaned up so the database doesn’t hold it open any longer than it has to.
So there you have it…the novella framework. Its pretty simple, flexible, and outside of novella works well with all sorts of different types of sites. The only thing this version is missing is transaction support. I have that built into some of the other frameworks that I have built, but novella didn’t need it so I figured I would slim it down a bit.
I hope this in depth look at the various layers is helpful as you take novella and mold it to your own purposes, or even just as a learning tool. If you run into any problems, shoot me an email at david at creativeui dot com and let me know how I can help.


Koen said,
Wrote on January 25, 2008 @ 8:00 am
About the sidebar: there really is no difference in performance between sprocs and inline SQL. Both are cached and compiled by SQL server. There has been quite a lot of discussion about this topic… See for instance http://weblogs.asp.net/fbouma/archive/2003/11/18/38178.aspx and all the follow ups.
David Baxter said,
Wrote on January 25, 2008 @ 10:33 am
Hey Koen, I read through the points in the article. Some make sense, some don’t.
I can tell from reading that post and others that this is a sticky subject for some so I don’t want to fan the flames so I will state why I prefer stored procs, and let others come to their own conclusions.
1. Speed. Although the differences may be slight in today’s world, there is a difference. While inline sql is cached (I was wrong on that apparently) there are still efficiencies with being directly in the database.
2. You can optimize/test the sql within the database itself. This is the biggest one for me. I can’t tell you how much time that saves when you are dealing with complex queries.
3. Separation of layers. Now, because of my history as a developer, I may be more structured than a lot of other web guys out there, but I am a HUGE fan of multiple tier architecture. Why? Because it simplifies the duties of the code. By having your queries in the database, it spreads the load as it were. This make maintenance a lot easier.
4. Speaking of maintenance…stored procs are a lot easier to maintain. Why? Because if you need to change something you don’t have to change the code and redeploy. If you have ever worked with desktop applications you know how big of a deal deploying to all your users can be. On the web that isn’t as big of a deal, but it is still easier to change a stored proc than it is to deploy the code again.
5. Inline sql can lead to security errors much more easily than stored procs. If you are a solid developer, then you know ways to make inline sql secure. However, in general it is easier to make security mistakes with inline sql than it is with stored procs. Why take the risks?
So that about sums up my position. You may agree or disagree, but there you have it. Bottom line is this. If you are building a site for a landscape company, then it really doesn’t matter. However, if you are building a complex web application, then, in my opinion, it is worth the little bit of extra effort to employ stored procs.
David
Scott Topiol said,
Wrote on January 30, 2008 @ 1:42 pm
I agree with you David. I’ve been in the web development and SQL Server business for a long time and I also strongly prefer the use of stored procedures. Inline SQL is a bad idea for many reasons aside from performance, which I think hit on very well in your response.
Of course, the debate seems to be filled with as much as emotion as Ford vs. Chevy or Mac vs. PC. Nevertheless, I’m glad you chose that architecture instead of cluttering up your code with a lot of database logic.
David Baxter said,
Wrote on January 30, 2008 @ 1:52 pm
Thanks Scott…ya know when I wrote my sidebar I had no idea how sensitive this topic is. It was only when I researched my response that I saw how touchy people get.
I guess it comes down to human nature. If you choose a solution or product, you do not want to be told that it is crap because you are going to feel like you made a bad decision. The bigger the decision, the more you are going to fight that it was the right one. PC vs. Mac…PHP vs. .NET…Stored Procs vs. Inline.
In the end, as you mentioned, novella’s architecture and code is much cleaner because of the stored procs. Since novella was always planned to be open source and available to “the masses”, I believe that this was the correct choice.
David