NHibernate in Action Web Seminar at UMLChina By
NHibernate in Action Web Seminar at UMLChina By Pierre Henri Kuaté 2008/08/27 http: //www. manning. com/kuate/
Object/Relational Mapping (ORM) �Persistence technique �Allows Object-Oriented Programming �Supports Relational Database �Enables many positive design principles: ◦ Separation of concerns ◦ Domain Driven Development ◦ Test Driven Development
NHibernate �ORM library implemented using. NET �Port of the Java library called Hibernate �Database-independent (almost) �Provides most advanced features �Free, open source and mature
Persistence �Make data outlives the execution of the program that created it �Commonly known as CRUD operations CRUD = Create, Retrieve, Update, Delete �Manipulates entities in an objectoriented language �Data persisted in tables of a relational database
Entity �Object-oriented class (inheritance and polymorphism) �Encapsulates fields using properties �Use methods to implement business logic �No constraint on the structure (Data. Set is not an entity) �Effective tools for software engineering
Table �Set of data �Contains columns and rows �Supports the relational algebra �Effective tools for data manipulation
Entities are best for modeling a business domain
Tables are best for efficient persistence
NHibernate Configuration Process Provide information: ◦ Database connection details ◦ Entities mapping Mapping of entities to tables Using. NET attributes: [Class] public class Animal { Auto generate the database tables with one line of code Use a NHibernate session to access the database: [Id(Name="Id")] [Generator(1, Class="native")] public int Id { get; set; } using(ISession session = session. Factory. Open. Session()) using(session. Begin. Transaction()) { // Use NHibernate here session. Transaction. Commit(); } [Property] public string Name { get; set; } }
Object/Relational Mapping brings the best of both worlds �Save entities (Object-oriented; no SQL!) var eagle = new Animal(2, "Eagle", "2 paws", "2 wings"); nhibernate. Session. Save(eagle); �Load entities var eagle = nhibernate. Session. Get<Animal>(2); �Using queries ◦ HQL: Hibernate Query Language ◦ QBC: Query by Criteria ◦ Linq for NHibernate (in beta)
Linq for NHibernate �Language � “Adds Integrated Query (LINQ) native data querying capabilities to. NET languages using a syntax reminiscent of SQL” var result = from lb in _session. Linq<Living. Being>() where lb. Name. Contains("e") orderby lb. Name select lb; � Potentially faster than hand-coded SQL commands due to caching, batching, lazy loading and other performance optimizations.
To Learn More �NHibernate Website: http: //www. nhibernate. org/ �NHibernate Resources: http: //www. hibernate. org/365. html �NHibernate in Action: http: //www. manning. com/kuate/ �NHibernate Forum: http: //forum. hibernate. org/viewforum. php? f= 25
- Slides: 12