Created
November 29, 2011 10:19
-
-
Save hanssens/1404299 to your computer and use it in GitHub Desktop.
[C#] Adding annotations/metadata with partial classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[MetadataType(typeof(HappyFunBallMetadata))] | |
public partial class HappyFunBall | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public Color Colour { get; set; } | |
public int Weight { get; set; } | |
} | |
public partial class HappyFunBall | |
{ | |
public class HappyFunBallMetadata | |
{ | |
[Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "HappyFunBallIdRequired")] | |
public int Id { get; set; } | |
[StringLength(25)] | |
[Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "HappyFunBallNameRequired")] | |
public string Name { get; set; } | |
public Color Colour { get; set; } | |
public int Weight { get; set; } | |
} | |
} | |
static List<ValidationResult> Validate(object instance, Type metaDataType) | |
{ | |
return Validate(instance, metaDataType, true); | |
} | |
static List<ValidationResult> Validate(object instance, Type metaDataType, bool validateAllObjects) | |
{ | |
var listOfErrors = new List<ValidationResult>(); | |
var T = instance.GetType(); | |
// Spider through the object's attributes to determine if the MetadataTypeAttribute is provided | |
//$$$$BUG:BUG var metaDataType = T.GetCustomAttributes(typeof(MetadataTypeAttribute), true).First() as Type; | |
//TypeDescriptor.AddProviderTransparent( | |
// new AssociatedMetadataTypeTypeDescriptionProvider(T, | |
// typeof(HappyFunBall.HappyFunBallMetadata)), T); | |
TypeDescriptor.AddProviderTransparent( | |
new AssociatedMetadataTypeTypeDescriptionProvider(T, metaDataType), T); | |
var validationContext = new ValidationContext(instance, null, null); | |
//var listOfErrors = new List<ValidationResult>(); | |
var isValid = Validator.TryValidateObject(instance, validationContext, listOfErrors, true); | |
if (!isValid) | |
{ | |
StringBuilder errorMsg = new StringBuilder(); | |
listOfErrors.ForEach((ValidationResult ex) => | |
errorMsg.Append(ex.ErrorMessage) | |
); | |
throw new ValidationException(errorMsg.ToString()); | |
} | |
return listOfErrors; | |
} | |
static void Main(string[] args) | |
{ | |
try | |
{ | |
var myBall = new HappyFunBall(); | |
var validationResults = Validate(myBall, typeof(HappyFunBall.HappyFunBallMetadata)); | |
if (validationResults != null) | |
{ | |
validationResults.ForEach(result => | |
{ | |
Console.WriteLine(result.ErrorMessage); | |
}); | |
} | |
Console.ForegroundColor = ConsoleColor.Green; | |
Console.WriteLine("All is well!"); | |
} | |
catch (Exception ex) | |
{ | |
Console.ForegroundColor = ConsoleColor.Red; | |
Console.WriteLine(ex.Message); | |
} | |
finally | |
{ | |
Console.ResetColor(); | |
} | |
Console.WriteLine("Press any key to quit..."); | |
Console.ReadKey(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment