From asp.net tutorials:
Controllers: Classes that handle incoming browser requests, retrieve model data, and then specify view templates that return a response to the browser.
So, in order to keep your controllers thin, you might have the following:
public class MyController {
private readonly IFooService fooService;
public MyController(IFooService fooService) {
this.fooService = fooService;
}
[HttpGet]
public ActionResult Foo(int id) {
var foo = fooService.Find(id);
var model = new FooViewModel(foo);
return View(model);
}
[HttpPost]
public ActionResult CreateFoo(CreateFooModel model) {
if (!ModelState.IsValid()) {
return View(model);
}
var foo = model.ToFoo();
fooService.Add(foo);
// return Redirect() or so
}
}
some 10 lines of code for validation logic, etc
You should not have validation logic in an action, especially in a "get" request. For "post" requests, the validation logic can be added on "create"-models (like CreateFooModel, in the example above) by implementing the IValidatableObject interface on the model in question.
The controllers will be thin enough if you do only this:
For GET requests:
- fetch the business object or more business objects from one or more application / business services
- map the business object / objects to a view model, preferably using just one line of code
- return a view - pass it the previously built view model.
For POST requests
- check if the model is valid (IValidatableObject should be implemented for the "create"-model in question). Supposing it is valid,
- instantiate a business object from the "create"-model, preferably in one line of code. Feel free to use a factory, a mapper or a simple function responsible with the conversion of the model to a business object
- pass the business object to one or more services, as required by your application
- return a redirect or whatever.
All in all, a GET action should generally have 3 lines of code, while a POST action might have 6-7 lines of code.
Also, you might have a look on:
- How we do MVC – View models (lostechies.com)
- http://stackoverflow.com/questions/25054416/viewmodels-cqrs-and-entities/25169023#25169023 https://stackoverflow.com/questions/25054416/viewmodels-cqrs-and-entities/25169023#25169023
From asp.net tutorials:
Controllers: Classes that handle incoming browser requests, retrieve model data, and then specify view templates that return a response to the browser.
So, in order to keep your controllers thin, you might have the following:
public class MyController {
private readonly IFooService fooService;
public MyController(IFooService fooService) {
this.fooService = fooService;
}
[HttpGet]
public ActionResult Foo(int id) {
var foo = fooService.Find(id);
var model = new FooViewModel(foo);
return View(model);
}
[HttpPost]
public ActionResult CreateFoo(CreateFooModel model) {
if (!ModelState.IsValid()) {
return View(model);
}
var foo = model.ToFoo();
fooService.Add(foo);
// return Redirect() or so
}
}
some 10 lines of code for validation logic, etc
You should not have validation logic in an action, especially in a "get" request. For "post" requests, the validation logic can be added on "create"-models (like CreateFooModel, in the example above) by implementing the IValidatableObject interface on the model in question.
The controllers will be thin enough if you do only this:
For GET requests:
- fetch the business object or more business objects from one or more application / business services
- map the business object / objects to a view model, preferably using just one line of code
- return a view - pass it the previously built view model.
For POST requests
- check if the model is valid (IValidatableObject should be implemented for the "create"-model in question). Supposing it is valid,
- instantiate a business object from the "create"-model, preferably in one line of code. Feel free to use a factory, a mapper or a simple function responsible with the conversion of the model to a business object
- pass the business object to one or more services, as required by your application
- return a redirect or whatever.
All in all, a GET action should generally have 3 lines of code, while a POST action might have 6-7 lines of code.
Also, you might have a look on:
- How we do MVC – View models (lostechies.com)
- http://stackoverflow.com/questions/25054416/viewmodels-cqrs-and-entities/25169023#25169023
From asp.net tutorials:
Controllers: Classes that handle incoming browser requests, retrieve model data, and then specify view templates that return a response to the browser.
So, in order to keep your controllers thin, you might have the following:
public class MyController {
private readonly IFooService fooService;
public MyController(IFooService fooService) {
this.fooService = fooService;
}
[HttpGet]
public ActionResult Foo(int id) {
var foo = fooService.Find(id);
var model = new FooViewModel(foo);
return View(model);
}
[HttpPost]
public ActionResult CreateFoo(CreateFooModel model) {
if (!ModelState.IsValid()) {
return View(model);
}
var foo = model.ToFoo();
fooService.Add(foo);
// return Redirect() or so
}
}
some 10 lines of code for validation logic, etc
You should not have validation logic in an action, especially in a "get" request. For "post" requests, the validation logic can be added on "create"-models (like CreateFooModel, in the example above) by implementing the IValidatableObject interface on the model in question.
The controllers will be thin enough if you do only this:
For GET requests:
- fetch the business object or more business objects from one or more application / business services
- map the business object / objects to a view model, preferably using just one line of code
- return a view - pass it the previously built view model.
For POST requests
- check if the model is valid (IValidatableObject should be implemented for the "create"-model in question). Supposing it is valid,
- instantiate a business object from the "create"-model, preferably in one line of code. Feel free to use a factory, a mapper or a simple function responsible with the conversion of the model to a business object
- pass the business object to one or more services, as required by your application
- return a redirect or whatever.
All in all, a GET action should generally have 3 lines of code, while a POST action might have 6-7 lines of code.
Also, you might have a look on:
- How we do MVC – View models (lostechies.com)
- https://stackoverflow.com/questions/25054416/viewmodels-cqrs-and-entities/25169023#25169023
From asp.net tutorials:
Controllers: Classes that handle incoming browser requests, retrieve model data, and then specify view templates that return a response to the browser.
So, in order to keep your controllers thin, you might have the following:
public class MyController {
private readonly IFooService fooService;
public MyController(IFooService fooService) {
this.fooService = fooService;
}
[HttpGet]
public ActionResult Foo(int id) {
var foo = fooService.Find(id);
var model = new FooViewModel(foo);
return View(model);
}
[HttpPost]
public ActionResult CreateFoo(CreateFooModel model) {
if (!ModelState.IsValid()) {
return View(model);
}
var foo = model.ToFoo();
fooService.Add(foo);
// return Redirect() or so
}
}
some 10 lines of code for validation logic, etc
You should not have validation logic in an action, especially in a "get" request. For "post" requests, the validation logic can be added on "create"-models (like CreateFooModel, in the example above) by implementing the IValidatableObject interface on the model in question.
The controllers will be thin enough if you do only this:
For GET requests:
- fetch the business object or more business objects from one or more application / business services
- map the business object / objects to a view model, preferably using just one line of code
- return a view - pass it the previously built view model.
For POST requests
- check if the model is valid (IValidatableObject should be implemented for the "create"-model in question). Supposing it is valid,
- instantiate a business object from the "create"-model, preferably in one line of code. Feel free to use a factory, a mapper or a simple function responsible with the conversion of the model to a business object
- pass the business object to one or more services, as required by your application
- return a redirect or whatever.
All in all, a GET action should generally have 3 lines of code, while a POST action might have 6-7 lines of code.
Also, you might have a look on:
- How we do MVC – View models (lostechies.com)
- http://stackoverflow.com/questions/25054416/viewmodels-cqrs-and-entities/25169023#25169023