2

I want to save two models in a transaction but it will be used on more than one view.

Should I put that transaction in one of those models? Or should I somehow reuse the controller action?

What is the best practice for MVC pattern in this case?

note: My view is consisted of more than one model.

tereško
  • 58,060
  • 25
  • 98
  • 150
user1750290
  • 125
  • 9
  • I always keep my transactions in the controller. Sometimes you have to use multiple things together. – wesside Oct 16 '12 at 14:34
  • But this is exactly the problem because I will have to copy the code of the same transaction to multiple controllers which is not DRY and I have a feeling that there is another(better) way of doing this. – user1750290 Oct 16 '12 at 14:38
  • You can and should definitely use a single controller/action if it is the using the same functionality in 2 places – Brett Gregson Oct 16 '12 at 14:46

1 Answers1

1

That depends, if your business logic, tells you that every time you save model A, model B should be updated/created as well. then put the transaction in your model, so that no matter where you call it (controller , CLI) it will remain true for your logic.

If on the other hand, this transaction is something that only matters to the current user request, then put that in your controller.

At this point the views are irrelevant, you can have any number of models in your view. it has nothing to do with your business logic.

Asgaroth
  • 4,274
  • 3
  • 20
  • 36