I recently discovered Microsoft ASP.NET Dynamic Data(or ADD for short). I know, some would say I'm slow off the mark, but I never have time to keep up with all the new technologies MS are putting out. Anyway, this one looked interesting, so I decided to devote some time to investigating it. I must say I was (to quote the vernacular), somewhat gob-smacked by it! One of the reasons it took me so long to get around to looking at this was that MS seem to have forgotten to promote it. Even the Dynamic Data section of the www.asp.net web site (when you can find it) doesn't really tell you what it's about. One look at that would be enough to make me yawn and go somewhere else.
The other problem is that when you do finally find some info about it, it's woefully out of date and incomplete. I even saw a comment from a Microsoft employee on one of the MS blogs saying that all the code samples for ADD on Codeplex (which is where MS are depositing a lot of their samples nowadays) should be ignored as they are out of date! Ho hum, not exactly encouraging is it? The links on the www.asp.net page are all for old, superceded version of ADD, and don't take account of the fact that it's part of .NET 4.0, so you don't need any previews, CTFs or what not.
Anyway, if you've written any reasonable sized database-driven web sites, then you'll know that the painful bit is writing a whole pile of administration pages so that the site owner can maintain the site. You end up doing almost exactly the same thing over and over again. When you've done it for the 97th time, you really wonder why there isn't a better way.
Well, now there is - maybe. Enter ASP.NET Dynamic Data. Now you can have a fully-featured CRUD web site in seconds (no, not an exaggeration).
Before getting into any details, here's a quick how-to. You need to have Visual Studio 2010 (or 2008 with various updates and add-ons) to follow this, but it's worth it...
- Create a new project, and choose an ASP.NET Dynamic Data Entities Web Application (or a Domain Service Web Application if you prefer to work with RIA).
- When the new project opens, you will see the Global.asax file in the editor. Ignore this for a moment, we'll come back to it. Add an ADO.NET Entity Data Model to the project (under the Data section on the new item dialog), and follow through the usual steps to create an entity model. Make sure to take a note of what you called the model as you'll need that in a moment.
- When the model is in, go back to Global.asax and look for the commented line (it's line 25 in mine) that starts "DefaultModel.RegisterContext"
- Uncomment the line and change the bit that says "YourDataContextType" to the name of the model you just created, and change the "false" at the end of the line to true.
- Hit f5 to run the application. You'll need to click OK on the pop-up window that asks if you want to modify Web.config to enable debugging.
At this stage, you should see a list of your tables. Click one of the tables, and you will see a grid of the ten or so records. Note that you can page and sort this grid.
See the three links on the left of each line? They are cunningly named "Edit," "Delete" and "Details." Guess what? Clicking them allows you to edit, delete or view details of the record. See the "Insert new item" link at the bottom of the grid? Guess what that does? No, I'm not going to tell you, try it and see for yourself.
If your table has foreign key references, you will see some drop-down controls above the grid that you can use to filter the data.
Now if you aren't jumping up and down in your seat by now, wondering how you can configure the heck out of this, then you obviously haven't spent enough time writing boring admin page after boring admin page! I couldn't believe it when I saw this first time. For almost no work at all, you have a fully working CRUD web site.
If you want to know more about ADD, join the club! The documentation is pitiful, and the ADD forum on www.asp.net is not very busy. There are a lot more people asking question than answering them. Kudos to David Ebb and Steve Naughton (who lives about 20 miles west of me, and is only 33 days younger!) who are doing a great job of answering questions. They both have blogs in which they reveal all sorts of clever things. I must admit that Steve's blog in particular has some stuff that's way above me head, but looks very clever.
Anyway, I intend to post more about ADD soon. However, I do want to add a few caveats before going any further. Whilst ADD is truly impressive, you need to see the full picture before getting too excited. In particular, the following points became clear to me over the past couple of weeks...
- The documentation is lousy, so you'll spend a lot of time searching, trying to find out why things don't work as you expect, or how to do something. Unfortunately, you won't find a huge amount of answers. Not many people seem to know much about ADD, and a lot of what is out there refers to old versions of ADD, which are out of date.
- ADD sites are slow! This is an important point, as it's all very well being able to create the admin stuff easily, but if it runs slowly, the users won't be happy. As far as I can see, ADD uses reflection to work out what should be displayed on the page, and as each page is built dynamically from templates, the overall effect is a site that's about as slow as an asthmatic ant with a load of heavy shopping.
- By the time you've customised the site, you are left wondering if it would have been any quicker just writing the whole thing from scratch. Obviously, this depends on how many tables your database has. If you are working with a huge database, then ADD is still going to be a whole lot faster, especially as you can apply customisations across the board very easily.
- There is no sanity checking on the delete links. This one is pretty serious actually. When you look at a table in the grid, every record has a delete link, even though there may be foreign key dependencies that would not allow you to delete the record. What's even worse (and this is pretty unforgivable MS), if you click the link, you get a really ugly unhandled exception thrown. I can understand why they didn't include checking the tables to see if the record could be deleted, as this could involve a lot of database hits for every record, but leaving an unhandled exception is pretty bad.
- If you use a RIA service as your data context, then the table's primary key ID will be shown. This is a bug that I discovered (I am proud), and was confirmed by David Ebb (see his post of 29 Aug 2010, 7:21 PM in this forum thread). It is easily fixed, but it bothered me that such an obvious issue was still present in a production release.
It sounds like I'm criticising ADD, but I'm not. It's just that it still has a lot of issues, and you need to be aware of them before investing too much time in it. What would be an ideal halfway house would be if MS could produce a tool to create these sort sof admin pages, but create ones directly related to the tables. Dynamically built ones are fine if your database structure changes regularly, but let's be honest, how often does that happen in production?
Anyway, until that happens, here are some of the useful links I found for ADD, roughly in order of how useful I found them...
- WROX e-book on ADD - this was an excellent find. Although it's over a year old (a long time in ADD terms), this e-book is one of the best places to start, and at a mere $6.99 isn't going to break anyone's bank. Well worth every penny. The author has a blog, but apart from being about loads of other subjects as well, it's also ancient, the last post on ADD being the one that announced this e-book.
- www.asp.net ADD section - as mentioned above, mainly out of date, but there are some videos to get you going. These are definitely worth watching, although they will leave you with a lot of questions. Make sure to scroll down to the section on ADD, and ignore the previous section on the preview version.
- The ADD forum on www.asp.net - the place to ask questions, and wonder why no-one answers them! When David and Steve have time, they do a brilliant job of helping out, but I guess they have other things to do with their time as well!
- Blogs by David Ebb and Steve Naughton - the two most helpful people in the forum. Their blogs contain some advanced stuff, and there's a lot to go through, but they really know what they're talking about.
- MSDN ADD content map - as with many MSDN pages, it's fine if you want to look something up, but no good for learning. There are a few walkthroughs, but not enough.
- ADD blog - not been updated in about nine months, which gives some idea of how much effort MS are putting into marketing ADD! Some useful posts though.
OK, that's far more than I intended writing about ADD in this post. I have loads more to write, mainly explaining some of the problems I encountered, and how I got around them (if I did). Those will have to wait for another day.