1 / 12

NHibernate in Action

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:

ksena
Download Presentation

NHibernate in Action

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. NHibernate in Action Web Seminar at UMLChina By Pierre Henri Kuaté 2008/08/27 http://www.manning.com/kuate/

  2. 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

  3. 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

  4. 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

  5. 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

  6. Table • Set of data • Contains columns and rows • Supports the relational algebra • Effective tools for data manipulation

  7. Entities are best for modeling a business domain

  8. Tables are best for efficient persistence

  9. 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; } }

  10. 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)

  11. 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.

  12. 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

More Related