DarkwingDuck
DarkwingDuck

Reputation: 2706

How can I intercept the domain services generated by RIA tooling during compile?

Currently it is possible to customise how entities are generated on the client side of RIA services via the 'CodeProcessor' class. For any domain service that needs client side generation, the code processor can be used to intercept each entity/namespace and make adjustments, etc.

But in my case I need to actually intercept which domain services will have generation occur to begin with.

I noticed that in the DomainServiceDescription there is a static private readonly domainServiceMap which contains all the services being generated. However there is no way to access this map during compile time that I can find.

I'm hoping (fingers crossed) that there is a hook somewhere that I can interact with that service map such that I can remove items from the collection prior to generation commencing.

Any ideas?

Upvotes: 2

Views: 422

Answers (1)

Miguel Madero
Miguel Madero

Reputation: 1948

The CodeProcessor seems to the old way of doing that and I think it's limited to only generate the entities. In newer versions we can specify T4 based Generators for all the different pieces.

Install RIAServices.T4 from Nuget in the WebProejct or a Class Library that will contain the the code generation classes. PM> Install-Package RIAServices.T4

If you have the toolkit already, just add a reference to "Microsoft.ServiceModel.DomainServices.Tools.TextTemplate"

Then we need to inherit from CSharpClientCodeGenerator, which doesn't really generate anything, but just tells RIA which generators to use by overriding some of its properties.

[DomainServiceClientCodeGenerator("MyCustomGenerator", "C#")]
public class MyCSharpClientCodeGenerator : CSharpClientCodeGenerator
protected override Microsoft.ServiceModel.DomainServices.Tools.TextTemplate.DomainContextGenerator DomainContextGenerator
    {
        get
        {
            //return base.DomainContextGenerator;
            return new MyDomainContextGenerator();
        }
    } 

Then we tell R# to implement that class for us (MyDomainContextGenerator) which has to inherit from CSharpDomainContextGenerator. Of course if you use R# it will just do it for you.

You have other 4 different code generators that you can provide from MyCSharpClientCodeGenerator.

Now to hook it all up, in the Silverlight project file we need to tell RIA to use our generator. We have to edit the Silverlight project and add the following element inside the first PropertyGroup just after LinkedServerProject (the order doesn't matter, I just say that as a reference).

  <LinkedServerProject>..\SilverlightApplication2.Web\SilverlightApplication2.Web.csproj</LinkedServerProject>
  <RiaClientCodeGeneratorName>
    SilverlightApplication2.Web.RiaStuff.MyCSharpClientCodeGenerator,SilverlightApplication2.Web
  </RiaClientCodeGeneratorName>
</PropertyGroup>

Recompile the Silverlight project and voila. It might crash. To debug this we can open another instance of Visual Studio, set breakpoints on the Generators in this new instance, attach to the first instance of Visual Studio and recompile the Silverlight project.

Upvotes: 4

Related Questions