Jul 14, 2011 07:43 PM|francesco abbruzzese|LINK
About a general solution for the exception it should be possible solving by rewriting a metadataprovider. However, the point is that if one doesn't use the pre-existing logics for retrieving attributes information one need to re-implent also the associated
caching mechanism...without caching the performance should be unacceptable.
However as I said the problem is just with the required attribute if you organize adequately your classes.
The best approach that solve the problem is:
1) define a metadata class whose properties have the right data type, and are defined as virtual but are "fake", that is get always return null(or default(T) for value types) and the set do nothing;
2) apply the metadata class to the model
3) all Viewmodel inherits from the MetaDataclass and override with "actual" properties just the properties they need. No memory is wasted because the "fake" properties doesn't waste memory in the inheriting classes since they have no storage associted with
The above works fine if one avoid using required attributes in the MetaData class and apply the directly to the classes that need them.
This is reasonable because while all other validation attributes normally apply to all viewmodels an to the ViewModel, the Required attribute depends on the usage. In particular a property may be required in the model without being required in some ViewModels,
since the value of that property might be COMPUTED by the system instead of being provided by the user. The vice versa is also possible.