0

I am now maintaining someone else's code in C# and I have found something that I am not sure I understand.

So far when I program Winforms I usually write my classes (and other code). However in this program there are some modules that have partial classes and in the header there is a commentary of the form

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

What are this exactly and what template are they talking about?

(*) This are not the auto-generated partial classes generated when someone design graphically a form. I have seen and used those. I suppose it is something similar but not the same

halfer
  • 19,824
  • 17
  • 99
  • 186
KansaiRobot
  • 7,564
  • 11
  • 71
  • 150
  • 1
    Probably auto-generated by the Entity Framework (see here: https://stackoverflow.com/questions/32327091/keep-manual-changes-of-ef-auto-generated-classes) –  Jun 04 '19 at 01:04
  • https://learn.microsoft.com/en-us/visualstudio/modeling/code-generation-and-t4-text-templates?view=vs-2019 – Robert Harvey Jun 04 '19 at 01:06
  • https://learn.microsoft.com/en-us/ef/ef6/modeling/designer/codegen/ – Robert Harvey Jun 04 '19 at 01:06
  • Yeah, it'll be something generated by a T4 script, but I wouldn't worry too much. If you're the maintainer now then it's unlikely you'll be regenerating those classes if you don't know how they were generated in the first place. – Ben Jun 04 '19 at 01:10
  • @Ben Yes, I suppose it is not much of a problem but I am always after the chance to learn how to do new things :) – KansaiRobot Jun 04 '19 at 01:14
  • @Ben Also I think by understanding where they came from I would understand its working better – KansaiRobot Jun 04 '19 at 01:20
  • @KansaiRobot I get that, but unless they mention it, they could come from anywhere. Learning about T4 could be useful for sure, but this could be anything from a widespread tool like entity framework, through to a simple script the original developer created to help generate initial source code themselves. – Ben Jun 04 '19 at 02:43
  • Found this great link https://www.entityframeworktutorial.net/choosing-development-approach-with-entity-framework.aspx – KansaiRobot Jun 07 '19 at 01:59

1 Answers1

1

The comments below your question point you in the right direction for the specific case at hand but it is interesting to think about what is the general best practice when working on a project with auto generated code that is hopefully marked as such by a comment.

Well such a comment is telling you that this is not actual source code, but code that has been transpiled/generated from another source. It is indeed not the best idea to modify it directly as the process generating that code (most likely as part of the build or using a tool) can discard your changes by overwriting the file. One way to think of that is that it is a bit like if this was binary generated by the compiler (a more correct term would be intermediate language). Same would apply in that case, modifying the binary would be counter productive, error prone and the changes would be discarded by the actual compilation process.

Moreover that kind of code usually follows strict conventions and is expected to work if those conventions are scrupulously respected. One thing to have in mind is that usually such auto generated code is dumb code that is better left to a tool to be generated.

From time to time I write tools to generate code automatically, this can be very useful for things like glue code (mapping to a database, serialization, etc..). Automatically generated tests etc.. One particular example that I remember having used in real production code is stubbing. We had a wpf client that was plagged with null reference exceptions. My team was spending too much time fixing them case by case but more were appearing all the time. At some point I implemented a configurable tool that would produce code that patch the data model putting default stub objects in place of null references.

Samuel Vidal
  • 883
  • 5
  • 16
  • This answer is pretty good, but it could be improved with other examples, maybe from T4. Does anyone know if there's a way to know what tool generated any given autogenerated code? Probably there is not, but it worth asking. – carloswm85 Oct 27 '21 at 14:39