Imagine the situation - your boss invades in your personal space while you are just surfing the Web or programming (but your own cool stuff, not your employer's...) and says: "We need a new Web based Intranet app for this new database for the (..) department. We need it in 2 hours ... or ..." (he starts to blackmail you).
This article might give you an idea of how to present him a cool demo to justify your web surfing and own stuff programming ... or seriously spoken, provides you a demo showing the power of the SubSonic library, providing an easy and efficient interface to a database.
This article is intended for intermediate and advanced .NET Web developers with a basic knowledge of the ASP.NET technology. You might have difficulties understanding (hopefully not running) the herewith described concepts if you are just beginning with C# ASP.NET development. You probably would need Visual Studio 2005 / 2008 and SQL Server 2005 / 2008; or, if you are using another IDE for C# centric ASP.NET development, probably, you know better than me what you are doing. If you are completely new to SubSonic, you might want to check the Creating a DAL with SubSonic tutorial by Michael Ceranski and return later on here.
Before the era of ORMs or properly separated DALs, developers had to write SQL queries for each particular type of data request from a database.
The latest advances in Object Relational Mapping do provide developers an additional abstraction on top of which building of software is faster, less error prone, and even easier. This article is aimed to justify this statement by providing a real working demonstration. Simply said, in one ASPX page, a drop down is populated with the number of tables in a database (Northwind is used herewith), and by changing the table name from the database, we could see the contents of this table, and browse through them by clicking the next and previous buttons provided by the SubSonic dedicated control, as hopefully could be seen from the image:
Using the code
Download the source code from the link to a folder of your choice (I use the D:\temp folder in the examples). It contains all the needed code. Open Visual Studio and select the Default.aspx page. Run the project by pressing F5. Follow the instructions from the page.
Read the comments from the code-behind of the NorthwindTableViewer.aspx page. Each row in the code has comments. I can't figure out a better way of explaining what the code does - read the comments, set debugging points, and play with the demo.
After having run the code, return to this article. If you are reading this without having run the code, you probably will miss the whole thing and want somehow to waste your time ...
The web site project has the configuration in the Web.config - the needed configuration could be simply copy pasted by searching for the "SubSonic" string - you should understand the configuration for the database connection etc., and know how to change those for your database; otherwise, obviously the requirements presented above will not be met.
For the SubSonic specific configuration, start studying the SubSonic documentation - this is the wrong place to reproduce it.
Obviously, for any database, you would need additional security, so there should be some kind of logic in the population of the dropdown or whatever control you choose to populate the set of tables from the database
Practice by replacing the Northwind database with some of your databases (chances are that you would need to remove SubSonic.dll, after changing the Web.Config, and read it later). You might get some compile errors - a strong smell for bad database design (missing Primary Keys, Foreign keys etc.).
How it is achieved
The SubSonic library is utilized to provide us with an additional abstraction layer. In this demo, some of the basic interfaces provided by SubSonic are utilized, as well as two of the custom controls of the project. The power of SubSonic could be realized after reconfiguring this demo to work with another database - there is nothing hardwired except the database name with its corresponding configuration in the Web.config file. The concept of using dynamic controls is also utilized. Yuriy Solodkyy has a great article on the subject - you should run all his examples before truly understanding how dynamic controls work in ASP.NET, if you are interested in it ... I have to admit that currently I just use a template class containing the basic overridden methods of a
System.Web.UI.Page object and follow simply the rules presented in the article. I have followed those rules the last two years, and whenever something goes broken, it always has been me, not following the rules ... so, looking at the mirror has given me a hint for where the bug is ...
This demo idea could be developed with the following actions in mind:
- Create a dropdown for all the databases to which this project has been configured to
- Add other DBMSs - Oracle , MySQL
- Obviously, some dynamic providers retrieval; I have seen some questions on the StackOverFlow site
- If someone gets excited about this type of demoing, may be creating an Open Source project could be a good idea ...
Comments and feedback are welcome
Let me know if you get some errors in the code. If the errors are due to the fact that my environment is "somewhat special", it would be fare for future readers to correct those errors. There is a small bug of not always setting the correct page size of the grid control; however, I assume that for demo purposes, it should be forgiven ... Anyone figuring out the answer for the bug ... I offer a beer (PayPal is a good friend of mine
- Vote - the vote reflects your opinion on the article's content.
- Comment - it is studied that it is 5 times more probable to provide negative feedback than a positive one, but even if your feedback is not positive, please share with us the reason for that.
- Collaborate - do you have a cool idea related to the subject of the article? Contact me, or simply share it with all the readers ...
- Improve - do you think this demo can be improved? If you have an existing improvement or better suggestions, please share those with us.