Raible's Wiki
Raible Designs AppFuseHomepage- Korean - Chinese - Italian - Japanese QuickStart Guide User Guide Tutorials Other ApplicationsStruts ResumeSecurity Example Struts Menu
Set your name in
UserPreferences
Referenced by
JSPWiki v2.2.33
Hide Menu |
This is version 1.
It is not the current version, and thus it cannot be edited. Parte II: Creare nuovi Manager - Un HowTo sulla creazione di Business Façade che parlino con il livello del database (DAO) e si occupino della gestione delle transazioni.
Informazioni su questo TutorialQuesto tutorial ti mostrerà come creare una classe di Business Façade (ed un Test JUnit) per parlare con il DAO che abbiamo creato nella Parte I.Nel contesto di AppFuse, questa viene denominata una classe Manager. La sua responsibilità principale è comportarsi da ponte fra il layer di persistenza (DAO) ed il layer web. È anche utile per disaccoppiare il tuo layer di presentazione dal tuo layer database (i.e. per applicazioni Swing). I Manager dovrebbero inoltre essere il luogo deputato nel quale mettere la business logic della tua applicazione.
Iniziamo con il creare un nuovo ManagerTest ed un nuovo Manager nell'architettura di AppFuse. Indice
Crea un nuovo ManagerTest per eseguire test JUnit sul Manager [#1]In Part I, we created a Person object and PersonDao - so let's continue developing this entity. First, let's create a JUnit test for the PersonManager. Create PersonManagerTest in the test/service/**/service directory. We'll want to test the same basic methods (get, save, remove) that our DAO has.
This class should extend BaseManagerTestCase, which already exists in the service package. The parent class (BaseManagerTestCase) serves similar functionality as the BaseDaoTestCase.
The code below is what you need for a basic JUnit test of your Manager. Unlike the DaoTest, this test uses jMock to isolate the Manager from its dependencies and make it a true "unit" test. This can be very helpful because it allows you to test your business logic w/o worrying about other dependencies. The code below simply sets up the Manger and its dependencies (as Mocks) for testing.
Now that you have the skeleton done for this class, you need to add the meat: the test methods to make sure everything works. Here's a snippet from the DAO Tutorial tutorial to help you understand what we're about to do.
Add the following methods to your PersonManagerTest.java file:
This class won't compile at this point because we have not created our PersonManager interface.
Crea un nuovo Manager per parlare con il DAO [#2]First off, create a PersonManager.java interface in the src/service/**/service directory and specify the basic CRUD methods for any implementation classes. I've eliminated the JavaDocs in the class below for display purposes. The setPersonDao() method is not used in most cases - its just exists so the PersonManagerTest can set the DAO on the interface.
Now let's create a PersonManagerImpl class that implements the methods in PersonManager. To do this, create a new class in src/service/**/service/impl and name it PersonManagerImpl.java. It should extend BaseManager and implement PersonManager.
One thing to note is the setPersonDao() method. This is used by Spring to bind the PersonDao to this Manager. This is configured in the applicationContext-service.xml file. We'll get to configuring that in Step 3[3]. You should be able to compile everything now using "ant compile-service". Now you need to edit Spring's config file for our services layer so it will know about this new Manager. Configura Spring per questo Manager e relative Transaction [#3]To notify Spring of this our PersonManager interface and its implementation, open the src/service/**/service/applicationContext-service.xml file. In here, you should see a commented out definition for the "personManager" bean. Uncomment this, or add the following to the bottom of this file.
The "parent" attribute refers to a bean definition for a TransactionProxyFactoryBean that has all the basic transaction attributes set. Esegui il ManagerTest [#4]Save all your edited files and try running ant test-service -Dtestcase=PersonManager. Yeah Baby, Yeah:
BUILD SUCCESSFUL I file che sono stati modificati ed aggiunti fino a questo punto sono disponibili in download. Prossima Puntata: Parte III: Creating Actions and JSPs - A HowTo for creating Actions and JSPs in the AppFuse architecture.
|