I need to insert a common set of code in Get method of all the entities generated by the RIA Domain Service wizard. Is it possible to override Domain Service wizard's code generation so I can insert that code automatically?
Thanks, Ankur
I need to insert a common set of code in Get method of all the entities generated by the RIA Domain Service wizard. Is it possible to override Domain Service wizard's code generation so I can insert that code automatically?
Thanks, Ankur
There is also a great blog-post written by Stefan Cruysberghs covering this topic.
He is using the RIAServices.T4 nuget-package to modify the code that is being generated on the client side. The referenced article covers this in great detail, but the essence is as follows:
Implement your own code generator using a pre-defined base class
public class MyDomainContextGenerator : CSharpDomainContextGenerator
{
protected override void GenerateClassDeclaration()
{
// Add something before the class generation here...
base.GenerateClassDeclaration();
}
}
Advise RIAServices to use that generator by means of an attributed class:
[DomainServiceClientCodeGenerator("MyCodeGenerator", "C#")]
public class MyCodeGenerator : CSharpClientCodeGenerator
{
protected override DomainContextGenerator DomainContextGenerator
{
get { return new MyDomainContextGenerator(); }
}
}
You can definitely modify and edit (or even write your own) T4 templates that are responsible for the code generation.. something along the lines of these two links:
T4 Code Generator for WCF RIA Services
Using T4 to change the way RIA services work
Furthermore, you can check the T4 template for RIA services (server side) from the WCF RIA Services Contrib project on CodePlex that allows you to modify your GET methods as follows:
public IQueryable<<#= Entity.Name #>> Get<#= Entity.Name #>()
{
//Inject Custom code here
return this.ObjectContext.<#= Entity.Mapping.StoreEntitySet #>;
}