0

I have an auto generated partial class in my Model folder with lots of properties and I need to modify some of the setters in these properties(annotate property).
To ensure that I don't repeat myself in the future(after a refresh) I have another partial class with the same properties by use of attributes, where these properties cannot be modified, only via provided attributes.
So I have built a custom attribute class to modify those properties. But this seems more abusive than useful, so how can I annotate the properties that I want that it's also a DRY and clean code?

public partial class Stud  <---- Extended partial class
{

    public string Property2                 <---- This does not work! 
    { 
        get {return Property2;}
        set {Property2 = Property2.Trim();} 
    }
}
H35am
  • 768
  • 2
  • 12
  • 32
  • Not sure I understand what you want to do. Your code example calls itself in a loop. If you are trying to change the auto-generated property implementation with a different implementation, then that's not possible. You will need a different property to get and set the generated property, and do the new logic there. – Tsahi Asher Aug 24 '17 at 06:58
  • I'm trying to override some properties in Models auto generated partial class. It is more than validation and I already using Metadata attributes. – H35am Aug 24 '17 at 08:32

1 Answers1

0

Your best option, assuming your class is generated with a T4 template, is to add custom logic in the template for generating the specific property. Otherwise, there is no way to override a property defined in one part of a partial class, in the other part of the class.

Another option is to wrap the generated property with a custom property in the part under your control, which will write to and read from the generated property with your custom logic. Then, you will use the wrapper property to access the generated one.

Tsahi Asher
  • 1,767
  • 15
  • 28