MVC and command pattern - model-view-controller

Ok, this is kinda dumb. I'm trying to wrap my head around the use of
the MVC and Command Patterns. The questions basically concern where to
place code.
In the MVC pattern, where are the Commands instantiated?
Are they created by the Controller, or are they contained fully in the Model or neither.
BTW, Should one be using the Command pattern if there is no need of undo/redo.
Regards

While there are many variations based on the needs/complexity of an application, you typically find them implemented in the Controller. Here is a great article on using the Command pattern in an MVC architecture. I'm not sure what type of application you are building, but here are a few more resources that show implementations on different application stacks.
http://java.sun.com/blueprints/guidelines/designing_enterprise_applications_2e/web-tier/web-tier5.html
http://msdn.microsoft.com/en-us/library/ff647590.aspx
http://www.phpwact.org/pattern/model_view_controller

To implement Command pattern in an MVC application depends on your specific need. Controller does not create any Command by default. If you need to implement command pattern in a MVC application you have to do it on your own.
You can either implement the commands in the Controller or can do in a separate Service layer. It should not be in Model as Command pattern is behavioural and should be independent of model.

Related

Multiple or single controller in Spring MVC?

What is the ratio in defining multiple controllers (in Spring MVC) instead of just one that matches all paths?
Could you write me also a use case scenario?
Following the suggestion of #Lyju, the number of Controller in a Spring MVC application is a personal choice, but it is better to follow the single responsability principle: every module should have responsibility over a single part of the functionality provided by the software.
For furhter details:
https://en.wikipedia.org/wiki/Single_responsibility_principle

When to use models vs service in Laravel?

I have been using for a while but still I am confuse on what to include in my controllers specifically "When should I use Model and When to Use Service"?
I am confused if I am making the right thing.
I would suggest to never use models directly into the controllers.. Adopt the repository-pattern which enables the use of dependency injection of services into the controller.. Use Service Layer to contain the business logic.. In this way all the related codes would be onto similar layer.
Reference for repository pattern:
http://heera.it/laravel-repository-pattern#.VuJcVfl97cs
Please don't use model directly in your controller, unless if you are building a very simple application but for a large application, it would be good to use a repository. Kindly take a look at this tutorial https://bosnadev.com/2015/03/07/using-repository-pattern-in-laravel-5/

Entity Framework, LINQ and patterns

I've started using EF and LINQ in a project and I'm trying to decide on the best approach/pattern to use. Until now I've been using a custom persistence framework that was based on DataSets and XML configuration. Basically it was a VS Custom Tool that would read the XML configuration file and the DataSets and would generate Object Oriented classes with all the necessary properties/associations/methods. This auto-generated classes then were used from the UI and I had the flexibility to expose only what the UI would need.
Now with EF and LINQ, I'm not comfortable with the idea that the UI can use directly the auto-generated classes and all the LINQ stuff. It seems that this approach would have a very tight integration between UI and the database.
So I'm looking for some pattern that would "hide" all the EF and LINQ goodies and basically limit what the UI can do. Is there any standard way to do this?
What you're looking for is an n-tier application. It's not so much a pattern as an architecture. You break your app up into 2 or more pieces, typically 3 composed of UI, business and Data. You might implement this through other patterns such as the Facade or Repository patterns to keep a strong seperation of concerns.
You might also use a Service Layer, which could be implemented by a facade or as a web service.
You would, ideally, pass data through objects called DTO's or Data Transfer Objects, and you might adapt those DTO's by using a view model in your UI (not to be confused with MVVM which another poster erroneously mentioned.)
Beyond that, much of it depends on the type of app you're buiding. Desktop app, server app, web app, etc..
The pattern you're looking for is, in general, Model-View-ViewModel, or MVVM.
Here's a tutorial that seems to hit on the high points of the design pattern: http://csharperimage.jeremylikness.com/2010/04/model-view-viewmodel-mvvm-explained.html

Can the presenter in Model-View-Presenter do non-UI actions/logic?

I am wondering if the Presenter in Model-View-Presenter is allowed to do logic/actions that are non-UI centric? Is MVP solely for UI and data or can I use it for a project where I need to write to a file (via the controller / the presenter)?
What's actually the difference between the presenter and the controller?
Controller is usually associated with MVC pattern, Presenter always with MVP. There are so many flavors. Remember, goal of MVP is to allow for testing and to decouple dependencies. How you achieve it is up to you. I for example delegate all data related operation to DataService that is injected to Presenter. If you need some file writing, you better inject that service, otherwise, good luck with testing it...

What is an MVC framework and why is it necessary/useful?

