Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Controller -> Service -> Repository
- ViewModel to Model mapping -> Validation and other business logic -> CRUD operations
- readonly IClientService _clientService;
- public ClientsController(IClientService clientService)
- {
- _clientService = clientService;
- }
- var client = new Client();
- viewModel.MapTo(client);
- _clientService.Insert(client);
- public class MyException : Exception
- {
- public MyException()
- {
- }
- public MyException(string message)
- : base(message)
- {
- }
- public MyException(string message, Exception inner)
- : base(message, inner)
- {
- }
- }
- public void Insert(Client client)
- {
- if (client == null)
- {
- throw new MyException("This thing's not right..");
- }
- try
- {
- _clientService.Insert(client);
- }
- catch (MyException)
- {
- // Add error to ModelState perhaps
- }
- protected override void OnException(ExceptionContext exceptionContext)
- {
- exceptionContext.ExceptionHandled = true;
- if (exceptionContext.Exception is MyException)
- {
- // Perhaps add to ModelState
- }
- else
- {
- // Add a generic error message to ModelState and log error
- }
- filterContext.Result = new ViewResult
- {
- ViewName = ...
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement