Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Lviv .Net User Group. NHibernate


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Lviv .Net User Group. NHibernate

  1. 1. NHibernate<br />by Andriy Buday <br />
  2. 2. Outline<br />Q1: What is NHibernate?<br />Q2: Why do I need it?<br />Q3: How does it look like?<br />Concepts<br />DEMO 1: Hello World!<br />Q4: How can I proceed with it?<br />Queries<br />More about mapping, querying, usage patterns<br />DEMO 2: Customer-Orders-Products<br /><br />Let see… will we get what we want!<br />
  3. 3. Q1: What is NHibernate?NHibernate is…<br />ORM<br />New level of Abstraction<br /><br />Paradigm mismatch<br />
  4. 4. Q2: Why Do I need it?Because…<br /><br />Development speed increases<br />Reducing database work<br />Optimizes access to DB, so could even perform better than plain ADO.NET<br />You think in terms of business model, not database<br />More time for real programming<br />
  5. 5. Q2: Why Do I need it?Why do I like NHibernate?<br /><br />Mature<br />Free & Open Source<br />Good community support<br />Flexible<br />Allows you do Domain Driven Design<br />You asked for comparison with other ORM…<br />LINQ to SQL doesn’t really lead you to good models<br />Entity Framework has better Linq and it is Microsoft<br />Visit for performance numbers<br />Google.Search(string.Format“Nhibernate vs. {0}”, yourORM);<br />
  6. 6. Q3: How does it look like?Concepts: Unit of Work<br /><br />Kind of “workspace” within which you can do anything you want<br />A Unit Of Work either success or fails as a unit<br />Conceptually like database transactions<br />In NHibernate we have ISession which does the same<br />Conceptually we have the same for in ADO.NET as DataSet<br />
  7. 7. Q3: How does it look like?Concepts: ISession<br /><br />Open a session<br />We get it from the session factory<br />We do some work<br />Session.Add(…)<br />Session.Delete(…)<br />Commit the session<br />Session.Flush(…)<br />
  8. 8. Q3: How does it look like?Concepts: ISessionFactory<br /><br />We are getting our session through this class<br />Very expensive to create<br />One per database or application<br />
  9. 9. Q3: How does it look like?Concepts: Summary<br /><br />Configuration Class builds Session Factory<br />Session Factory builds Session<br />Session is used as Unit Of Work to interaction with objects and persist to underlying database<br />var sessionFactory = newConfiguration().Configure().BuildSessionFactory(); <br />using (var session = sessionFactory.OpenSession())<br />{<br />var customer = newCustomer();<br /> session.SaveOrUpdate(customer);<br /> session.Flush();<br /> }<br />
  10. 10. Q3: How does it look like?Architecture: and look from inside?<br /><br />
  11. 11. Q3: How does it look like?DEMO: Hello World!<br /><br />Just basics… to refresh our minds <br />
  12. 12. Q4: How can I proceed with it? More on Query API<br /><br />Criteria API<br />Object oriented querying<br />ICriteria chaining<br />Easy to write<br />Hard to read<br />HQL API<br />String based<br />Parameterized<br />Similar to SQL<br />Speak in terms of Objects<br />More flexible<br />“from Customer c where<br />c.FirstName = :firstName”<br />session.CreateCriteria(typeof(Customer))<br /> .Add(Restrictions.Eq("FirstName", "Hello"))<br />
  13. 13. Q4: How can I proceed with it? Mapping<br /><br /><hibernate-mappingxmlns="urn:nhibernate-mapping-2.2"default-lazy="true"assembly="LearningNHibernate"namespace="LearningNHibernate"><br />  <classname="Customer"table="Customer"xmlns="urn:nhibernate-mapping-2.2"><br />    <idname="CustomerId"column="CustomerId"type="Int32"><br />      <generatorclass="identity" /><br />    </id><br />    <propertyname="FirstName"column="Firstname"length="50"type="String"><br />      <columnname="Firstname" /><br />    </property><br />    <propertyname="LastName"column="Lastname"length="50"type="String"><br />      <columnname="Lastname" /><br />    </property><br />  </class><br /></hibernate-mapping><br />publicclassCustomerMap : ClassMap<Customer><br /> {<br />public CustomerMap()<br /> {<br /> WithTable("Customer");<br /> Id(x => x.CustomerId);<br /> Map(x => x.FirstName, "Firstname");<br /> Map(x => x.LastName, "Lastname").WithLengthOf(50);<br /> }<br /> }<br />
  14. 14. Q4: How can I proceed with it? Fetch strategies<br /><br />Lazy loading<br />Default for all collections<br />Members are loaded from database when assessed<br />Usually good for performance<br />Session has to stay OPEN<br />Eager Fetching<br />Use when you sure you’ll use data<br />OUTER joins<br />
  15. 15. Q4: How can I proceed with it? Caching, batching, concurrency<br /><br />First level cache<br />Following will run only one SELECT<br />Second level cache<br />Within two sessions you still run one SELECT<br />It is needed to explicitly turn it on everywhere<br />Batch processing<br /><property name="adonet.batch_size">250</property><br />Concurrency<br /><version name="Version" column="Version"/> <br />var customer1 = session.Get<Customer>(5);<br />var customer2 = session.Get<Customer>(5);<br />
  16. 16. Q4: How can I proceed with it?<br /><br />Use FluentNhibernate to do not bore developers with XML<br />Create wrappers to have NHibernateRepository<br />Implement UnitOfWork<br />VS projects<br />Two ways for kick-off<br />From database to Objects<br />From objects to Database<br />
  17. 17. DEMO 2: Mapping, Querying, LazyLoad, etc..<br /><br />Damn it! Let’s write some code again! <br />
  18. 18. Read blog! Follow me! Visit LVIV .NET UG!<br /><br /><br />@andriybuday<br /><br /><br />
  19. 19. Links<br /><br />My blog:<br /><br />Lviv .NET User Group:<br /><br />NHibernate:<br />Home Page:<br />Reference:<br />Great series of screencasts on NHibernate:<br /><br />ORM with NHibernate screen cast (2 hours)<br /><br />Tools:<br />Profiler:<br />Fluent NHibernate:<br />MyGeneration:<br />