Constructors for Data Contracts

Jul 29, 2009 at 7:15 PM

All,

Is there a way to add a construcor to the generated data contract classes.

Basically i want to set some default values on the members of a data contract when ever a new instance of data contract is initialized.

Thanks,

Naveen Kumar.

Jul 29, 2009 at 10:05 PM
naveen_virgo14 wrote:
>
> From: naveen_virgo14
>
> All,
>
> Is there a way to add a construcor to the generated data contract classes.
>
> Basically i want to set some default values on the members of a data
> contract when ever a new instance of data contract is initialized.
>
> Thanks,
>
> Naveen Kumar.
>
Well, they're partial classes, so you can define another partial class
of the same name directly under the data contracts project (i.e. not in
the Generated Code folder) and define your constructor there. That's
what I do for Fault Contracts in order to define a constructor that
takes a string to be assigned to the FaultMessage.

Does that help?

Cheers


Peter
Jul 30, 2009 at 5:13 PM

Peter,

Thanks for the reply.

I know this way of modifcation. i was just wondering if it can be done using UI while modelling.

Thanks,

Naveen Kumar.

Jul 30, 2009 at 6:21 PM
naveen_virgo14 wrote:
>
> From: naveen_virgo14
>
> Peter,
>
> Thanks for the reply.
>
> I know this way of modifcation. i was just wondering if it can be done
> using UI while modelling.
>
> Thanks,
>
> Naveen Kumar.
>

I may be wrong, but I don't think so.

Cheers


Peter
Aug 2, 2009 at 2:45 AM

Naveem,

Peter is right in both suggestions and you can also update the .tt datacontract file and add the constructor there so you will always get the same constructors in all your DCs. If you need flexibility then the first advise from Peter is the best choice.

Sep 11, 2009 at 11:33 PM

What is a ".tt" datacontract file. Where do I find it, and how should I modify it?

Thanks,

Mike

Sep 14, 2009 at 11:27 AM

The text template file (extension .tt) is where you can manipulate code gen in a "scripting-like" way so you may add your own custom directives or any other code gen update (search for "T4 text template code generation").

In WSSF you can fin the DataContract text templates for WCF in "..\Data Contract DSL\Dsl\TextTemplates\WCF\CS" and for ASMX in "..\Data Contract DSL\Dsl\TextTemplates\Asmx\CS". There you may modify the DataContract.tt file and add your own customization. You don;t need to re build WSSF, just re open VS.