The Elephant and the Silverlight

Thoughts and analysis of Silverlight for business applications

Page List

    How to setup your DomainService using partial classes for easy maintenance

    One of the common questions that gets asked in the RIA Services Forum is how to add additional tables to a DomainService. One of the best practices that has been developed by the RIA Services community is using partial classes to spread a DomainService across multiple files with one Entity in each partial class file. Here are the steps on how to do this.

    I am starting with Shawn Wildermuth’s MVVM project since he has multiple tables. Here is what the EDMX looks like:

    Model

    First you need to add a new DomainService

    AddNewItemOne

    I am using a naming standard here of EntityName.DomainServiceName

    After adding the DomainService the wizard will appear

    OriginalAddNew

    One of the nice features of the wizard is that you can change the name of the DomainService. We are going to take advantage of that to remove the entity name. Here is what the wizard looks like once we have completely filled it in. i have outlines what changed in red.

    ChangedAddNew

    After pushing OK this is what the project will look like

    ProjectResult

    Finally we need to modify the DomainService to add a partial to the class definition

    CodeChangeOne

    Repeat this process for any additional entities you want in your domain service. The only difference is that you will also need to remove the [EnableClientAccess()] attribute from all other partial class files

    CodeChangeTwo

    Setting up your project using partial class makes it easy to add additional tables. If you want to regenerate an entity due to database changes then you will need to comment out the existing code and recompile first. The wizard will not regenerate metadata that already exists in the project.


    Permalink | Comments (12) | Post RSSRSS comment feed

    Comments

    Tony United States

    Tuesday, August 25, 2009 2:51 PM

    Tony

    Excellent article! I just have one question. When you say in the last paragraph, "If you want to regenerate an entity due to database changes then you will need to comment out the existing code and recompile first." What existing code must be commented out? I think an example of actually regenerating an entity because of database changes would be great!

    Thanks.

    ColinBlair United States

    Tuesday, August 25, 2009 3:07 PM

    ColinBlair

    @Tony: You will need to comment out the full contents of the DomainService and Metadata files for the entity that you are regenerating. I would recommend that you also change the file names so that the replacement files can use the original file names. After you recompile (the wizard is looking in the compiled .PRC files for existing metadata, that is why you need to recompile) you can then follow the above steps as if you were generating the entity for the first time. You will have to manually merge any code from the original files into your new files.

    If you don't need any of the code from the original files you can just delete them, recompile, and create the new files.

    MarcG Australia

    Thursday, November 12, 2009 11:29 PM

    MarcG

    Hi Colin,

    Great Article. I'm a little new to this so please forgive me if this is a silly question.

    In your example you add a Supplier entity.  Now if I wanted to add other Entity Say "Game" from you above example. Do I need to go through the process of adding a Domain service with the same name as the existing one? Then choose game from the wizard?

    cheers.

    ColinBlair United States

    Friday, November 13, 2009 7:15 AM

    ColinBlair

    @MarcG Yes, follow the instructions at the end where I show how you then modify the generated DomainService file to set the partial correctly.

    Tooraj Canada

    Tuesday, December 01, 2009 10:12 AM

    Tooraj

    Hi Colin,
    Shall I do the same thing if I use Ria 4?
    Thanks.

    ColinBlair United States

    Tuesday, December 01, 2009 10:53 AM

    ColinBlair

    @Tooraj Yes, this works exactly the same way with WCF RIA Services.

    Tooraj Canada

    Tuesday, December 01, 2009 12:04 PM

    Tooraj

    Thanks Colin, I see you put the domain services in the MVVM.Data project not the web project. If I do the same thing the code generator still works? It didn't work when I tried to put all entities in the same domainservice cs file. The Checkbox (to generate classes) on the domain service wizard was disabled.

    ColinBlair United States

    Tuesday, December 01, 2009 7:13 PM

    ColinBlair

    Did you have the EDMX in the same project and did you make sure to compile first? The client and server class project together are knowns as a WCF RIA Services Class Library and there is a project type for that in Visual Studio.

    Tooraj Canada

    Friday, December 11, 2009 12:44 PM

    Tooraj

    Thanks, it is working.

    Jose Spain

    Saturday, December 26, 2009 4:35 PM

    Jose

    Hi Colin, what could be the problem after creating a simple application with your solution? because I receive this exception "AG_E_PARSER_BAD_PROPERTY_VALUE [Line: 25 Position: 50]" in the line "<ds:QuasarDomainContext />".
    The exception start at "InitializeComponent();" call.

    Any idea?
    Thanks in advanced
    Jose

    ColinBlair United States

    Sunday, December 27, 2009 8:15 AM

    ColinBlair

    @Jose Which particular solution are you referring to? Using partial classes shouldn't have any effect client side. If you are referring to the WCF RIA Services Class Library I was discussing with Tooraj, it might be that you don't have your namespaces setup correctly. Is the ds: namespace referring to your class library?

    Jose Spain

    Sunday, December 27, 2009 1:15 PM

    Jose

    Hi Collin, thanks, I'm referring to the use of partial classes. I'm using a simple application "SL Business application" with 2 standard projects (SL client, and Web project).
    The ds: namespace is referring to the web project (service class library). I'll continue reviewing my code... thanks a lot.

    Add comment


    (Will show your Gravatar icon)

      Country flag

    Click to change captcha
    biuquote
    • Comment
    • Preview
    Loading