I have a very simple Model, that needs to get validated from Database
public class UserAddress
{
public string CityCode {get;set;}
}
CityCode can have values that are only available in my database table.
I know i can do something like.
[HttpPost]
public ActionResult Address(UserAddress model)
{
var connection = ; // create connection
var cityRepository = new CityRepository(connection);
if (!cityRepository.IsValidCityCode(model.CityCode))
{
// Added Model error
}
}
This seems very WET as I have to use this model at lot of placed and adding the same logic each place seems like i am not using MVC Architecture properly.
So, what is the best pattern to validate model from Database ?
NOTE: Most of the validation are single field lookup from Database, other validation may include combination of field. But right now I am happy with single field lookup validation, as long as it is DRY and is not using too much reflection it is acceptable.
P.S. If any one can hint me how to do a attribute based validation from Database, will be extremely greatful, and a bounty will be awarded.
Aucun commentaire:
Enregistrer un commentaire