Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.
NHibernate<br />by Andriy Buday		<br />
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:...
Q1: What is NHibernate?NHibernate is…<br />ORM<br />New level of Abstraction<br />http://andriybuday.blogspot.com/<br />Pa...
Q2: Why Do I need it?Because…<br />http://andriybuday.blogspot.com/<br />Development speed increases<br />Reducing databas...
Q2: Why Do I need it?Why do I like NHibernate?<br />http://andriybuday.blogspot.com/<br />Mature<br />Free & Open Source<b...
Q3: How does it look like?Concepts: Unit of Work<br />http://andriybuday.blogspot.com/<br />Kind of “workspace” within whi...
Q3: How does it look like?Concepts: ISession<br />http://andriybuday.blogspot.com/<br />Open a session<br />We get it from...
Q3: How does it look like?Concepts: ISessionFactory<br />http://andriybuday.blogspot.com/<br />We are getting our session ...
Q3: How does it look like?Concepts: Summary<br />http://andriybuday.blogspot.com/<br />Configuration Class builds Session ...
Q3: How does it look like?Architecture: and look from inside?<br />http://andriybuday.blogspot.com/<br />
Q3: How does it look like?DEMO: Hello World!<br />http://andriybuday.blogspot.com/<br />Just basics… to refresh our minds ...
Q4: How can I proceed with it? More on Query API<br />http://andriybuday.blogspot.com/<br />Criteria API<br />Object orien...
Q4: How can I proceed with it? Mapping<br />http://andriybuday.blogspot.com/<br /><hibernate-mappingxmlns="urn:nhibernate-...
Q4: How can I proceed with it? Fetch strategies<br />http://andriybuday.blogspot.com/<br />Lazy loading<br />Default for a...
Q4: How can I proceed with it? Caching, batching, concurrency<br />http://andriybuday.blogspot.com/<br />First level cache...
Q4: How can I proceed with it?<br />http://andriybuday.blogspot.com/<br />Use FluentNhibernate to do not bore developers w...
DEMO 2: Mapping, Querying, LazyLoad, etc..<br />http://andriybuday.blogspot.com/<br />Damn it! Let’s write some code again...
Read blog! Follow me! Visit LVIV .NET UG!<br />http://andriybuday.blogspot.com/<br />http://andriybuday.blogspot.com/<br /...
Links<br />http://andriybuday.blogspot.com/<br />My blog:<br />http://andriybuday.blogspot.com<br />Lviv .NET User Group:<...
Prochain SlideShare
Chargement dans…5
×

NHibernate

2 482 vues

Publié le

NHibernate is mature ORM. This presentation goes through main concepts of it. Was used at Lviv .NET User Group.

Publié dans : Technologie

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 />http://andriybuday.blogspot.com/<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 />http://andriybuday.blogspot.com/<br />Paradigm mismatch<br />
  4. 4. Q2: Why Do I need it?Because…<br />http://andriybuday.blogspot.com/<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 />http://andriybuday.blogspot.com/<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 http://ormbattle.net/ 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 />http://andriybuday.blogspot.com/<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 />http://andriybuday.blogspot.com/<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 />http://andriybuday.blogspot.com/<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 />http://andriybuday.blogspot.com/<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 />http://andriybuday.blogspot.com/<br />
  11. 11. Q3: How does it look like?DEMO: Hello World!<br />http://andriybuday.blogspot.com/<br />Just basics… to refresh our minds <br />
  12. 12. Q4: How can I proceed with it? More on Query API<br />http://andriybuday.blogspot.com/<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 />http://andriybuday.blogspot.com/<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 />http://andriybuday.blogspot.com/<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 />http://andriybuday.blogspot.com/<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 />http://andriybuday.blogspot.com/<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 />http://andriybuday.blogspot.com/<br />Damn it! Let’s write some code again! <br />
  18. 18. Read blog! Follow me! Visit LVIV .NET UG!<br />http://andriybuday.blogspot.com/<br />http://andriybuday.blogspot.com/<br />@andriybuday<br />andriybuday@gmail.com<br />http://dotnetug-lviv.blogspot.com/<br />
  19. 19. Links<br />http://andriybuday.blogspot.com/<br />My blog:<br />http://andriybuday.blogspot.com<br />Lviv .NET User Group:<br />http://dotnetug-lviv.blogspot.com/<br />NHibernate:<br />Home Page: http://nhibernate.com/<br />Reference: http://www.nhforge.org/doc/nh/en/index.html<br />Great series of screencasts on NHibernate:<br />http://www.summerofnhibernate.com/<br />ORM with NHibernate screen cast (2 hours)<br />http://www.flux88.com/uploads/ORMWithNHibernateScreenCast.wmv<br />Tools:<br />Profiler: http://nhprof.com/<br />Fluent NHibernate: http://fluentnhibernate.org/<br />MyGeneration: http://www.mygenerationsoftware.com/<br />

×