Wednesday 25 March 2009

Setting up your Dynamic Data Site with a Domain Service Layer (UPDATED)


This article is detail how to get up and running with ASP.NET Dynamic Data 4.0 Preview 3 and Tools, in particular to be able to use the DomainService template provided in the ASP.NET Dynamic Data 4.0 Preview 3.

So download the above Unzip and Install where necessary.

See David Ebbo’s presentation at MIX09 here Microsoft ASP.NET 4.0 Data Access: Patterns for Success with Web Forms to view online or to download.

As I said in my previous post it really is worth a watch.

Using the Sample as a Template

There are two ways you may want to do this as a:

  1. ASP.NET Web Application
  2. ASP.NET Web Site

Option 1.

See David Ebbo’s article here Using a DomainService in ASP.NET and Dynamic Data where he explains most of what you need to know.

  1. Copy the Default project of your preference to the projects folder
    1. DefaultDomainServiceProject
    2. DefaultEFProject
    3. DefaultProject
  2. Rename appropriately
    1. Rename the folder to say “MyPreview Project”
    2. Rename the Project file say “MyPreview Project”
  3. Edit the project file in an XML editor, I delete the following lines (as they attempt to copy the Northwind DB and if you move the project or delete the App_Data folder you will get build errors)
  4. <Target Name="SetUpDatabase" Condition="!Exists('$(TargetAppDataPath)\NORTHWND.MDF')">
    Copy mdf file –>
    Copy SourceFiles="$(DatabaseSourcePath)\NORTHWND.MDF" DestinationFolder="$(TargetAppDataPath)" /> </Target>
    Target Name="AfterBuild"> <CallTarget Targets="SetUpDatabase" />
  5. You are now done and ready to go.
Note: These are  the correct references for the Preview assemblies.
Assembly version for Preview 3
All the Item outlined in RED should be referenced from the CommonFiles folder all the rest if the are missing you would add them the normal way.

Option 2.

Is a bit more tricky and I’ve covered this in several previous articles see here Adding Dynamic Data Previews to a New or Exiting Dynamic Data Website the main this is the name spaces you’ll just have to check each of the projects to see what they are:

Defaul Project files 
Figure 1 – The three Default Project types for Dynamic Data.

UPDATE: You cannot use a file based website with this build, only because the DomainService wizard is not available, you could do it manually of create your service in a Web Application Project and then copy etc, but I thought that was too much messing about

You can use any of the three above to start a new project, but the one I'm most interested in is the DefaultDomainServiceProject this is the one David uses in his Session at MIX09

Adding a DomainService to the DefaultDomainServiceProject

First you will need to add your data model you can use one of several.

A Scalable Pattern

Figure 2 – A Scalable Pattern (unashamedly swiped from Nikhil’s blog)

As you can see from the above diagram copied from Nikhil Kothari’s blog we can have virtually any data source and you can see from David Ebbo’s session at MIX09 where he connecto Azure services and also Nikhil’s session mentions several also. It also has a nice separation of client code from database code, so if we change the DB we only have to change the App Logic (Domain Service) and not all the dependant client code. :D

And now the adding the Domain Service

First we need to add a data model and in this case I’m going to add an Entity Framework modem instead of my usual Linq to SQL using Northwind into the Models folder.

So I’m going to add Categories Products and Suppliers as per David's demo.

Categories, Products and Suppliers

Figure 3 – Categories, Products and Suppliers

Note: Do a quick build before launching the Domain Service dialogue (this is a minor undocumented feature :)

Then right click the DomainServies folder and click Add->New Item…


Figure 4 – Adding the DomainService

Choose the Domain Service Class


Give it a name like NW I like to keep it short when I doing a demo.

 Adding the DomainService

Figure 5 – Adding the DomainService

  1. Enable client access Only required for Silverlight at the moment.
  2. Which tables from the Model to include.
  3. Whether to add full CRUD operation
  4. This generates the Metadata Partial Classes for us :D (Thanks guys this is GREAT)

Now you add the DomainService name into the Global.asax file like we did with Linq to SQL or Entity Framework and we are away.

Happy coding.


Anonymous said...

Check out the sample, will you ever be able to do a update operation with foreign key involved?

Stephen J. Naughton said...

Sorry not sure what you mean?

Steve :D

Domain services said...

Hey Steve, thank you very much for posting this, i am sure many would appreciate it.

Gynecomastia said...

It's quite a sophisticated subject, so I'm not sure if anybody understands it...

Stephen J. Naughton said...

You are correct, I will try and do a more clear version soon :)


Anonymous said...

Hi Steve

I admire your generosity in time in sharing your ideas.

In a previous web page you have said that you are looking into doing the Cascading Filters using Domain Service for Dynamic Data. I have tried unsuccessfully to convert your DD_CascadingFiltersFutures (EF) into a Domain Service Dynamic Data Project.

Could you let me know when this will be available.

Stephen J. Naughton said...

Hi I will be doing this at some point but I don't have the time to dedicate it at the moment. We all have to earn a crust :)

Hopefully I will find someone to pay me to write article and do support... :)