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