This question is related to my ASP.NET MVC 2 development, but it could apply to any MVC environment and a question of where the logic should go.
So let’s say I have a controller that takes an online payment such as a shopping cart application. And I have the method that accepts the customers’ credit card information:
public class CartController : Controller
CartRepository cartRepository = new CartRepository()
[HttpPost]
public ActionResult Payment(PaymentViewModel rec)
{
if(!ModelState.IsValid)
{
return View(rec);
}
// process payment here
return RedirectToAction("Receipt");
}
At the comment process payment here should the payment processing be handled:
- In the controller?
- By the repository?
- Someplace else?
You want 3. Someplace else.
Put this in a class library. Create an interface that has all the methods required for the payment processing. Make the methods generic. Put the specifics in the implementation of the interface. Then derive your payment processing service from that interface. This give you options that include testing and multiple payment processors.
Look at the MVC Storefront videos at http://www.asp.net/learn/mvc-videos/. Probably video #23 (part 22). It’s been a while since I viewed these.