If you are building a web application, and you have a user management component, is it recommended to do:
class UserAdminView extends View {
}
or
class UserAdminUpdateView extends View {
}
What about controllers? Thoughts?
0
It will depend
on which web-development design pattern your team
is set to go/follow.
For example: in MVC design patter
, each view it is recommended to have a dedicated view-model. While your models are the constructs that serve as a data containers, it is not recommend expose them directly as view-models. Thus, depending on view requirement, a custom view-model is build from a single or multiple models.
Important productivity tip: for a convention-based object-object mapper always try to look fr tools like AutoMapper. It will save tons of time during the development.
If you want to do it MVC style, I personally most of the time have different views for different actions. In some cases, when the content for show / edit / save / list or whatever I had there was very similar I used only one view. But rarely.
As for the Controller, there should be one controller with different methods for the different actions.
As for Model. I consider models kind of redundant. I use them only to validate forms and only because they integrate with the framework.
Business logic should be outside the MVC framework, which I consider only a delivery mechanism to the application.
PS: I know many web developers will not agree with my last statement. Don’t flame, think. Thanks.
4
I think that the first approach “class UserAdminView extends View” is better. UpdateView fits better as an method of class a than an actual class.
If you want UserAdminUpdateView to be inheriting from a class than the superclass should be an UserAdmin class not a view class.
Taking a look at a popular MVC framework such as Codeigniter for PHP or even the structure of Play Framework (java) should help.
For example in both cases the idea of a view is that it seperates the coding from the templating (gui). Creating an individual view as its own class confuses this as a front end developer shouldn’t be using code. It would be more beneficial to create a view class which effectively allows you to load HTML based templates and pass your data to them. For example:
class userAdmin extends Controller {
public void view() {
data = userAdminModel->getView();
view_html->('header');
view_html->('userAdmin.view',data);
view_html->('footer');
}
public void updateView() {
success = userAdminModel->updateView(...);
view_html->('header');
view_html->('userAdmin.updateView',success);
view_html->('footer');
}
}
The controller invokes the method based on the users action. This method performs any business logic requesting any persistent data from the database using the model. The data is then passed into the view which is effectively a templating engine which has rudimentary scripting to iterate through such data.
The brilliance of the MVC model is that a developer can create the logic that passes the expected data to the view. A HTML/CSS developer (for example) with limited coding skills can then create the front end which uses variables passed in without any concern over how the data is generated and has been manipulated.
The model allows you to access the data within the controller in a standardised way without worrying how that data is stored.
I would suggest googling MVC for a more indepth look.