Change code generation template for Init() method

4 posts, 1 answers
  1. Norbert
    Norbert avatar
    35 posts
    Member since:
    Dec 2009

    Posted 24 Nov 2011 Link to this post

    Hello,

    I want a way to initialize the created domain objects automatically. Therefore every generated class should have a constructor which calls a method called InitializeClass().
    I will use this method for custom initialisation...

    Thank you very much and go on with OA - it's wonderful!

    regards
    Norbert
  2. Ivailo
    Admin
    Ivailo avatar
    318 posts

    Posted 28 Nov 2011 Link to this post

    Hi Norbert,

    You are on the right track. In order to insert the method call in the class constructor, you will have to modify the code generation templates. Another option would be to add this constructor in a base class and add the class manually, as shown in this code library sample. In both cases make sure you add the method itself either in the class code generation or in partial classes for all domain classes, otherwise you will of course get compilation errors.

    If you want to call the method in the constructor without a base class, we cannot offer you a matching sample, but you can still use the base class project as a pattern. The process of templates modification is described in our dedicated documentation article, where you will find the exact locations of the relevant files.

    Do not hesitate to let us know if you need further information.
     

    Best wishes,
    Ivailo
    the Telerik team

    Q3’11 of Telerik OpenAccess ORM is available for download. Register for the What's New in Data Tools webinar to see what's new and get a chance to WIN A FREE LICENSE!

  3. DevCraft banner
  4. Norbert
    Norbert avatar
    35 posts
    Member since:
    Dec 2009

    Posted 27 Dec 2011 Link to this post

    Sorry, I was trying but I can't get bit to run.
    No matter where to put the Init() Method call...no constructor found?!!

    Could you give me a short tipp, please!!!

    Thank you in advance,
    Norbert
  5. Answer
    Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 29 Dec 2011 Link to this post

    Hello Norbert,

    You can add constructors to the generated persistent classes by editing the Specific.ttinclude file, located at C:\Program Files\Telerik\OpenAccess ORM\dsl2010\CodeGenerationTemplates\CSharp\Includes. At line 133 you will find a method called GenerateClass, which is responsible for generating the code of the persistent classes. You can extend it with a constructor as shown below, I have added comments for the code that I have added:
    ...
    this.GenerateCustomAttributes(codeClass.Attributes);
    this.WriteLine(classSignature);
    this.WriteLine(classOpenToken);
    if (!codeClass.DoubleDerived)
    {
      
    //Custom code starts here:
    #>
    public <#= codeClass.Name #>()
    {
        InitializeClass();
        ... //Additional custom code
    }
      
    <#+ //Custom code ends here.
        this.GenerateProperties(codeClass.Properties);
    ...

    Once you see the constructors generated, I believe you will easily manage to generate the rest of the code you want. Just note that you need to save the rlinq file or use the Run Custom Tool command from the rlinq's context menu in order to regenerate the persistent classes.
    Hope that helps.

    Regards,
    Alexander
    the Telerik team

    Q3’11 of Telerik OpenAccess ORM is available for download. Register for the What's New in Data Tools webinar to see what's new and get a chance to WIN A FREE LICENSE!

Back to Top