Colorado Software Summit: October 21 – 26, 2007 © Copyright 2007, Chris Richardson Improving Your Code with POJOs, Dependency Injection, AOP, and O/R Mapping Chris Richardson Author of POJOs in Action Chris Richardson Consulting, Inc http://www.chrisrichardson.net Chris Richardson: Improving Your Code with POJOs, Dependency Injection, AOP, and O/R Mapping Slide 1 Colorado Software Summit: October 21 – 26, 2007 © Copyright 2007, Chris Richardson About Chris (cid:1) Grew up in England (cid:1) Live in Oakland, CA (cid:1) Over twenty years of software development experience (cid:3) Building object-oriented software since 1986 (cid:3) Using Java since 1996 (cid:3) Using J2EE since 1999 (cid:1) Author of POJOs in Action (cid:1) Speaker at JavaOne, JavaPolis, NFJS, JUGs, …. (cid:1) Chair of the eBIG Java SIG in Oakland (www.ebig.org) (cid:1) Run a consulting and training company that helps organizations build better software faster Chris Richardson: Improving Your Code with POJOs, Dependency Injection, AOP, and O/R Mapping Slide 2 Colorado Software Summit: October 21 – 26, 2007 © Copyright 2007, Chris Richardson Overall presentation goal Show how dependency injection, aspect-oriented programming object/relational mapping makes code easier to develop and maintain Chris Richardson: Improving Your Code with POJOs, Dependency Injection, AOP, and O/R Mapping SSlliiddee 33 Colorado Software Summit: October 21 – 26, 2007 © Copyright 2007, Chris Richardson Agenda (cid:1) Tangled code, tight coupling and duplication (cid:1) Using dependency injection (cid:1) Simplifying code with aspects (cid:1) Simplifying DAOs with O/R mapping Chris Richardson: Improving Your Code with POJOs, Dependency Injection, AOP, and O/R Mapping SSlliiddee 44 Colorado Software Summit: October 21 – 26, 2007 © Copyright 2007, Chris Richardson Common pattern: Big Fat Service (cid:1) Services are tightly coupled to other components/infrastructure APIs (cid:1) Services contain a tangle of business logic and infrastructure logic (cid:1) Implementation of infrastructure concerns is scattered/duplicated throughout the service layer (cid:1) Code is difficult to: write, test and maintain (cid:1) Dies with the infrastructure frameworks Chris Richardson: Improving Your Code with POJOs, Dependency Injection, AOP, and O/R Mapping SSlliiddee 55 Colorado Software Summit: October 21 – 26, 2007 © Copyright 2007, Chris Richardson Example banking application Chris Richardson: Improving Your Code with POJOs, Dependency Injection, AOP, and O/R Mapping Slide 6 Colorado Software Summit: October 21 – 26, 2007 © Copyright 2007, Chris Richardson Example design Web Tier AccountServiceDelegate void create (Account) BankingTransaction transfer (fromAccountId , toAccountId , amount) Business Tier AccountService BankingSecurityManager void create (Account) verifyCallerAuthorized () {static } BankingTransaction transfer (fromAccountId , toAccountId , amount) <<singleton>> AuditingManager Account audit() accountId balance JdbcBanking overdraftType JdbcAccountDao TransactionDao Account findAccount (accountId ) addTransaction () <<singleton>> TransactionManager BankingTransaction begin() commit() txnId rollback() date <<singleton>> JdbcConnection Manager getConnection() cleanUp() Chris Richardson: Improving Your Code with POJOs, Dependency Injection, AOP, and O/R Mapping Slide 7 Colorado Software Summit: October 21 – 26, 2007 © Copyright 2007, Chris Richardson Demo (cid:1) Let’s walk through the code Chris Richardson: Improving Your Code with POJOs, Dependency Injection, AOP, and O/R Mapping SSlliiddee 88 Colorado Software Summit: October 21 – 26, 2007 © Copyright 2007, Chris Richardson The good news (cid:1) Code is relatively clean (cid:1) Database access logic is encapsulated by DAOs (cid:1) Other concerns, e.g. transaction management, are implemented by other classes BUT Chris Richardson: Improving Your Code with POJOs, Dependency Injection, AOP, and O/R Mapping SSlliiddee 99 Colorado Software Summit: October 21 – 26, 2007 © Copyright 2007, Chris Richardson Procedural code (cid:1) Anemic Domain Model public class AccountServicelImpl implements AccountService { (cid:3) AccountService = public BankingTransaction transfer(String Business logic fromAccountId, String toAccountId, double amount) { (cid:3) Account and … BankingTransaction = Account fromAccount = accountDao.findAccount(fromAccountId); dumb data objects Account toAccount = (cid:1) Code is more difficult to: accountDao.findAccount(toAccountId); double newBalance = fromAccount.getBalance() – (cid:3) Develop amount; (cid:3) Understand fromAccount.setBalance(newBalance); toAccount.setBalance(toAccount.getBalance() + (cid:3) Maintain amount); …. (cid:3) Test (cid:1) Solution: Attend my other talk Chris Richardson: Improving Your Code with POJOs, Dependency Injection, AOP, and O/R Mapping SSlliiddee 1100
Description: