AppFuse Document (4) - Create Manager

xiaoxiao2021-03-06  47

Create Manager

This chapter will show you how to create business facade that can interact with the DAO layer and can handle transactions.

This part of the content relies on Part i: Create DAO.

About this chapter

This section will show you how to create a Business Façade class (and a JUnit Test) that is interactive with DAO created by the first part.

In Appfuse, it is called a Manager class. Its main responsibility is to serve as a bridge between the persistence layer (DAO) and the web layer. It is also used to decouple the representation layer and database layer (such as: swing program). Manager is also where you place business logic.

Let's get started from creating a ManagerTest and Manager!

content

· [1] Create a ManagerTest to execute the JUnit test Manager

· [2] Create a Manager called Dao

· [3] Configuring Spring for Manager and Transactions

· [4] Run ManagerTest

Create a ManagerTest to execute the JUnit test Manager

In the first part, we have created a Person object and a personDao interface - we will continue to develop this example here. First, we created a JUnit Test for PersonManager. Create a PersonManagerTest in the TEST / Service / ** / service directory. We want to test the same basic methods as in DAO (GET, SAVE, REMOVE).

This class should inherit BaseManagerTestCase already existing in the Servic package.

BaseManagerTestCase provides the same function as BaseDaOTestCase - loaded a property file with your * Test.class as the same name, and also initializes Spring's ApplicationContext.

I often copy (open → saving as) an existing test (such as userManagerTest.java), then use [PP] Erson Find / Replace [UU] Ser.

The following code is a basic JUnit test for our Manager needs. This code will be simply created and destroy the PersonManager. "CTX" object is BASEMANAGERTESTCASE

Initialization.

package org.appfuse.service; import org.appfuse.model.Person; import org.springframework.dao.DataAccessException; public class PersonManagerTest extends BaseManagerTestCase {private Person person = null; private PersonManager mgr = null; protected void setUp () throws Exception {Super.Setup (); Mgr = ("PersonManager");} protected void teardown () throws exception {super.teardown () (); mgr = null;}}

Now we have this class's JUnit test framework, now you want to add content: Make sure everything in Manager is working properly. Here is a snippet, which can help you understand what we will do.

.. We created a method starting with "TEST" (all lowercase). These methods are public, returned to Void, no parameters. They will be called by the task of the Ant Build.xml file. Here is some simple test of the test crudation. It is necessary to keep in mind that each method is autonomous ["every method you have to get Person]. Add the following method to your PersonManagerTest.java file:

public void testGetPerson () throws Exception {person = mgr.getPerson ( "1"); assertNotNull (person.getFirstName ());} public void testSavePerson () throws Exception {person = mgr.getPerson ( "1"); person. setFirstName ( "test"); mgr.savePerson (person); assertEquals (person.getFirstName (), "test");} public void testAddAndRemovePerson () throws Exception {person = new Person (); person = (Person) populate ( Person; Mgr.savePerson (Person); Assertequals (Person.GetFirstName (), "Bill"); AssertNotNull (Person.GetId ()); log.debug ("Removing Person, Personid:" person.getid ()) Mgr.removePerson (Person.GetId (). TOSTRING ()); try {person = mgr.getPerson (). Tostring ()); Fail ("Person Found In Database");

} Catch (DataAccessException DAE) {log.debug ("Excection:" dae.getMessage (); assertNotnull (DAE);}}

At this point, this class does not compile success because we didn't create a PersonManager interface.

In order to make the APPFUSE framework is more scalable, I have used many design patterns, which is very interesting. In fact, I have learned a lot within a year, so I don't want to expand their architecture for my previous participation. - I want to rewrite it again! Fortunately, use my insight and experience, you can make Appfuse's trend, but unfortunately: I can only upgrade each year, but I can't rewrite. ;-)

Create a MANager called Dao

We will create a PersonManager.java interface in the src / service / ** / service directory, and then specify a CRUD method for classes that implement this interface. I'VE Eliminated The Javadocs in The Class Below for Display Purposes.

I usually copy [duplicate] (Open → Save as) an already existing file (such as UserManager.java). package org.appfuse.service; import org.appfuse.model.Person; public interface PersonManager {public Person getPerson (String id); public void savePerson (Person person); public void removePerson (String id);}

Now let's create a PersonManagerImpl class, which implements the method of interface PersonManager. To achieve this, we create a new class PersonManagerImpl.java in the directory src / service / ** / service / impl. It inherits BaseManager and implements the PersonManager interface.

package org.appfuse.service.impl; import org.appfuse.model.Person; import org.appfuse.dao.PersonDAO; import org.appfuse.service.PersonManager; public class PersonManagerImpl extends BaseManager implements PersonManager {private PersonDAO dao; public void SetPersondao (Persondao Dao) {this.dao = DAO;} Public Person getPerson (long.valueof (id));} public pers ason (Person Person) {DAO.SAVEPERSON (PERSON); } Public void transoveperson (String ID) {DAO.REMOVEPERSON (long.valueof (id));}}

It should be noted that the setPersondao method. In order to use Spring Bind Persondao to this Manager, we have to configure the ApplicationContext-Service.xml file. We will be configured below. Now run "Ant Compile-Service", you should be able to compile all things ...

Finally, we have to create a PersonManagerTest.Properties file in the directory TEST / Service / ** / Service. This Person = (Person) populate (Person); will work for our testing.

If you have created a Persondaotest.Properties file above, copy, then rename it is a good way. Of course, you can also manually set these properties.

Firstname = BILL

Lastname = Joy

Now we need to modify the spring profile to let it know our new Manager.

Configuring Spring for Manager and Transactions

Notify Spring so that it knows the PersonManager interface and its implementation class. Open file src / service / ** / service / applicationContext-service.xml. Here, you should be able to see the comments defined as "PersonManager" bean. Cancel the comment, or add the following to this file:

"parent" attribute involves a bean definition TransactionProxyFactoryBean

(It has all basic transaction attributes)

Run ManagerTest

Save all modified files and run the ANT Test-Service -dtestcase = PersonManager.

Yeah Baby, Yeah:

Build SuccessFultotal Time: 9 Seconds

The Files That Were Modified and Added to this Point Are Available for Download

.

Next:

Part III: Create an action and JSP -

How to create

Action

with

JSP.

转载请注明原文地址:https://www.9cbs.com/read-54392.html

New Post(0)