Ask a questionAsk a question
 

AnswerT4 - generated POCO class modification

  • Thursday, October 29, 2009 9:21 PMIan Mac Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I've generated my POCO classes. Now i want to modify certain getter/setter accessors to set a value based on, for example, another property's value. I also want to plumb in some business logic. Obviously I cannot do this in the generated class, since next time the T4 generator is run, my mods will be overwritten! So, am i missing something here? Obviously I want the convenience of generated POCOs rather than writing by hand plus the ability to mod them with further code. I have a feeling that there must be a massive oversight on my part. I would be most grateful for someone to prove me right!

    Many thanks.

Answers

  • Monday, November 02, 2009 8:08 AMDiego B VegaMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hello Ian,

    There are two alternatives that might help:

    1. In general types we generate are partial classes, so you can extend the types themselves with your own properties and methods in a separate source file without affecting the output of the templates.
    2. With POCO classes it might make more sense to use the teamplate only to generate the type only once at the beginning, then continue evolving the type and the model manually. Currently, to do that, you need to copy the source file and remove the template. We are thinking of ways of making this easier.

    Hope this helps,
    Diego
    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Monday, November 02, 2009 4:22 PMDiego B VegaMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hello Ian,

    Partial classes only help if you are making purely additive changes. I guess I forgot to mention one of the most important options:

    3. You can always modify the template to suit your needs.

    A single template cannot satisfy all requirements. As an example, in the standard template we create partial methods that you can implement in case you want to inject logic in your property setter. But a feature like this just means additional clutter in the code for people that are not going to use it. In the POCO template the goal has been to produce minimalistic classes that can work with EF, and so we decided not to generate the partial methods.

    Now, if you need them, you can introduce them again by modifying the template. I suggest you look at the standard code generation template will give you hints on how to do it.

    Hope this helps,
    Diego


    This posting is provided "AS IS" with no warranties, and confers no rights.

All Replies

  • Monday, November 02, 2009 8:08 AMDiego B VegaMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hello Ian,

    There are two alternatives that might help:

    1. In general types we generate are partial classes, so you can extend the types themselves with your own properties and methods in a separate source file without affecting the output of the templates.
    2. With POCO classes it might make more sense to use the teamplate only to generate the type only once at the beginning, then continue evolving the type and the model manually. Currently, to do that, you need to copy the source file and remove the template. We are thinking of ways of making this easier.

    Hope this helps,
    Diego
    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Monday, November 02, 2009 9:11 AMIan Mac Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Diego,

    Thank you for your response. I guess logically those were the thoughts I had in mind; it just didn't seem 'clean' enough to me and that there must be something more 'engineered' about the process! Good to hear you're thinking about making improvements though.

    I was wondering, let's say I took option 1 (partial class route) but I want to make changes to the setter of one of the properties that are T4 generated. So I then create by hand my (other) partial class. How do i refer to a 'new' version of the property in question, since it already exists in the generated class? Is some sort of override syntax used in this case?

    Thanks once more!
  • Monday, November 02, 2009 4:22 PMDiego B VegaMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hello Ian,

    Partial classes only help if you are making purely additive changes. I guess I forgot to mention one of the most important options:

    3. You can always modify the template to suit your needs.

    A single template cannot satisfy all requirements. As an example, in the standard template we create partial methods that you can implement in case you want to inject logic in your property setter. But a feature like this just means additional clutter in the code for people that are not going to use it. In the POCO template the goal has been to produce minimalistic classes that can work with EF, and so we decided not to generate the partial methods.

    Now, if you need them, you can introduce them again by modifying the template. I suggest you look at the standard code generation template will give you hints on how to do it.

    Hope this helps,
    Diego


    This posting is provided "AS IS" with no warranties, and confers no rights.