exploring the nhibernate ecosystem steve bohlen e-mail: [email protected] blog: twitter: @sbohlen
TRANSCRIPT
![Page 1: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/1.jpg)
Exploring the NHibernate EcosystemSteve BohlenE-Mail: [email protected]: http://blog.unhandled-exceptions.comTwitter: @sbohlen
![Page 2: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/2.jpg)
Steve BohlenNearly 20 years developing software
LISP, Pascal, C/C++, VB, VB.NET, C#
Co-Founder, NYC Alt.Net User Group http://nyalt.net
Contributor: various OSS projects
http://www.summerofnhibernate.com
blog: http://blog.unhandled-exceptions.com
e-mail: [email protected]
twitter: @sbohlen
![Page 3: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/3.jpg)
![Page 4: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/4.jpg)
Oredev2009: Efficiency
Relational Persistence
Object Relational Mapping with NHibernate
Persistence Framework
NHibernate-based Frameworks
NHibernateAdd-ins
![Page 5: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/5.jpg)
Coming Up: A Tour
Malmo
![Page 6: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/6.jpg)
…Not a Deep Dive
![Page 7: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/7.jpg)
The NHibernate Ecosystem
Mapping the Universe
![Page 8: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/8.jpg)
NHibernate Core
ExternalNHContrib
NH Spatial
NH Burrow
NH Linq
FluentNH
Castle ActiveRecord
NH Validator
NH Proxy Gen
Rhino Tools
Castle NH Facility
uNHAddins
NH Caches
NH Mapping Attributes
NH Shards
NH Search
Castle ActiveWriter
JetDriver
Lambda Extensions
NH Prof
![Page 9: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/9.jpg)
Non-Relational Data Sources
Mapping, Configuration, and Query Infrastructure and Frameworks
Relational Data Sources
NHibernate Core
NHSpatial
NHSearch
NH LINQ
FluentNH
Castle ActiveRecord
NH Validator
JetDriver
Rhino Tools
Castle NH Facility
NHBurrow
NH Attribute Mapping
uNHAddins
NH Caches
Lambda Extensions
![Page 10: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/10.jpg)
Rhino.ToolsNHibernate Implementation Framework (plus a lot more)
![Page 11: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/11.jpg)
A Complete Infrastructure Stack
Unit-of-Work Abstraction IoC Container Convenience Services
Assumes Castle Windsor NH Session lifecycle management for
ASP.NET apps Conversation-per-Business-Transaction NHRepository<T> implementation Multiple, concurrent DB support Lots more
![Page 12: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/12.jpg)
Rhino IRepository<T>public interface IRepository<T>{ // Methods long Count(); long Count(DetachedCriteria criteria); T Create(); DetachedCriteria CreateDetachedCriteria(); DetachedCriteria CreateDetachedCriteria(string alias); void Delete(T entity); void DeleteAll(); void DeleteAll(DetachedCriteria where); object ExecuteStoredProcedure(string sp_name, params Parameter[] parameters); ICollection<T2> ExecuteStoredProcedure<T2>(Converter<IDataReader, T2> converter, string sp_name, params
Parameter[] parameters); bool Exists(); bool Exists(DetachedCriteria criteria); ICollection<T> FindAll(params ICriterion[] criteria); ICollection<T> FindAll(Order order, params ICriterion[] criteria); ICollection<T> FindAll(Order[] orders, params ICriterion[] criteria); ICollection<T> FindAll(DetachedCriteria criteria, params Order[] orders); ICollection<T> FindAll(string namedQuery, params Parameter[] parameters); ICollection<T> FindAll(int firstResult, int numberOfResults, params ICriterion[] criteria); ICollection<T> FindAll(DetachedCriteria criteria, int firstResult, int maxResults, params Order[] orders); ICollection<T> FindAll(int firstResult, int numberOfResults, Order selectionOrder, params ICriterion[]
criteria); ICollection<T> FindAll(int firstResult, int numberOfResults, string namedQuery, params Parameter[]
parameters); ICollection<T> FindAll(int firstResult, int numberOfResults, Order[] selectionOrder, params ICriterion[]
criteria); T FindFirst(params Order[] orders); T FindFirst(DetachedCriteria criteria, params Order[] orders); T FindOne(params ICriterion[] criteria); T FindOne(DetachedCriteria criteria); T FindOne(string namedQuery, params Parameter[] parameters); FutureValue<T> FutureGet(object id); FutureValue<T> FutureLoad(object id); T Get(object id); T Load(object id); ICollection<ProjT> ReportAll<ProjT>(ProjectionList projectionList); ICollection<ProjT> ReportAll<ProjT>(DetachedCriteria criteria, ProjectionList projectionList); ICollection<ProjT> ReportAll<ProjT>(ProjectionList projectionList, params ICriterion[] criterion); ICollection<ProjT> ReportAll<ProjT>(ProjectionList projectionList, params Order[] orders); ICollection<ProjT> ReportAll<ProjT>(ProjectionList projectionList, bool distinctResults); ICollection<ProjJ> ReportAll<ProjJ>(string namedQuery, params Parameter[] parameters); ICollection<ProjT> ReportAll<ProjT>(DetachedCriteria criteria, ProjectionList projectionList, params Order[]
orders); ICollection<ProjT> ReportAll<ProjT>(ProjectionList projectionList, Order[] orders, params ICriterion[]
criteria); ProjT ReportOne<ProjT>(DetachedCriteria criteria, ProjectionList projectionList); ProjT ReportOne<ProjT>(ProjectionList projectionList, params ICriterion[] criteria); T Save(T entity); T SaveOrUpdate(T entity); T SaveOrUpdateCopy(T entity); void Update(T entity);}
![Page 13: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/13.jpg)
uNhAddinsAbstractions, Tools, and a WHOLE lot more!
![Page 14: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/14.jpg)
uNhAddins: a Smörgåsbord!
NH UserTypes
NH Event Listeners
Inflector
NH Session Abstraction
Query Pagination
Tolerant Query Cache
Conversation-Per-Business Transaction
http://unhaddins.googlecode.com
NH Audit Event Listeners
IoC Container Abstraction
Castle Windsor Adapter
Spring.NET Adapter
Ninject Adapter
NH Session Mgt for WCF
NH Session Mgt for WPF
Validation Abstraction
NH Validator Adapter
Data Annotations
Adapter
Castle Validator Adapter
Validation Ent. Application
Block Adapter
![Page 15: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/15.jpg)
NHibernate CachesEfficient Database Caching
![Page 16: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/16.jpg)
Cache Providers MemCache
Implementation for MemCached http://memcached.googlecode.com
Prevalence Bamboo.Prevalence engine http://bbooprevalence.sourceforge.net
SharedCache Inspired by MemCached but 100% managed code (C#) http://www.sharedcache.com
Velocity Microsoft’s Distributed Caching Engine (CTP2)
SysCache ASP.NET Cache Provider
SysCache2 ASP.NET Cache Provider
○ with SQLServer call-back-invalidate support
![Page 17: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/17.jpg)
Castle ActiveRecordSimpler Data Access
![Page 18: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/18.jpg)
ActiveRecord Example[ActiveRecord]
public class Category : ActiveRecordBase
{
[PrimaryKey]
public int Id { get; set; }
[Property]
public string Name { get; set; }
[BelongsTo("parent_id")]
public Category Parent { get; set; }
[HasMany]
public IList<Category> SubCategories { get; set; }
}
![Page 19: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/19.jpg)
NHibernate ValidatorIntegrated Validation Framework
![Page 20: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/20.jpg)
Using NHValidator
1. Get and Build it (NHContrib)
2. Add References
3. Register Event Listeners in code or hibernate.cfg.xml file
4. Off and Running!
![Page 21: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/21.jpg)
NHibernate Validator Demo
Let’s Look at Some Code!
![Page 22: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/22.jpg)
NHLambdaExtensionsDeath to String-Literals!!!!
![Page 23: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/23.jpg)
Using NHLambdaExtensions
1. Download the Assembly (googlecode) Add Reference to Assembly
2. Off and Running!
![Page 24: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/24.jpg)
LambdaExtensions In Action
session.CreateCriteria<Customer>().Add(Restrictions.Eq(“Firstname”, “Steve”).List<Customer>();
session.CreateCriteria<Customer>().Add<Customer>(c => c.Firstname ==
“Steve”).List<Customer>();
![Page 25: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/25.jpg)
NHLINQ(not LINQ to
Nhibernate…yet!)
One Query Language to Rule Them All!
![Page 26: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/26.jpg)
Using NHLINQ
1. Download the Assembly (sourceforge) v1.0 NH 2.1GA release
2. Add Reference to Assembly
3. Off and Running!
![Page 27: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/27.jpg)
NHLINQ in Actionusing (var session = sessionFactory.OpenSession()){
using (var tx = session.BeginTransaction()){
var customers = session.Linq<Customer>().Where(c => c.Firstname == “Steve”);
foreach (var customer in customers){
Console.WriteLine(customer.Firstname);}
tx.Commit();}
}
![Page 28: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/28.jpg)
NHibernate BurrowStateful NHibernate Session Management for ASP.NET WebForms
![Page 29: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/29.jpg)
Using Burrow
1. Get it and Build it (NHContrib)
2. Add References
3. Add NHibernate.Burrow config section to web.config
4. Add Burrow HTTP Module to your web.config
![Page 30: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/30.jpg)
Modify web.config for Burrow<configSections> <section name="NHibernate.Burrow“
type="NHibernate.Burrow.Configuration.Nhibern ateBurrowCfgSection, NHibernate.Burrow" />
</configSections>
<NHibernate.Burrow> <persistenceUnits> <add name="PersistenceUnit1" nh-config-file
= “~/hibernate.cfg.xml“ /> </persistenceUnits></NHibernate.Burrow>
![Page 31: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/31.jpg)
Register Burrow HTTPModule
<httpModules> <add
name="NHibernate.Burrow.WebUtil.HttpModule” type="NHibernate.Burrow.WebUtil.WebUtilHTTPModule,NHibernate.Burrow.WebUtil"/>
</httpModules>
![Page 32: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/32.jpg)
Burrow Conversation PatternBurrowFramework bf = new BurrowFramework();bf.CurrentConversation.SpanWithPostBacks(Transaction
Strategy.BusinessTransaction);
//do a bunch of work in a bunch of postbacks
BurrowFramework bf = new BurrowFramework();bf.CurrentConversation.FinishSpan(); //commit to DB…
bf.CurrentConversation.GiveUp(); //…or abandon!
![Page 33: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/33.jpg)
NHibernate SpatialSpatial Queries
![Page 34: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/34.jpg)
Understanding Spatial Data
Latitude / LongitudeCoordinate Systems (Spatial Reference ID)SRID Projections
![Page 35: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/35.jpg)
Supported Spatial Engines MS SQLServer 2008
Includes SQLServer 2008 Express! MySQL PostGIS (PostGre-based) Oracle (work-in-progress)
![Page 36: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/36.jpg)
Using NH Spatial
1. Get and Build it (NHContrib)
2. Add References (GeoAPI, Spatial, etc.)
3. Change Dialect in hibernate.cfg.xml
4. Optional: add support for spatial metadata to the Configuration instance before building SessionFactory
5. Map properties as ‘Geometry Type’
6. Off and Running!
![Page 37: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/37.jpg)
NHSpatial: Dialect<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory name="NHibernate.Test">
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">
Server=(local);initial catalog=nhibernate;Integrated Security=SSPI
</property>
<property name="adonet.batch_size">10</property>
<property name="show_sql">false</property>
<property name="dialect“>
NHibernate.Spatial.Dialect.MsSql2008SpatialDialect, NHibernate.Spatial.MsSql2008
</property>
<property name="use_outer_join">true</property>
<property name="command_timeout">60</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<property name="proxyfactory.factory_class">
NHibernate.ByteCode.LinFu.ProxyFactoryFactory,NHibernate.ByteCode.LinFu
</property>
</session-factory>
</hibernate-configuration>
![Page 38: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/38.jpg)
Add Spatial Metadata ClassesConfiguration cfg = new Configuration();cfg.Configure();
Metadata.AddMapping(cfg, MetadataClass.GeometryColumn);
Metadata.AddMapping(cfg, MetadataClass.SpatialReferenceSystem);
var sessionFactory = cfg.BuildSessionFactory();
//rest of your app here!
![Page 39: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/39.jpg)
Add Geometry Type + Mappingusing GeoAPI.Geometriespublic class MyThing{
public virtual IGeometry Geometry {get;set;}//more of our class
}
<!-- short version --><property name="Geometry" column="the_geom" type =
"NHibernate.Spatial.Type.GeometryType, NHibernate.Spatial" />
<!-- long version --><property name="Geometry" column="the_geom">
<type name = "NHibernate.Spatial.Type.GeometryType, NHibernate.Spatial">
<param name="srid">4326</param><param name="subtype">POLYGON</param>
</type></property>
![Page 40: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/40.jpg)
Perform Spatial Queriesvar country = session.CreateCriteria<Country>()
.Add(SpatialExpression.Contains("Boundaries", new Point(-70.40, -33.24))).UniqueResult<Country>();
IList<Town> towns = session.CreateCriteria<Town>() .Add(SpatialExpression.Filter("Boundaries", new Envelope(-70, -68, -32, -34)))
.Add(Restrictions.Not(SpatialExpression.Contains("Boundaries", new Point(-70.40, -33.24)))).List<Town>();
![Page 41: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/41.jpg)
NHibernate SearchQuerying Unstructured Text Indices
![Page 42: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/42.jpg)
The Power of Lucene.NET Databases are efficient and querying
relational data Databases are inefficient at querying
unstructured text Better tools exist to do that
Lucene.NET○ A port of the Lucene project to .NET○ High-performance indexed searching of text
content
![Page 43: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/43.jpg)
NHibernate Search
NHibernate Query
Lucene.NET Document Index
Relational Database
Select all Customers who have more than 10 ordersand whose comments on their Invoices contain the word “’dissatisfied”
![Page 44: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/44.jpg)
Using NHSearch
1. Get and build it (NHContrib)
2. Add References
3. Add index-related properties to hibernate.cfg.xml
4. Register Ins, Upd, Del event listeners to trigger updates to index on change
5. Add attributes to your classes to indicate what should be indexed
6. Off and Running!
![Page 45: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/45.jpg)
Modify Configuration File<?xml version="1.0" encoding="utf-8"?><hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" > <session-factory name="NHibernate.Test"> <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> <property name="connection.connection_string"> Server=(local);initial catalog=nhibernate;Integrated Security=SSPI </property> <property name="adonet.batch_size">10</property> <property name="show_sql">false</property> <property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property> <property name="use_outer_join">true</property> <property name="command_timeout">60</property> <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property> <property name="proxyfactory.factory_class"> NHibernate.ByteCode.LinFu.ProxyFactoryFactory,NHibernate.ByteCode.LinFu </property> <property name=“hibernate.search.default.directory_provider”> NHibernate.Search.Store.FSDirectoryProvider, NHibernate.Search </property> <property name=“hibernate.search.default.indexBase”>c:\MyIndex</property> <property name=“hibernate.search.indexing_strategy”>event</property> </session-factory></hibernate-configuration>
![Page 46: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/46.jpg)
Register Event Listeners<!-- register in hibernate.cfg.xml file --><listener class = “NHibernate.Search.Event.FullTextIndexEventListener,
NHibernate.Search” type=“post-insert”/><listener class = “NHibernate.Search.Event.FullTextIndexEventListener,
NHibernate.Search” type=“post-update”/><listener class = “NHibernate.Search.Event.FullTextIndexEventListener,
NHibernate.Search” type=“post-delete”/>
//register in codevar cfg = new Configuration();cfg.SetListener(NHibernate.Event.ListenerType.PostUpdate, new
FullTextIndexEventListener());cfg.SetListener(NHibernate.Event.ListenerType.PostInsert, new
FullTextIndexEventListener());cfg.SetListener(NHibernate.Event.ListenerType.PostDelete, new
FullTextIndexEventListener());
![Page 47: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/47.jpg)
Add Attributes for Index Engine
public class Document{[DocumentId]public virtual int Id { get; set; }
[Field(Index.Tokenized, Store=Store.Yes)]public virtual string Title { get; set; }
[Field(Index.Tokenized)]public virtual string Body { get; set; }
}
![Page 48: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/48.jpg)
Perform Indexed Queriesusing (var session = sessionFactory.OpenSession()){
using(var textsearch = Search.CreateFullTextSession(session)){
using (var tx = session.BeginTransaction()) {
var results = textsearch
.CreateFullTextQuery<Document>(“Title:Oredev") .SetMaxResults(10).List<Document>();
}}
}
![Page 49: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/49.jpg)
Fluent NHibernateMapping and Configuration without XML
![Page 50: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/50.jpg)
Using FluentNHibernate
1. Get it ( http://fluentnhibernate.org )
2. Add References
3. Off and Running!
![Page 51: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/51.jpg)
Sample Classes
![Page 52: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/52.jpg)
XML Mappings<?xml version="1.0" encoding="utf-8" ?><hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="FluentNHibernateDemo" namespace="FluentNHibernateDemo" >
<class name="Customer"> <id name="Id" column="CustomerId" type="integer"> <generator class="native" /> </id> <property name="Firstname" type="string"/> <property name="Lastname" type="string"/> <set name="Orders" table="`Order`" generic="true"
inverse="true"> <key column="CustomerId"/> <one-to-many class="Order"/> </set> </class></hibernate-mapping>
![Page 53: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/53.jpg)
Fluent Mappings public class CustomerMap : ClassMap<Customer>
{public CustomerMap(){Id(c => c.Id).Column("CustomerId");Map(c => c.Firstname);Map(c => c.Lastname);HasMany<Order>(c => c.Orders).Table("Order").KeyColumn("CustomerId").Inverse().Generic();
} }
![Page 54: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/54.jpg)
XML Configuration<?xml version="1.0" encoding="utf-8"?><hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" > <session-factory name="NHibernate.Test"> <property
name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> <property name="connection.connection_string"> Server=(local)\sqlserver2005;initial
catalog=FluentNHibernateDemo;user=sa;password=password </property> <property name="adonet.batch_size">10</property> <property name="show_sql">true</property> <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> <property name="use_outer_join">true</property> <property name="command_timeout">60</property> <property name="query.substitutions">true 1, false 0, yes 'Y', no
'N'</property> <property
name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<mapping assembly="FluentNHibernateDemo"/> </session-factory></hibernate-configuration>
![Page 55: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/55.jpg)
Fluent ConfigurationsessionFactory = Fluently.Configure()
.Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2005.ConnectionString(CONNSTRING).AdoNetBatchSize(10).ProxyFactoryFactory
<NHibernate.ByteCode.Castle.ProxyFactoryFactory>().UseOuterJoin()).Mappings(m => m.FluentMappings.AddFromAssemblyOf<CustomerMap>()).BuildSessionFactory();
![Page 56: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/56.jpg)
Convention Mapping
Enables Fluent NHibernate to ‘infer’ your mappings from your objects
Uses conventionsBaked into FNHOverrides provided by yourselfIdentity field conventionMany-to-many intermediate table conventionForeign-key id column conventionMany, many more
![Page 57: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/57.jpg)
NH ProfProduction-Class Profiling for ORMs
![Page 58: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/58.jpg)
Metrics, Analysis, Recommendations
![Page 59: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/59.jpg)
Summary If you’re doing your data-access by hand…
YOU’RE DOING IT WRONG If you’re doing the rest of the stuff you saw
here today by hand…YOU’RE DOING IT WRONG
NHibernate has one of the richest ecosystems of extensions, frameworks, and tools of any .NET technology, OSS or otherwise…LEARN TO LEVERAGE THEM for EFFICIENCY
![Page 60: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/60.jpg)
*NHibernate 2.1.1 GA Released!*
November 1, 2009 Probably the final 2.x release before 3.0 Primarily bug-fix, no breaking changes Most of these tools will work with 2.1.1
Most will need to be recompiled against the new release before use○ Binary dependency on NH assemblies
HORNGET.NET is your friend here!
![Page 61: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/61.jpg)
Resources NHForge
http://www.nhforge.org NHContrib
http://sourceforge.net/projects/nhcontrib uNhAddins
http://uNhAddins.googlecode.com NHProf
http://www.nhprof.com HornGet.NET
http://www.hornget.net
![Page 62: Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: Twitter: @sbohlen](https://reader030.vdocuments.mx/reader030/viewer/2022012906/56649e555503460f94b4ca86/html5/thumbnails/62.jpg)
~fini~