Agregate exception class, IValidatableObject si ModelState in mvc.

Acesta este un guest post de la Pascanu Alexandru. A facut o chestie deosebita pentru http://pmkb.codeplex.com/ si a scris despre ea.

Agrregate exception class este un un tip de exceptie care da posibilitatea programatorului de a arunca o exceptie care are insa ca sursa (InnerException) o colectie de exceptii nu una ca in cazul classic al celorlalte clase ce deriveaza din exception.Mai multe aici: http://msdn.microsoft.com/en-us/library/system.aggregateexception.aspx.

Interfata IValidatableObject din namespace-ul DataAnnotation http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.ivalidatableobject.aspx

expune o singura metoda de implementat Validate de forma : IEnumerable<ValidationResult> Validate(

ValidationContext validationContext

)

Mai jos void a un exemplu cum conlucreaza intr-o aplicatia asp.net mvc cele doua de mai sus plus ModelState.

Presupn ca am clasa,undeva in model, POCO Link ce implemneteaza aceasta interfata


Class Link:IvalidatableOBject

{

Public string LinkName{get;set;}

Pulbic string LinkDescription{get;set;}

Public string LinkUrl {getset;}

//implementez metoda Validate

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)

{

var validationResults = new List<ValidationResult>();

if (string.IsNullOrEmpty(this.LinkDescription))

validationResults.Add(new ValidationResult(PMKBEntities_resource.Link_LinkDescription_Required,new string[1]{ Link_List.FieldNames.LinkDescription}));

if (string.IsNullOrEmpty(this.LinkName))

validationResults.Add(new ValidationResult(PMKBEntities_resource.Link_LinkName_Required, new string[1] { Link_List.FieldNames.LinkName}));

if (string.IsNullOrEmpty(this.LinkUrl))

validationResults.Add(new ValidationResult(PMKBEntities_resource.Link_LinkUrl_Required, new string[1] { Link_List.FieldNames.LinkUrl}));

return validationResults;

}

}

In model,in clasa Repository aferenta entitatii Link ,in metoda Save mai exact ,inaite de persistarea entitatii voi verifica daca ce intoarce metoda Validate adica Ienumerable<ValuidationResult> contine date sau nu.In functie de aceasta ,mai exact pentru fiecare ValidationResult ,crrez o clasa Exception aferenta,pe care o pun intr-o colectie Ienumerable<Exception>.

In final atribui aceasta lista proprietatii InnerExcception a unui obiect AggregatException creat in prealabil.

In final arunc obiectul AggregateException Layerului urmator ,cel care a apelat repository ,adica controllerului afaerent entitatii link.

In clasa LinkControloler in actiunea in care fac Save la entitatea link in catch voi actualiza colectia de errrori a obiectului ModelState afferent controller in cazul in care a fost prins un obiect AggregateException-astfel in View voi avea validation summry-ul cu erorile de validare aferente

Exemplu:


catch (AggregateException aggrexp)

{

foreach (var exp in aggrexp.InnerExceptions)

{

ExceptionProperty arg = exp as ExceptionProperty;

if (arg != null)

{

ModelState.AddModelError("link."+arg.Property, arg.Message);

}

else

{

ModelState.AddModelError("", exp.Message);

}

}

}

PS: Daca va pasioneaza subiectul, puteti citi si
http://msprogrammer.serviciipeweb.ro/2012/03/19/ivalidatableobject-and-idataerrorinfo/

Leave a Reply

Your email address will not be published. Required fields are marked *