Using Spring and Hibernate
Using Spring and Hibernate
05/05/12
G53ELC
05/05/12
G53ELC
05/05/12
G53ELC
DI is all about wiring up objects or plumbing if you prefer It is about ensuring loose coupling and fits well with design patterns Design to an interface and then inject the actual class at run time This is what inheritence is really for
05/05/12
G53ELC
Stock Quotes
Authenticator
Error Handler
Logger
Database
This example was originally created by Jim Weirich in Ruby on his blog.
05/05/12 G53ELC 5
G53ELC
}
05/05/12 G53ELC 6
05/05/12
G53ELC
05/05/12
G53ELC
G53ELC
public MyLocator() { dict.Add(typeof(ILogger), new Logger()); dict.Add(typeof(IErrorHandler), new ErrorHandler(this)); dict.Add(typeof(IQuotes), new StockQuotes(this)); dict.Add(typeof(IDatabase), new Database(this)); dict.Add(typeof(IAuthenticator), new Authenticator(this)); dict.Add(typeof(WebApp), new WebApp(this)); } }
05/05/12 G53ELC 9
Good things
G53ELC
Classes are decoupled from explicit imlementation types Easy to externalise the configuration
05/05/12
G53ELC
11
Gets rid of the dependency on the ILocator Object is no longer responsible for finding its dependencies The container does it for you
05/05/12
G53ELC
12
Then what?
Write your objects the way you want G53ELC Setup the container Ask the container for objects The container creates objects for you and fulfills dependencies
05/05/12
G53ELC
13
05/05/12
G53ELC
14
Creating Objects
G53ELC [Test] public void WebAppTest() { DIContainer container = GetContainer(); IStockQuotes quotes = container.Get<IStockQuotes>(); IAuthenticator auth = container.Get<IAuthenticator>(); Assert.IsNotNull( quotes.Logger ); Assert.IsNotNull( auth.Logger ); Assert.AreSame( quotes.Logger, auth.Logger ); }
05/05/12 G53ELC 15
Existing Frameworks
G53ELC
Java
Python
PyContainer
.NET Ruby
Rico Copland
05/05/12
G53ELC
16
05/05/12
G53ELC
17
05/05/12
G53ELC
18
G53ELC
05/05/12
G53ELC
19
G53ELC
05/05/12
Tutor.hbm.xml
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="elc.Tutor" table="tutor"> <id name="id" type="String" column="ID" > <generator class="assigned"/> </id> <property name="firstName"> <column name="FIRSTNAME" /> </property> <property name="surname"> <column name="SURNAME"/> </property> </class> </hibernate-mapping>
05/05/12 G53ELC
G53ELC
21
hibernate.cfg.xml
G53ELC
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url"> jdbc:mysql://localhost/hibernatetutorial</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password"></property> <property name="hibernate.connection.pool_size">10</property> <property name="show_sql">true</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.hbm2ddl.auto">update</property> <!-- Mapping files --> <mapping resource="tutor.hbm.xml"/> </session-factory> </hibernate-configuration>
05/05/12
G53ELC
22
05/05/12
G53ELC
23
Also saveTutor(); deleteTutor() etc. CRUD is now done with single lines of code. No SQL needed
05/05/12 G53ELC 24
05/05/12
G53ELC
25
Any Questions?
G53ELC Im confused
05/05/12
G53ELC
26
G53ELC
05/05/12
G53ELC
27
05/05/12
G53ELC
28
Annotations
G53ELC There has been a revolution in Server-side Java and Tiger Jane 1.5: EJB
05/05/12
G53ELC
29
05/05/12
G53ELC
30
The Concept
G53ELC
Entity bean
corresponds to a set of records in a database
Session bean
handles business flow (one per client unless stateless, when may be shared)
05/05/12
G53ELC
31
Enterprise JavaBeans
Definition from OReillys Enterprise JavaBeans book
G53ELC
05/05/12
G53ELC
32
client
security
JNDI
JTS
..
33
05/05/12
G53ELC
return results
return results
05/05/12
G53ELC
A Taxonomy of EJBs
G53ELC
EJB
Entity Session
Bean managed
Container managed
stateless
stateful
05/05/12
G53ELC
35
Entity Bean
Clients
RDBMS
05/05/12
G53ELC
36
05/05/12
G53ELC
37
Many clients can access it Implements business logic One bean can service multiple clients (fast and efficient) Lifetime controlled by container No state across methods Each instance identical upon creation Short-lived
05/05/12 G53ELC 38
EJB Server
05/05/12 G53ELC 39
Entity EJBs
G53ELC
developer writes JDBC code and SQL statements Container generates methods to read and write to the database dropped! Takes necessary information from the Deployment Descriptor
05/05/12
G53ELC
40
05/05/12
G53ELC
41
G53ELC
05/05/12
G53ELC
42
G53ELC
05/05/12
G53ELC
43
05/05/12
G53ELC
44
05/05/12
G53ELC
45
05/05/12
G53ELC
46