the jaded haven of blogness

Just another WordPress.com weblog

Archive for September 2008

Project Rosetta

leave a comment »

I was sent this link recently, a project dedicated to getting Flash developers (AS Developers) to use their existing knowledge of Flash to build next generation Silverlight application.

Something I feel worth sharing

Written by jadederic

September 10, 2008 at 5:19 am

Posted in Uncategorized

ASP.Net 3.5 SP1 – dynamic data

with one comment

I was recently asked to feedback to our development team our findings at Tech-Ed 08, so paging through my notes and the companion DVD we received, I figured it good to look at the workings behind dynamic data extensions, a new feature in the .Net 3.5 stack.

Where to start off? I remembered seeing dynamic data for the first time at a Dev4Devs session, a small community driven event hosted by Microsoft South Africa a couple of months ago. Brent Samodien briefly went into a quick “hello world” application, demo’ing how easily an inferred schema can be used to context a database and display the values back onto the page without writing, in effect, a single line of code. Wow I thought at the time but the interest slowly faded away in LINQ and writing custom providers for it. So how best to get back into this then use it as an introductory demo to the team …

Right into it. What is dynamic data extensions and why would it be useful to use?

Dynamic data is fast, dynamic model created with the focus on allowing developers to push database content into production in a quick way. Imagine having to write 50 pages that pull content for a clothing store from a database, and displaying the content so that it can be easily accessible by users and easily maintainable by a group of people. How long would you spend developing something like that, using either 3rd party controls and or building such a system from scratch? Will take some time.

With dynamic data you can build an application like this in seconds.

In this demo, we will look at how easy a new application can be generated using dynamic data extension. To follow along with this demo you need the following:

  1. Web Developer Express 2008, Visual Studio 2008 Professional
  2. ASP.Net 3.5 Service Pack 1
  3. SQL Server 2005 with SP2, SQL Server 2005 Developer Edition with SP2, SQL Server 2008 Express or SQL Server 2008 Developer Edition

I’m using Visual Studio 2008 Team System Developer Edition on my development box with SQL Server 2008 Developer Edition but everything will workin in either of the above mentioned product stack.

So let’s get started by building an empty web application.

(NOTE: I’m using the Web Application templates in Visual Studio, and not Web site. If my Solution Explorer looks different from yours please keep in mind that Web sites are handled differently from Web Applications and or that I have “Always Show Solutions” enabled)

For this demo we will be building an assemblied web application, if you wish to follow an example that uses the Dynamic Data Website Wizard, please click through to this blog.

In Visual Studio 2008, select File -> New Project and select Dynamic Data Web Application

figure 1 (New Dynamic Data Web Application)

I called my project “DynamicDataLINQ” as we won’t be looking at creating a dynamic data web application around the ADO.Net Entity Framework, which is also a new feature in the 3.5 stack.

The template should now have created all the necessary project files for us, so let’s look at them quickly:

figure 2 (project file outline)

The project structure is pretty standard, we have a web.config file, a master page, a stylesheet with a couple of styles, a global.asax file, a default.aspx page which is a content page built off the masterpage and then a directory called DynamicData which is of great interest to us. Also a couple of new assemblies have been added to this project during creation:

figure 3 (assembly references)

This example is not about these new assemblies but in a nutshell they are the driving force around the new 3.5 SP1 extensions stack and is used within both Dynamic Data and the new MVC application models. Abstractions abstracts the usual HTTPWrappers from conventional Web Forms into this new class that extends them for the new Routing model. DynamicData and DynamicData.Design hosts all the required classes for building a dynamically data-driven web application, like we are doing now. These two classes holds all the magic. Use RedGate Relector.Net to dig into these assemblies if you want to find out how they are plastered together. The Entity assembly holds reference to the ADO.Net Entity Framework, which is not covered in this demo. Extensions, Mobile and Services will also not be covered in this demo. The new System.Web.Routing assembly is the force behind the new routing model that transforms your static URLS into a new, easy to understand model, for example /{table}/{action} which we’ll see a little bit later on.

URL Rewriting is not new to ASP.Net, please don’t confuse the two. The routing model streamlines the urls in a search engine friendly manner that makes catalogue paging easier. For more information about this review the MVC framework where this is covered in greater detail.

The folder that is of interest to us is the “DynamicData“. When this folder is expanded we see four (4) folders and a web.config file. The config file is just extra configuration settings that’s applied to the “DynamicData” folder.  The first sub directory is called “Content” and hosts and “Image” folder and 2 ascx files, called “FilterUserControl.ascx” and “GridViewPager.ascx” respectively. The FilterUserControl exposes filter functionality to the dynamic data model, usually used alongside a grid control. The second, “GridViewPager” exposes the paging control at the bottom of the standard ASP.Net Grid control.

The second directory is an empty folder called “CustomPages” and will be covered in another part of my article on Dynamic Data.

The last remaining directores, “FieldTemplates” and “PageTemplates” holds relevance to the actual dynamic data model. Field templates are custom controls that infers the DB schema using standard HTML controls, for example, a BIT field in a table, will be represented by a Checkbox, a VARCHAR field will be represented by both a Label control for view and Textbox for edit. These field templates are customizable. The Page templates are the actual parts of the application that display the data from the DB (DataContext) on the page. When building your own website based on the dynamic data model, this is were you will start.

(figure 4) Dynamic Data folder

Let’s go ahead and add our database schema to this project. To keep my project clean have I gone ahead and created a new folder within this project called “DataContext“, you do not need to do this. Right-click on the project, or if you have gone ahead and created the “DataContext” folder and add a New Item to the project. On the next screen, within the treeview on the left hand side of the screen choose “Data“, note that I’m building this project in Visual C#, but all should look similar in Visual Basic.Net. Within the new option on the right hand side, choose “LINQ to SQL Classes“. For this demo I will be connecting to the Northwind database schema, which is not part of both SQL Server 2005 or SQL Server 2008. You can download SQL Server 2000 sample database from this location. I’m going ahead and I’m calling my DBML file, “Northwind” and press “Add“.

(figure 5) LINQ to SQL classes

A new window appears with to columns, the left hand column expects tables from a data store to be dragged onto the canvas. This will create the tables as classes for you that you can then reference in other parts of your project. This is exactly like other ORM tools such as CodeSmith, SubSonic and or any other available on the market. The exception here is that the ORM automatically builds up the LINQ prodiver LINQ to SQL for you so you don’t have to go and add it manually yourself. The right hand column will generate “methods” from your DB stored procedures. This is not required for this demo but if you have update, inserts and deletes that you would like to use from your DB this is where you would like to assign them.

Before we add our tables to the DBML file, open up the Northwind.designer.cs file or the *.designer.cs file for your named classes file. Notice that this file is quite empty. The class declaration, namespace and some using statements are present.

Now in order to get the tables onto the canvas, we simply open our Server Explorer window, add a new connection, if it doen not exist already to your database that you wish to map. In our demo we will be pointing our database to the Northwind database in SQL Server 2008. You will map the data store to where you have your database set up. Simply add a new connection to your database, expand the tables folder in the treeview, select the tables that you wish to map and then drag them onto the left hand canvas. The ORM tool will automatically create a class diagram of the tables, highlight the relationships and in the back-end you will now see that the file Northwind.designer.cs has been updated with all the classes, properties and methods from your DB schema.

Now that we have our Northwind database schema’d out, we only need to tell the web application where to get the data, and how to manipulate it.

To do this, we need to open up the Global.asax.cs file, which is the code behind file for the Global.asax file. So go again and open this file. You will notice, that unlike the normal Global.asax file only the Application_Start method is available, with a new public static method called RegisterRoutes() method that accepts a RouteCollection object as parameter. This method is then called whenever the application starts up.

So lets look at the RegisterRoutes() method. First a new MetaModel is instantiated. The MetaModel reads the metadata from the DB you just added to this project. Next is some important notice. It says that you should only allow scaffolding of the data if you so which to support templating. For this demo we do need to scaffold the database due to the various relationships in the DB. The template will then automatically target, for instance, a Product to a Catalogue, a Catalogue to a Shipping Address and so forth. So go ahead and uncomment the line that reads:

model.RegisterContext(typeof(YourDataContextType), new ContextConfiguration() { ScaffoldAllTables = false});

and change the part that reads “YourDataContextType” to read “DataContext.NorthwindDataContext” or whatever you decided to name the DBML. If you cannot find your data context file and you created the “DataContext” folder, you will need to add the DataContext namespace infront of the NorthwindDataContext object. If you are not sure what you called your data context class, open up the *.designer.cs file for your DBML file, the name you want is the main class (first class) within that file.

Now set ScaffoldAllTable = true and save the file.

That is it. You have now successfully, if you followed instructions correctly, created your first Dynamic Data web application using the new data-driven model from Microsoft.

Run the application and see how everything has now been compiled for you and all you had to do was create a schema inferred from the DB and changed a single line in the Global.asax file.

Part II will feature a more customized review of the Dynamic Data model, how to incorporate 3rd party controls and get them to leverage this model.

Written by jadederic

September 4, 2008 at 9:39 am

Posted in Devlings

Samsung Omnia South Africa

leave a comment »

Our team is proud to announce the launch of the Samsung Omnia website.

http://www.samsungmobile.co.za/omnia/

Written by jadederic

September 4, 2008 at 9:33 am

Posted in Uncategorized