Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Clarification: I think this needs some clarification because the answers don't seem to be addressing the question.</p> <p>For example,</p> <p>Create Person Validates that has name, age and email is not in use Delete Person Validates that the Person isn't a Parent. Business logic dictates that Parents can't be eliminated</p> <p>How do I have these two different validation scenarios?</p> </blockquote> <p>"Encapsulate what varies."</p> <p>One way to accomplish this is using the Command pattern, and applying your validation to the Command instead of the Entity. Here's a simple example without using any validation framework, but the idea is the same.</p> <pre><code>public class Blub { public int BlubId { get; set; } public string Name { get; set; } public bool SomeBlockingCondition { get; set; } } public class BlubEditController : Controller { [AcceptVerbs(HttpVerbs.Get)] public ActionResult Rename(int blubId) { var ctx = new DataContext(); var blub = ctx.Blubs.Single(o =&gt; o.BlubId==blubId); return ShowRenameForm(blub); } [AcceptVerbs(HttpVerbs.Post)] public ActionResult Rename(int blubId, RenameCommand cmd) { var ctx = new DataContext(); var blub = ctx.Blubs.Single(o =&gt; o.BlubId==blubId); cmd.Subject = blub; if (cmd.Validate(ModelState, "cmd.")) { cmd.Execute(); ctx.SubmitChanges(); return RedirectToAction("Show", new { blubId = blubId }); } else { return ShowRenameForm(blub); } } } public class RenameCommand { public Blub Subject { get; set; } public string Name { get; set; } public bool Validate(ModelStateDictionary modelState, string prefix) { if (Subject.SomeBlockingCondition) { modelState.AddModelError(prefix + "SomeBlockingCondition", "No!"); return false; // Optional shortcut return to prevent further validation. } if (String.IsNullOrEmpty(this.Name)) { modelState.AddModelError(prefix + "Name", "Seriously, no."); } return modelState.IsValid; } public void Execute() { Subject.Name = this.Name; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload