W4der
W4der

Reputation: 9

MVC controller dependency

I have question connected with MVC in pure C#.

There are 3 scripts for Player (M + V + C)

1) PlayerModel

public int level;
public int money;

2) PlayerView

with button AddLevel, which triggers broadcast message

"PlayerView.LevelUP"

3) PlayerController

The controller receive message PlayerView.LevelUp and do something like this:

PlayerModel.level++;
PlayerView.Update();

The above example is clear, everybody knows how MVC works, but when it come to the more complex situation I have problem. In example we use still our PlayerModel, PlayerView and PlayerController, but we want to add Shop.

So again we have 3 scripts for Shop (M+V+C)

1) ShopModel

List of available shop items

2) ShopView

Displays List of items

When button buy is clicked for specific item the message is send:

"ShopView.BuyItem.{id}, price"

3) ShopController

and the problem occurs here. I don't know how to substract the money from my PlayerModel. Should I use:

playerModel =- price;

or should I use reference to the PlayerController and do something like this:

playerController.SubstractMoney(price);

The problem might be more complex when we would like to add the Confirmation Window - are you sure to buy the item? With ConfirmationWindowView, ConfirmationController, ConfirmationModel?

Upvotes: 0

Views: 75

Answers (2)

DevGuru
DevGuru

Reputation: 156

Each of your class should have a single responsibility according to SOLID principles. Shop controller is different than player controller, same as the models. Break your project into Presentation (View), Business(Controller) and Data (Model) layers. Your controllers will contain all business logic while the data layer will provide data.

Upvotes: 1

Shivani
Shivani

Reputation: 216

Your approach can completely depend upon the scenarios how you want to make a call. You can create a seperate viewmodel for combined shop + player.

Regarding your 3rd point - ShopController: You can either make an ajax call or call action method directly(playerController.SubstractMoney(price);) It depends on your calls.

For confirmation window: you can use jquery modal popups or any 3rd pop ups eg: foundation.js. Make a global popup(with a global id) and then you can call it from any view.

Upvotes: 0

Related Questions