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. Now my question is, am I doing this correctly or am I just creating a mess. Is there a better or another way to achieve my goal? Please explain how to approach this delicate matter.
This is an auto generated class in Models folder:
public partial class Stud
{
public string Property1{ get; set; }
public string Property20{ get; set; }
}
This is the class for mine metadata with properties that I want to modify:
public class MetadataClasses
{
public class StudMetadata
{
[Required, DisplayName("Property one"), UpperCaseAttribute]
public string Property1 { get; set; }
[Required, DisplayName("Property two"), ReplaceNrAttribute]
public string Property2{ get; set; }
}
}
This is an custom attribute class in different folder called Attributes:
public class UpperCaseAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
try
{
validationContext
.ObjectType
.GetProperty(validationContext.MemberName)
.SetValue(validationContext.ObjectInstance, value.ToString().ToUpper(), null);
}
catch (System.Exception)
{
}
return null;
}
}
This is an partial class named "PartialClasses.cs" in Models folder:
[MetadataType(typeof(MetadataClasses.StudMetadata))]
public abstract partial class Stud
{
}