I know that an MVC framework allows you to separate business logic, data base access and presentation, but why do we need a framework to do this.
Can't we just keep our classes separated, perhaps using different packages/folders for the model, view and controller classes?
In my opinion the thing you are talking about is the MVC pattern and not a specific framework. Of course you can go and keep all your classes within one project and still use the MVC pattern, as you have all your UI code in the views, the logic in the controllers, ...
A MVC framework on the other hand makes it easier for you to use this pattern. It may provide some base classes for controllers and a mechanism for the communication between view and controller.
I don't know if you are familiar with ASP.NET MVC. The framework itself is very small, but it helps you developing an application with the MVC pattern, as you don't have do think about the previously decribed areas...
Hope this helps
An MVC framework is a framework written to split up the the business logic, database access and presentation.
This is very useful in most web applications, and now lately into software/desktop applications.
This is due to the fact that following the MVC model, your code will be much clearer, cleaner and you keep your application DRY (Do not Repeat Yourself).
You can write your own classes and separate them into Model, View and Control. But again, you will need a framework to help you in accomplishing certain tasks. Like a List control in ASP.NET, or PHP framework being able to help you translate text between languages and so on. (Oh why reinvent the wheel?!)
MVC and framework is a different thing. MVC is just an architectural pattern, which can be applied with any project, with or without framework.
So you don't need a framework to do this. You can separate them by yourself. :)
MVC stands for “MODEL” “VIEW” “CONTROLLER”. ASP.NET MVC is an architecture to develop ASP.NET web applications in a different manner than the traditional ASP.NET web development. Web applications developed with ASP.NET MVC are even more SEO (Search Engine) friendly.
Developing ASP.NET MVC application requires Microsoft .NET Framework 3.5 or higher.
Model:
MVC model is basically a C# or VB.NET class.
A model is accessible by both controller and view.
A model can be used to
pass data from Controller to view.
A view can use model to display
data in page.
View:
View is an ASPX page without having a code behind file.
All page specific HTML generation and formatting can be done inside view.
One can use Inline code (server tags ) to develop dynamic pages.
A request to view (ASPX page) can be made only from a controller’s action method
Controller:
Controller is basically a C# or VB.NET class which inherits system.mvc.controller.
Controller is a heart of the entire MVC architecture.
Inside Controller’s class action methods can be implemented which are responsible for responding to browser OR calling views.
Controller can access and use model class to pass data to views
Controller uses ViewData to pass any data to view.
MVC is a code organization architecture style to organize your code-logic in a meaningful way for web applications. As a programmer I have almost puked when I have inherited other people's code when their code logic is all over the place and following their web application code turns into following a rabbit down the gutter hole. Why MVC? hmm.. well why should I use a filing cabinet or folders to organize my plethora of paper and not just have my papers stashed in a large pile and have others figure how they connect to each other. It increases code readability. With MVC it becomes very easy to follow code logic since you are following standard structure for a web application. Business logic is separated out from UI. Easier to delegate work decouple work on a project.
You can of course approach it yourself by segregating your classes. A framework supplies common scaffolding that you wouldn't have to build yourself. But it will also impose some structure on your code. You'll have to evaluate whether the framework helps more than it hurts.
You are correct, there are strategies that you can implement to help with separation of concerns without using MVC.
Microsoft's ASP.NET MVC framework is one strategy that can be employed, and that is what I think you are asking about.
This MVC framework makes such separation of concerns easy.
The other major advantage of MVC is testability - (depends on whether you believe in unit testing - I do).
The MVC framework ensures that all orchestration logic is on your controllers and through the FormControls collection allows full unit testing of all aspects of your application except for how it is presented.
As the MS MVC framework encourages adherence to common rules and structure of the application which should lead to greater maintainability.
The major downside of MVC is the code-in-front code weaving required for presentation, but this can be easily overcome.
Perhaps this is just a linguistic thing. I've seen "frameworks" referring to themselves as a DSL -- Domain Specific Language.
And you don't need a framework But here's something to consider: You already know for a web app you're going to want to do a few common things... route URLs, render pages, etc. Why re-write it all? For other problem domains you'll have generic things to do as well.
Hai Friends There are somemany types of architecture frame work has been there,firstly i know 2tier and 3 tier frame work ,the 3 tier and mvc ,entity framework are same but in different name's,so study a good background in any one architecture there fore if you went to any multinational companies ,you can easly score/highlight to your carrer.
Model View Controller or MVC as it is popularly called, is a software design
pattern for developing web applications. A Model View Controller pattern is made
up of the following three parts:
**Model** - The lowest level of the pattern which is responsible for maintaining data.
**View** - This is responsible for displaying all or a portion of the data to the user.
**Controller** - Software Code that controls the interactions between the Model and View

Resources