1 / 21

Object Relational Mapping with NHibernate

Object Relational Mapping with NHibernate. Ben Scheirman Principal Consultant Sogeti flux88.com. Why Objects?. Take advantage of . NET Complex business logic is easier to model IntelliSense Fluid interfaces. Focus on Business Logic FIRST. Paradigm Mismatch. Objects are hierarchical

duante
Download Presentation

Object Relational Mapping with NHibernate

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. Object Relational Mapping with NHibernate Ben Scheirman Principal Consultant Sogeti flux88.com

  2. Why Objects? • Take advantage of .NET • Complex business logic is easier to model • IntelliSense • Fluid interfaces Focus on Business Logic FIRST

  3. Paradigm Mismatch • Objects are hierarchical • Databases are relational

  4. The problem of Granularity User ? Address

  5. The problem of Inheritance BillingAccount ? CreditCard CheckingAccount

  6. Why NHibernate? • Mature • Good community support • Good direction • Flexible • Can do POCO

  7. NHibernate in a Nutshell • ISessionFactory • ISession • ITransaction

  8. Hello World with NHibernate Demo time

  9. Key concepts • ISessionFactory is expensive • ISession should be use-case bounded • Open at the start of a unit of work • Close at the end

  10. NHibernate Architecture Business Layer Persistent Classes IInterceptor ILifecycle IUserType IValidatable Persistence Layer ISessionFactory ISession ITransaction IQuery Configuration ADO.NET

  11. How do you GLUE the objects and the database? Foo.cs .NET Object Foo.hbm.xml Mapping XML

  12. Did I just say XML?

  13. Mapping Collections

  14. Unidirectional One to many A Blog has many Posts Blog IList<Post> Posts; Post 1 * Blog.hbm.xml <bag name="Posts"> <key column="BlogId" /> <one-to-many class="Post" /> </bag>

  15. Bidirectional Many-to-Many A category has items, an item has categories Item ISet Categories Category ISet Items * * Item.hbm.xml <set name="Categories" table="Item_Categories"> <key column="ItemId" /> <many-to-many class="Category" /> </set> Category.hbm.xml <set name=“Items" table="Item_Categories“ inverse=“true”> <key column=“CategoryId" /> <many-to-many class=“Item" /> </set> What is INVERSE?

  16. Understanding INVERSE Post p = new Post(); Category cat = new Category(); p.Categories.Add(cat); cat.Posts.Add(p); UPDATE Post_Categories(PostId, CategoryId) VALUES(@p, @c) UPDATE Post_Categories(PostId, CategoryId) VALUES(@p, @c) One side needs to “Manage” the association

  17. Understand Lazy-Loading • Default Behavior • Lazy for Objects • Lazy for Collections

  18. Blogging with NHibernate Demo time

  19. What’s the first step? • The object model, of course! Blog Post * Categories * 1 * 1 * Comments

  20. ASP.NET ISession Management • Session-per-Request pattern Page IIS A S P .N E T Http-Module A web request Create / Destroy Use HttpContext ISession

  21. Resources • ayende.com/blog • Hibernate in Action • NHibernate in Action (pre-release PDFs) • NHibernate Forums • Castle Project (Active Record)

More Related