Java webservice: use MVC or DAO+managers - java

I'm trying to learn how to develop web-services in java.
Now some tutorials (I'm trying to learn how to do it myself without using a framework) use the MVC pattern and expose the class to create a WSDD file etc., other tutorials go for a DAO + managers.
Now I haven't been able to find an article discussing the differences (asking for up and downsides is probably a bit too simplistic), so I was wondering if anyone here had some insights and maybe some recommended reading (be it books or tutorials on sites) since some sites are a bit contradictory.
Thank you in advance.

DAO + managers is simply how you would implement the model part of your MVC design.
A DAO gives you access to your data store and the DAO, the managers, and all the logic are what make up your model.
The view and controller and then made up of other components and talk to the model to get information from your data store.
WSDD files simply chain these parts together.
You should be able to use both pattern together.

Related

Embedding database operations in our framework

We started writing a Java Framework for our company. But we don't have enough experience about Java. We decided to use JPA framework for database CRUD operations.
What do you suggest about that:
about defining persistence.xml. We search creating dynamic
EntityManager and found some documents but we don't know that is
it best way.
Is it a good way that create a layer over JPA base db operations?
(for example CRUD methods.)
How can we do calling JPA CRUD methods from my CRUD methods in
framework?
We will use this framework for desktop and web applications. Is
deployment a problem for us.
Do we have to use EJB?
Is there alternative to JPA which you suggest? (example: ADF,JDBC)
Thanks
It highly depends on your requirements and what you want to do with your "framework". I do not know enough of your project to give you a real advise, but here are some thoughts:
What do you mean with "framework"? Are you developing a library which other people should use? What should be the purpose of your framework? Is it a data access layer for some of your company data? If so: JPA is a kind of a standard and might be a good fit since it is widely used. If other people should use your "framework" it is good to use something which is standard and used in many other applications and tools.
Do you really need a data access layer on the desktop? Do you have a rich client? It is no problem to just "deploy" the application to the desktop, but a data access layer must always be configured and (maybe) updated. And that's where the pain begins when you use a rich client. Users must configure a database, the database must be installed or accessible remote and the version of the client must match the version of the database. Sooner or later this will hit you.
What else have you considered already? What about a ORM? Hibernate might by a good and popular fit. Also eBeans which is used in Play! is very cool. If you make a CRUD applications, frameworks like eBeans are doing most of the work out-of-the-box for you. You create a model (just POJOs + annotations) and the frameworks provides the complete data access layer (including the database setup).

MVC and Swing in desktop application

After realizing that I have completely ignored the MVC pattern I have tried to utilize the concept in an application with a Swing view. I have now read most of the posts on the subject of MVC with Swing but am still a bit confused, because it is too complicated for me to grasp, and I think I need some basic clarifications so I don't set off on the wrong path.
I also wonder how common it is to use MVC in real projects. Many online tutorials seem to leave out the controller and mix it with the model, while I was confused by XSTL:s business logic capabilities. Why would you want to address a datasource from a JSP view?
These thoughts aside, my proper question is this:
If you have a Swing component, should event listener in that Swing class update the component state through calling (static perhaps?) methods in a POJO controller class, which in turn gets the appropriate business logic from the model, which is made up by POJO class hierarchy and associated persistence?
I've worked as a freelance for a long time and almost 90% of the projects were about Java Swing (Desktop applications). Also a lot of projects involved migration from languages like Visual Fox Pro to Java, it was a pain, because the hard part is not think in the logic which is already done, the hard part is take the code that is a mess and turn it into a good-looking code following the good practices and using design patterns, that's why it is a good idea to make a schema or a map in your mind how you can separate your code following the concepts of Model, View, Controller.
MVC as mentioned helps you to have a good-looking, maintainable and easy to read code, as well as you follow the programming paradigms and good practices.
View: Obviously, the part that interacts with the user (user interface), in case of Swing, your windows, frames, panels and all the code that involves the graphic components you need for your app.
Controller: Involves the core or business logic you stablish for your application, in this "layer" you should include the functionality and the "how my application will achieve the goals?".
Model: Related with the data you manage, for example, your entities and classes that represents the data you want to manage or give maintenance.
Applying MVC is not so hard, but as I mentioned, it could be sometimes a pain when you have to migrate your code from a not-applying-MVC structure to a MVC structured application. It is easier to start coding using MVC.
A way I get used to it is by using maven and separate my application into little "modules", of course, you don't need maven, I just found it useful in that moment, but in any case you can try practicing or get used to MVC by separating your application into little projects, for instance:
Java Project 1: application-data-model (contains all the code related with data management: entities, dtos, beans, daos)
Java Project 2: application-core-controller (contains all the business logic and functionality, you can use a facade pattern here if you want to make your code more "transparent" when you relate with your view)
Java Project 3: application-view-ui (contains all the panels, frames and graphic components)
Working this way helped me (and forced me) to get used to separate my code and keep an eye on what really matters to the project I'm working on. For instance, if I'm on application-data-model I'm focused in data model, I'm not thinking in business logic nor graphic interface.
Long explanation, maybe somebody could do it better, but hope I could have helped you or at least gave you a hand with this.
Best regards.
Firs the URL for basic understanding of MVC
http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
Now the approach to implement it in Swing applications. Don't get confused with controller functionality with the listeners functionality.
UI controls and listeners attached to them should be defined in your view classes.
On any event, whenever you need to invoke a business logic, then you need to call the controller class. Like fetching some value from the database.
Controller class should talk to your model to fetch the data and manipulate it if required.
Model classes should work on the data.
The idea of using MVC is to reduce redundant code and more manageable code. So if you are doing some calculations/manipulations then those can be moved to Controllers. Controllers can be called from different views requiring the same stuff. Similarly model can be used by multiple controllers to fetch the data.

Is it better to use spring mvc with hibernate?

I am going to develop small ERP System. The product has only one major requirement to support multiple databases.
I have planned to use
Front side: JSP+JSTL+JQuery [I have good command on that] + I have create my own custom component for re-useability and full control of my component.
Back-end: Using hibernate[ORM] framework [due to Cross database] and I have also good knowledge of it.
I am happy with above and feeling confident to build product, soon.
BUT
My friend made me unhappy ;)
My friend suggest me that you should use spring MVC with hibernate, because when your product become large,it create a problem in future due to many developers involvement.So, you should use framework , so that every developer follow the pattern and then your product remain stable.
But I am already follow hibernate pattern :)
I have no any knowledge of Spring MVC. When I search about spring MVC , I found it is a different thing than my requirement and also find Cross database context issue, if I use Spring MVC with hibernate.
Either should I used spring MVC or not. If yes, I have to learn then Spring MVC and
I do not want to waste my time, if spring is not suitable for me.
That's why I am consulting that forum , to go for right direction.
Actually, Spring MVC does not interfere with any of the components you have chosen already. It just introduces a way to structure your frontend - backend interface in a standard way (MVC pattern).
I actually run the very same setup and is very happy with it. (Jquery, JSP, Spring MVC, Hibernate).
Other than structure, you will get easy return of invested time in Spring MVC when you want to do Ajax heavy things (as I assume you want in an ERP app). It's trivial to build AJAX/JSON stuff with jQuery and SpringMVC.
Using Spring MVC will make your application structured. This will help in future maintenance.
Your code will look clean and you can separate your code into components and Spring will help you wire them together and you might decide to combine them in different ways, or wish to make it easy to swap out one component for another depending on different settings or environments. This would be great for multiple databases when used with hibernate.
Learning spring will not take much time if you have a good knowledge of Java EE.

