120 likes | 234 Views
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:
E N D
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 object-oriented 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 (DataSet 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
NHibernate Configuration Process • Provide information: • Database connection details • Entities mapping • Auto generate the database tables with one line of code • Use a NHibernate session to access the database: using(ISession session = sessionFactory.OpenSession()) using(session.BeginTransaction()) { // Use NHibernate here session.Transaction.Commit(); } Mapping of entities to tables Using .NET attributes: [Class] public class Animal { [Id(Name="Id")] [Generator(1, Class="native")] public int Id { get; set; } [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"); nhibernateSession.Save(eagle); • Load entities var eagle = nhibernateSession.Get<Animal>(2); • Using queries • HQL: Hibernate Query Language • QBC: Query by Criteria • Linq for NHibernate (in beta)
Linq for NHibernate • Language Integrated Query (LINQ) • “Adds native data querying capabilities to .NET languages using a syntax reminiscent of SQL” var result = from lb in _session.Linq<LivingBeing>() 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