Advertisement
Guest User

Untitled

a guest
Aug 24th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. Controller -> Service -> Repository
  2.  
  3. ViewModel to Model mapping -> Validation and other business logic -> CRUD operations
  4.  
  5. readonly IClientService _clientService;
  6.  
  7. public ClientsController(IClientService clientService)
  8. {
  9. _clientService = clientService;
  10. }
  11.  
  12. var client = new Client();
  13. viewModel.MapTo(client);
  14. _clientService.Insert(client);
  15.  
  16. public class MyException : Exception
  17. {
  18. public MyException()
  19. {
  20. }
  21.  
  22. public MyException(string message)
  23. : base(message)
  24. {
  25. }
  26.  
  27. public MyException(string message, Exception inner)
  28. : base(message, inner)
  29. {
  30. }
  31. }
  32.  
  33. public void Insert(Client client)
  34. {
  35. if (client == null)
  36. {
  37. throw new MyException("This thing's not right..");
  38. }
  39.  
  40. try
  41. {
  42. _clientService.Insert(client);
  43. }
  44. catch (MyException)
  45. {
  46. // Add error to ModelState perhaps
  47. }
  48.  
  49. protected override void OnException(ExceptionContext exceptionContext)
  50. {
  51. exceptionContext.ExceptionHandled = true;
  52.  
  53. if (exceptionContext.Exception is MyException)
  54. {
  55. // Perhaps add to ModelState
  56. }
  57. else
  58. {
  59. // Add a generic error message to ModelState and log error
  60. }
  61.  
  62. filterContext.Result = new ViewResult
  63. {
  64. ViewName = ...
  65. };
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement