ASP.NET MVC - throwing exceptions?

I have a common repository - should the repository remove exceptions or should I keep it at a dead end? If I selected an exception from it, should the service layer then catch it and throw an exception with a friendlier message before it goes to the controller?

  • Pos1: Repository (Throw) => Service (Catch, Throw new) => Controller (Catch)
  • Pos2: Repository => Service (Throw) => Controller (Catch)
+3
source share
3 answers

Should the repository throw exceptions or should I keep it at a standstill?

- . - "" , :)

, " " - , - "" "" .

, , ?

, , . , - .

- Ms Ent Libs, ( ), , - ; , .

: ?

+2

1.

"" " " . , . , .

:

  • , .

  • , , , . , , "", .

ALL , , . , , LINQ, , .

, Repositiory , , .

, . : "", . , . , ( ) .

, , . Mute: , .

+2

, null , . , , .

public ActionResult Details(int id) {

    Dinner dinner = dinnerRepository.FindDinner(id);

    if (dinner == null)
        return View("NotFound");
    else
        return View("Details", dinner);
}

http://nerddinnerbook.s3.amazonaws.com/Part4.htm

, , :

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection formValues) {

    Dinner dinner = dinnerRepository.GetDinner(id);

    try {

        UpdateModel(dinner);

        dinnerRepository.Save();

        // Success!
        return RedirectToAction("Details", new { id=dinner.DinnerID });
    }
    catch {

        // Old-school data validation from ASP.NET MVC 1.0
        foreach (var issue in dinner.GetRuleViolations()) {
            ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
        }

        // Return original view, so user can correct errors.
        return View(dinner);
    }
}

http://nerddinnerbook.s3.amazonaws.com/Part5.htm

+1
source

Source: https://habr.com/ru/post/1769223/


All Articles