Can any processing be done on the model? [MVC]

I decided to make a big push towards MVC for all the new sites I make. My question is if you can have any processing at the model level.

The case related to this question is a video site. I have a video class (model) and one of the things I need to do when the user views the video, I need the view to register with the database. I'm not sure if I need to add a request to the controller or if I can add an addView method to the Video class.

The main basic question for me is what methods will I restrict in models? Could it be anything or should it only be accessor methods (aka getValue / setValue)?

+1


source to share


6 answers


Ruby on Rails has the motto skinny controller, fat model . This is not specific to Rails and should be done with any mvc framework.



+5


source


I think your model is where you can handle this. Now your model is necessarily composed of your entity classes. Your model, in my opinion, will include your entities as well as any business logic you need to implement. Your controller should simply handle the I / O for the view by invoking the model's methods to perform actions called by the user through the user interface (view).



+2


source


This is how I will do it. It must be valid in almost any language.

The view will initiate a method call to the controller's OnView () method, and then display whatever the controller will return back to it (in a controlled manner, of course ... I think your view will contain a video player, so you get some kind of video from the controller)

Your controller has an OnView () method that does 3 things: instantiate the Video object (i.e. use your data layer to get the model object), call the updateViewCount () method on the Video object, and show the video (for example, by returning the object Video to the presentation).

The video module object contains data representing your video and any material you need to work with, which includes updateViewCount (). The rationale for this is that the video has a counter (aggregation). If the "view count" is to be a complex object, not just an integer, so be it. The data layer that creates Video objects from their primitive on-disk representation (database?) Will also be responsible for loading and creating the appropriate view count object as part of video creation.

So my $ 0.02. You can see that I have created a 4th place (the first three are Model, View and Controller) which is the data layer. I don't like loading model objects and saving them because they need to know about your database. I don't like controllers that load and save directly because that would lead to duplicate code between controllers. Thus, a separate layer of data that controllers must directly access.

As a final note, here's how I look at views: everything the user does and everything the user sees has to go through the view. If a button that says "play" appears on the screen, it should not directly call the controller method (in many languages ​​there is no danger of doing this, but some, like PHP, could potentially allow it). Now, the "play" view method will just expand and call the corresponding method on the controller (which in the example is OnView) and do nothing, but this layer is conceptually important, although functionally irrelevant. Likewise, in most situations, I would expect your view to play a video stream, so the data returned by the controller to the view will be a stream in the format it wants to see.which may or may not be your exact model object (and adding that extra decoupling layer might be appropriate, even if you can directly use the Video object). This means that I could force my OnView method to accept a parameter indicating which video format I want to receive, or perhaps create separate view methods on the controller for each format, etc.

Long enough for you ?: D I'm expecting some flames from Ruby developers who seem to have a slightly different (albeit incompatible) MVC idea.

+1


source


Since you can use whatever model code you want to use with MVC (not limited to LINQ only), the short answer is yes. What needs to be done in the model is perhaps the best question. In my opinion, I would add a ViewCount property (which will probably map to a column in the Video table if you are not tracking the user, in which case it will be in the UserVideo table). Then from the controller you can increase the value of that property.

0


source


With MVC, people seem to be struggling with setting four layers to three.

The MVC paradigm misdirects the data store. And this is the "fourth layer". The model has processing; but since it also accesses data, programmers add SQL as well. Wrong. Create a data abstraction layer that is the only place to talk to background storage. MVC must be DMVC.

0


source


Keep in mind that there are many variations in MVC and there is no real "right way" to do something. Ultimately, how you design your classes comes down to personal preference. However, since you asked for design advice, here are my two cents:

The business logic belongs to the controller. Save it from the model and preview.

Of the many variations on the MVC pattern, the passive style seems to be the easiest to test. Passively, your classes are constructed like this:

  • Your controller is smart: it makes changes to the database, updates the model, and synchronizes the view with the model. Apart from the model and view reference, the controller should store as little state information as possible.

  • The model is "silly", which means that it contains only view state and no additional business logic. Models should not contain a link to a view or controller.

  • The view is "dumb", which means that it only copies information from the model to the user interface and calls event handlers that are handled by the controller. There should be no additional business logic in the view. Views must not contain a link to a controller or model.

If you're an MVC purist, it won't make sense for the model to update itself or the database, since those responsibilities are the responsibility of the controller, so it would n't make sense to create an addView for your Video class.

-1


source







All Articles