Showing posts with label Preview. Show all posts
Showing posts with label Preview. Show all posts

Wednesday, 3 June 2009

A Great Buried Sample in Dynamic Data Preview 4 – Dynamic Data Futures

I just thought I’d mention this find ad David Ebbo mentioned overriding MetaTable to get at the table.GetScaffoldColumns() method, and then I found the Dynamic Data Futures sample inside of Preview 4 (which had a billing of being the Futures sample updated to work with Preview 4) and in side the it I found the CustomMetaClasses folder:

The CustomMetaClasses folder

Figure 1 – The CustomMetaClasses folder

This of course give you the sample you need to do this your self my plan is to use this sample to make a MetaTable Class that I can pass in a Lambda function to do all my column generation without ever having to add IAutoFieldGenerator to the page (which in .Net 4.0 will not work on the Details, Edit and Insert pages as they are to be based on FormView to accomodate EntityTemplates).

public class Global : System.Web.HttpApplication
{
    private static MetaModel s_defaultModel = new CustomMetaModel();
    public static MetaModel DefaultModel
    {
        get
        {
            return s_defaultModel;
        }
    }

    public static void RegisterRoutes(RouteCollection routes)
    {
        DefaultModel.RegisterContext(typeof(NorthwindDataContext), new ContextConfiguration()
        {
            ScaffoldAllTables = true,
            MetadataProviderFactory = (type => new InMemoryMetadataTypeDescriptionProvider(type,
new AssociatedMetadataTypeTypeDescriptionProvider(type))) });

Listing 1 – Adding a DefaultModel based on CustomMetaModel from the CustomMetaClasses folder

Listing 1 shows the addition of the CustomMetaModel to the Global.asax allowing you to override the Meta Classes in one place this is cool.

Thanks again ASP.Net Team HappyWizard

Wednesday, 25 March 2009

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

Requirements

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" />
    </
    Target>
  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…

ScreenShot264

Figure 4 – Adding the DomainService

Choose the Domain Service Class

ScreenShot265

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.

Monday, 16 March 2009

Adding Dynamic Data Previews to a New or Exiting Dynamic Data Website

What I want to do here is create a template website I can copy and reuse to build web sites based on the Preview. Hope this is of some help.

Create a new Dynamic Data Web Site:

ScreenShot248

Once created empty the DynamicData folder (keeping the folder) remove all sub folders.

Copy the contents of the following folders to the Dynamic Data Folder for:

Linq to SQL Web Sites

Linq to SQL folders

Entity Framework Web Sites

Entity Framework folders

Once you’ve copied the files

Copied folders

It’s time to do some editing of the files, we’ll use the Ctrl+Shift+H to do a search and replace of files in folders.

Setting up Look in folder

In the Find and Replace dialogue click the ellipsis (…) button at the end of the Look in: drop down list.

  1. Find the root folder of your website.
  2. Select the DynamicData folder in your site.
  3. Add to the Selected folders.

Find and Replace dialogue

Using the Search Patterns list below fill in the Find and Replace dialogue box:

  1. Search for pattern.
  2. Replace with pattern.
  3. Use regular expression.
  4. Enter the file type to search in.

Search patterns:

step Search for: Replace with: Use Regular expressions file types
1. namespace DynamicData(EF)*Project \{\n   yes *.ascx.cs
2. \}\n\} \} yes *.ascx.cs
3. Inherits="DynamicData(EF)*Project.   Yes *.ascx
4. CodeBehind=" CodeFile=" No *.ascx

In step 1 & 3 you may encounter any of the following name spaces to match and remove:

  1. DynamicDataProject.DynamicData.Filters {
  2. DynamicDataProject.DynamicData.EntityTemplates {
  3. DynamicDataProject.DynamicDataL2S.EntityTemplates {
  4. DynamicDataProject {
  5. DynamicDataEFProject {

I would attempt to remove them in the order shown above (4 & 5 are covered by the pattern namespace DynamicData(EF)*Project \{\n

 Finally Add a Reference to the Preview DLL’s

  1. Right click the root of the website, and choose “Add Reference…”
    Add reference to web site
  2. In the “Add Reference” dialogue browse to the Common Files folder in the location where you unzipped the Preview. Then select all the dll’s and click OK to add a reference to them in your web site.
    Add reference dialogue

Finally copy the:

File Copy to Folder
AutocompleteFilter.asmx.cs App_Code folder (create if you have not already).
AutocompleteFilter.asmx ~/ (root of website)
AjaxToolkitFixes.css ~/ (root of website)
AutocompleteStyle.css ~/ (root of website)
Site.css ~/ (root of website)
Site.master ~/ (root of website)
Site.master.cs ~/ (root of website)
web.config ~/ (root of website)

 

Similar editing is now required of these files see the Search Patterns above.

Edit the web.config and remove the contents of the connection strings node in the web.config

remove connection strings from web.config

Finally tweak the GridView  on the Default.aspx page.

<asp:GridView ID="Menu1" 
    runat="server" 
    AutoGenerateColumns="false" 
    CssClass="DDGridView"
    RowStyle-CssClass="td" 
    HeaderStyle-CssClass="th" 
    CellPadding="6">

See the BOLD ITALIC text this is to take advantage of the friendlier CSS now supplied with the Preview it won’t stamp on other CSS you may want to use in your web site as the CSS in the .Net 3.5 SP1 version does.

Now you have a sample that you can use over an over like a template

Your next step is the same as if you had just done file new “Dynamic Data Web Site” but I would copy to a new folder first so I can use this as a template each time I want a Preview DD website.

Happy coding

I’m a an MVP and I’m a PC