Share via


Using a DomainService in ASP.NET and Dynamic Data

Update (8/26/2010): I updated some of the content to reflect the new way to get the bits.

 

One of the big things that I discussed in my MIX 2009 talk is the new DomainDataSource control.  It is available as part of the WCF RIA Services Toolkit.  To install it, you’ll first need to install the core RIA Services.  In addition to the runtime, this will give you some useful tooling to get started.

So basically, there is this thing called a DomainService, which is essentially a Business Layer class that exposes CRUD methods.  Once you have a DomainService, you can use it in various scenarios, including ASP.NET apps and Silverlight apps.  Here, we will focus on its use from ASP.NET. 

Important: the DomainDataSource discussed here is the server side ASP.NET WebForms control.  There is also a Silverlight client control by the same name (also used in RIA services).  And to add to the confusion, Nikhil has a blog post which discusses a sample ASP.NET control also named DomainDataSource.  But it is not the same as we are discussing here.  Yes, I realize that this is pretty confusing.  Just trying my best to clear it up!

The rest of this blog was written for earlier preview bits, and some of the details have changed.  For a walk through based on more recent bits, check out this post by Pranav Rastogi.

 

 

Creating a DomainService

DefaultDomainServiceProject comes with a default DomainService, but to make things more interesting you should just delete it and recreate one from scratch.  Just delete DomainServices\NorthwindEntitiesDomainService.cs.

Now right click on DomainServices, choose Add New Item, and pick Domain Service Class.  Name it for instance Catalog.

image

You’ll see a New Domain Service dialog.  In here, do the following:

  • Uncheck Enable Client Access. That’s only useful for the Silverlight scenario.
  • Check Categories and Products.  For Products, also click the right check box to enable editing.
  • Check ‘Generate associated classes’ at the bottom. This will be useful for Dynamic Data.

image

You should end up with a class that looks like this:

 public class Catalog : LinqToEntitiesDomainService<NorthwindEntities> {

    public IQueryable<Categories> GetCategories() {
        return this.Context.Categories;
    }

    public IQueryable<Products> GetProducts() {
        return this.Context.Products;
    }

    public void InsertProducts(Products products) {
        this.Context.AddToProducts(products);
    }

    public void UpdateProducts(Products currentProducts, Products originalProducts) {
        this.Context.AttachAsModified(currentProducts, originalProducts);
    }

    public void DeleteProducts(Products products) {
        if ((products.EntityState == EntityState.Detached)) {
            this.Context.Attach(products);
        }
        this.Context.DeleteObject(products);
    }

Basically, it’s just a class for a few CRUD methods that you want to expose.  In this case, it’s working over an Entity Framework model (hence the base class), but it would look similar with Linq to SQL or other ORM technologies.

The important part is that all data access goes through those methods, so you have full control over it.  This is quite different from using LinqDataSource/EntityDataSource, where they talk directly to the DAL without going through your code.  You have to write a little code, but the extra control you get makes it well worth it!

 

Registering your DomainService with Dynamic Data

Before we get into writing ‘standard’ ASP.NET pages by hand (in future posts), we’ll use Dynamic Data to get started quickly.  To do this, simply add this line to global.asax (replacing the similar line that’s already there):

 DefaultModel.RegisterContext(
    new DomainModelProvider(typeof(Catalog)),
    new ContextConfiguration() { ScaffoldAllTables = true });

 

Running it

Now you can just Ctrl-F5 and you should get a working Dynamic Data app.  Note how it only lets you do things for which you have CRUD methods.  e.g.  you can edit Products but not Categories.

To make things more interesting, try various things:

  • Debug the app and set break points in your CRUD methods to see them getting called.
  • Change GetProduct() to only return a subset of the products, and watch it affect the UI
  • Change UpdateProduct() to modify the product before saving it,
  • Add some Dynamic Data style metadata in DomainServices\Catalog.metadata.cs.  e.g. add a [Range(0, 100)] attribute to some integer field, and try an edit that violates the range

 

Conclusion

This was really just a quick intro to using DomainService in ASP.NET and Dynamic Data.  I’ll try to follow up with some posts that go into more details on how to use DomainDataSource directly in a page without involving any Dynamic Data.  To reiterate, though Dynamic Data works great with DomainDataSource, DomainDataSource completely stands on its own without Dynamic Data (as was shown in my MIX talk).

Comments

  • Anonymous
    March 24, 2009
    One of the big things that I discussed in my MIX talk is the new DomainDataSource control.&#160; It is

  • Anonymous
    March 25, 2009
    DotNetBurner.com - news and articles about .net DotNetBurner

  • Anonymous
    March 25, 2009
    Thank you for submitting this cool story - Trackback from DotNetShoutout

  • Anonymous
    March 25, 2009
    When trying to install the RIAServices it insists I install all the Silverlight 3.0 Beta stuff first. Is there a way I can get the DomainService without installing Silverlight 3 beta?

  • Anonymous
    March 26, 2009
    Dave, currently if you want the dialog that helps create the DomainService class, you do need to install the RIA package, and it comes with SL3.  However, noe that:

  • You can simply uninstall SL3 after installing the RIA package if you want.
  • You don't have to use the dialog and can just create the DomainService by hand. We'll clean this up eventually so you don't have to install SL pieces to get this working.
  • Anonymous
    March 31, 2009
    Thanks for your post on this David as I was really excited by your Mix talk. Just wondering: How do the RIA generated Metadata classes relate to the partials one normally creates in a dynamic data app, an example of which is in the Models/NorthwindEF.partials.cs file of the sample? Thanks for your work on Dynamic Data, its a really cool product.

  • Anonymous
    March 31, 2009
    Rols, they really are exactly the same thing. Basically, the Domain Service wizard generates the metadata class to get you quickly started without having to create it from scratch. But it is no different from the partial classes that Dynamic Data has always used. David

  • Anonymous
    April 05, 2009
    I do not have VS2008 but VWD Express 2009 SP1. I cannot see the Domain Service Class when i try to add new item. However i did not install the RIA service yet.

  • Anonymous
    April 06, 2009
    Ignore my previous comment, i installed the RIA services, now everything works like a charm.

  • Anonymous
    April 13, 2009
    A few more great posts on .NET RIA Services over the last week.. Christoph had a great post on Customize

  • Anonymous
    April 14, 2009
    Link Round up on .NET RIA Services

  • Anonymous
    May 05, 2009
    Why are these called "DomainServices"?  Do they have anything to do with a domain model or domain driven design?  I think you should change your wording to avoid confusion among developers.

  • Anonymous
    June 19, 2009
    Afewmoregreatpostson.NETRIAServicesoverthelastweek.. Christophhadagreatposton

  • Anonymous
    March 28, 2011
    The comment has been removed

  • Anonymous
    March 26, 2013
    Can I add a view definition to a existing domain service class