Adding business logic in JSP

This has been asked in few times in this thread. But sometimes its hard to make it decouple. Let say Im dealing with session, cookie stuff how do I decouple or basically how do I move the existing code out from the JSP? UseBean will be a good alternative. But how to deal with business logic with lot of HTTPServletRequest and HTTPServerletResponse and its related objects used services?
Thank you
Developing web-applications in Java has evolved quite a lot over the past 10 or so years. PHP-style JSP pages may have been ok-ish in 1999 but not today. At very minimum, you should isolate your business-logic in a servlet and only have your rendering/view in the JSP. That is just the bare-minimum to pass some form of basic level of hygiene. If you are doing any sort of new development or even refactoring, you should look into a more refined MVC/MVP framework. Spring is a good candidate. Maybe not the "coolest" but definitely established and well-known. The tutorial Andrea mentioned is a good place to start. And don't get too nervous about things like "how does it make sure the right request and session is used" - that is what spring-mvc does, it's the core functionality. Go through the tutorials, try it out and then dig deeper to understand what is happening under the hood and how you can customize the behavior to fit your needs.
I suggest you to use a MVC patter to add the business logic to your web application on Java classes instead of add the logic into the JSP.
Take a look to that: http://static.springsource.org/docs/Spring-MVC-step-by-step/part3.html

Three-layered approach in PHP

I liked using three layers (data/business logic|services/ui) in Java and wish to apply this to a web site in PHP. What advice, links, tutorials could you suggest in this respect?
P.S: I'm particularly interested in knowing how the domain/entity objects can be written and used.
You could go with some frameworks of PHP.
They already done three layer for you
ZendFramework
CakePHP
This could be interesting for you and help you to choose right framework.
PHPFrameworks
If you want a PHP framework that actually follows the Model View Controll (MVC) pattern then you will need to look at Agavi. Many other frameworks have attempted to implement it but have failed.
Correct me if I am wrong, but this is an MVC approach (Model->View->Controller)?
If so, checkout a framework like CakePHP, they have a pretty good learning zone for you.
You could look at the MVC pattern used in many frameworks for PHP.
You could use frameworks like Kohana, Zend, CakePHP, Yii, which all uses this pattern.
MVC stands for Model-View-Controller, where the model contains the business logic, the view contains the UI, and the controller handles the requests and ties things together.
Symfony (http://www.symfony-project.org/) gets good reviews from my PHP slinging mates

Categories