620 likes | 816 Views
Exploring the NHibernate Ecosystem. Steve Bohlen E-Mail: sbohlen@gmail.com Blog: http://blog.unhandled-exceptions.com Twitter: @ sbohlen. Steve Bohlen. Nearly 20 years developing software LISP, Pascal, C/C++, VB, VB.NET, C# Co-Founder, NYC Alt.Net User Group http://nyalt.net
E N D
Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: http://blog.unhandled-exceptions.com Twitter: @sbohlen
Steve Bohlen Nearly 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: sbohlen@gmail.com twitter: @sbohlen
Oredev2009: Efficiency NHibernate Add-ins NHibernate-based Frameworks Object Relational Mapping with NHibernate Persistence Framework Relational Persistence
Coming Up: A Tour Malmo
Mapping the Universe The NHibernate Ecosystem
NHContrib External NH Caches Rhino Tools NH Search uNHAddins NH Spatial NH Burrow Castle ActiveRecord NH Shards NH Mapping Attributes Lambda Extensions FluentNH JetDriver Castle NH Facility NH Validator NH Linq NH Prof Castle ActiveWriter NH Proxy Gen NHibernate Core
Non-Relational Data Sources Relational Data Sources NHSpatial JetDriver NHSearch NHibernate Core Mapping, Configuration, and Query Infrastructure and Frameworks Lambda Extensions NH Validator Rhino Tools NH LINQ uNHAddins NH Attribute Mapping Castle NH Facility Castle ActiveRecord NHBurrow NH Caches FluentNH
NHibernate Implementation Framework (plus a lot more) Rhino.Tools
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
Rhino IRepository<T> public interface IRepository<T> { // Methods long Count(); long Count(DetachedCriteria criteria); T Create(); DetachedCriteriaCreateDetachedCriteria(); DetachedCriteriaCreateDetachedCriteria(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(paramsICriterion[] criteria); ICollection<T> FindAll(Order order, paramsICriterion[] criteria); ICollection<T> FindAll(Order[] orders, paramsICriterion[] criteria); ICollection<T> FindAll(DetachedCriteria criteria, params Order[] orders); ICollection<T> FindAll(string namedQuery, params Parameter[] parameters); ICollection<T> FindAll(intfirstResult, intnumberOfResults, paramsICriterion[] criteria); ICollection<T> FindAll(DetachedCriteria criteria, intfirstResult, intmaxResults, params Order[] orders); ICollection<T> FindAll(intfirstResult, intnumberOfResults, Order selectionOrder, paramsICriterion[] criteria); ICollection<T> FindAll(intfirstResult, intnumberOfResults, string namedQuery, params Parameter[] parameters); ICollection<T> FindAll(intfirstResult, intnumberOfResults, Order[] selectionOrder, paramsICriterion[] criteria); T FindFirst(params Order[] orders); T FindFirst(DetachedCriteria criteria, params Order[] orders); T FindOne(paramsICriterion[] 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>(ProjectionListprojectionList); ICollection<ProjT> ReportAll<ProjT>(DetachedCriteria criteria, ProjectionListprojectionList); ICollection<ProjT> ReportAll<ProjT>(ProjectionListprojectionList, paramsICriterion[] criterion); ICollection<ProjT> ReportAll<ProjT>(ProjectionListprojectionList, params Order[] orders); ICollection<ProjT> ReportAll<ProjT>(ProjectionListprojectionList, booldistinctResults); ICollection<ProjJ> ReportAll<ProjJ>(string namedQuery, params Parameter[] parameters); ICollection<ProjT> ReportAll<ProjT>(DetachedCriteria criteria, ProjectionListprojectionList, params Order[] orders); ICollection<ProjT> ReportAll<ProjT>(ProjectionListprojectionList, Order[] orders, paramsICriterion[] criteria); ProjTReportOne<ProjT>(DetachedCriteria criteria, ProjectionListprojectionList); ProjTReportOne<ProjT>(ProjectionListprojectionList, paramsICriterion[] criteria); T Save(T entity); T SaveOrUpdate(T entity); T SaveOrUpdateCopy(T entity); void Update(T entity); }
uNhAddins: a Smörgåsbord! NH Session Mgt for WCF NH Event Listeners Validation Abstraction NH UserTypes NH Session Mgt for WPF NH Validator Adapter Conversation-Per-Business Transaction IoC Container Abstraction Inflector Data Annotations Adapter Castle Windsor Adapter NH Audit Event Listeners NH Session Abstraction Castle Validator Adapter Spring.NET Adapter Validation Ent. Application Block Adapter Tolerant Query Cache Query Pagination Ninject Adapter http://unhaddins.googlecode.com
Efficient Database Caching NHibernate Caches
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
Simpler Data Access Castle ActiveRecord
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; } }
Integrated Validation Framework NHibernateValidator
Using NHValidator • Get and Build it (NHContrib) • Add References • Register Event Listeners • in code or hibernate.cfg.xmlfile • Off and Running!
Let’s Look at Some Code! NHibernateValidator Demo
Death to String-Literals!!!! NHLambdaExtensions
Using NHLambdaExtensions • Download the Assembly (googlecode) Add Reference to Assembly • Off and Running!
LambdaExtensions In Action session.CreateCriteria<Customer>() .Add(Restrictions.Eq(“Firstname”, “Steve”) .List<Customer>(); session.CreateCriteria<Customer>() .Add<Customer>(c => c.Firstname == “Steve”) .List<Customer>();
One Query Language to Rule Them All! NHLINQ (not LINQ to Nhibernate…yet!)
Using NHLINQ • Download the Assembly (sourceforge) • v1.0 NH 2.1GA release • Add Reference to Assembly • Off and Running!
NHLINQ in Action using (var session = sessionFactory.OpenSession()) { using (vartx = session.BeginTransaction()) { var customers = session.Linq<Customer>() .Where(c => c.Firstname == “Steve”); foreach (var customer in customers) { Console.WriteLine(customer.Firstname); } tx.Commit(); } }
StatefulNHibernate Session Management for ASP.NET WebForms NHibernate Burrow
Using Burrow • Get it and Build it (NHContrib) • Add References • Add NHibernate.Burrowconfig section to web.config • Add Burrow HTTP Module to your web.config
Modify web.config for Burrow <configSections> <section name="NHibernate.Burrow“ type="NHibernate.Burrow.Configuration.NhibernateBurrowCfgSection, NHibernate.Burrow" /> </configSections> <NHibernate.Burrow> <persistenceUnits> <add name="PersistenceUnit1" nh-config-file = “~/hibernate.cfg.xml“ /> </persistenceUnits> </NHibernate.Burrow>
Register Burrow HTTPModule <httpModules> <add name="NHibernate.Burrow.WebUtil.HttpModule” type="NHibernate.Burrow.WebUtil.WebUtilHTTPModule,NHibernate.Burrow.WebUtil"/> </httpModules>
Burrow Conversation Pattern BurrowFramework bf = new BurrowFramework(); bf.CurrentConversation.SpanWithPostBacks(TransactionStrategy.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!
Spatial Queries NHibernate Spatial
Understanding Spatial Data Latitude / Longitude Coordinate Systems (Spatial Reference ID) SRID Projections
Supported Spatial Engines • MS SQLServer 2008 • Includes SQLServer 2008 Express! • MySQL • PostGIS (PostGre-based) • Oracle (work-in-progress)
Using NH Spatial • Get and Build it (NHContrib) • Add References (GeoAPI, Spatial, etc.) • Change Dialect in hibernate.cfg.xml • Optional: add support for spatial metadata to the Configuration instance before building SessionFactory • Map properties as ‘Geometry Type’ • Off and Running!
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>
Add Spatial Metadata Classes Configuration cfg = new Configuration(); cfg.Configure(); Metadata.AddMapping(cfg, MetadataClass.GeometryColumn); Metadata.AddMapping(cfg, MetadataClass.SpatialReferenceSystem); varsessionFactory = cfg.BuildSessionFactory(); //rest of your app here!
Add Geometry Type + Mapping using GeoAPI.Geometries public class MyThing { public virtual IGeometryGeometry {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>
Perform Spatial Queries varcountry = 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>();
Querying Unstructured Text Indices NHibernate Search
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
NHibernate Search Select all Customers who have more than 10 orders and whose comments on their Invoices contain the word “’dissatisfied” Relational Database NHibernate Query Lucene.NET Document Index
Using NHSearch • Get and build it (NHContrib) • Add References • Add index-related properties to hibernate.cfg.xml • Register Ins, Upd, Del event listeners to trigger updates to index on change • Add attributes to your classes to indicate what should be indexed • Off and Running!
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>
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 code varcfg = 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());
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; } }
Perform Indexed Queries using (var session = sessionFactory.OpenSession()) { using(vartextsearch = Search.CreateFullTextSession(session)) { using (vartx = session.BeginTransaction()) { var results = textsearch .CreateFullTextQuery<Document>(“Title:Oredev") .SetMaxResults(10) .List<Document>(); } } }
Mapping and Configuration without XML Fluent NHibernate
Using FluentNHibernate • Get it ( http://fluentnhibernate.org ) • Add References • Off and Running!