1 / 62

Exploring the NHibernate Ecosystem

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

perry
Download Presentation

Exploring the NHibernate Ecosystem

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Exploring the NHibernate Ecosystem Steve Bohlen E-Mail: sbohlen@gmail.com Blog: http://blog.unhandled-exceptions.com Twitter: @sbohlen

  2. 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

  3. Oredev2009: Efficiency NHibernate Add-ins NHibernate-based Frameworks Object Relational Mapping with NHibernate Persistence Framework Relational Persistence

  4. Coming Up: A Tour Malmo

  5. …Not a Deep Dive

  6. Mapping the Universe The NHibernate Ecosystem

  7. 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

  8. 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

  9. NHibernate Implementation Framework (plus a lot more) Rhino.Tools

  10. 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

  11. 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); }

  12. Abstractions, Tools, and a WHOLE lot more! uNhAddins

  13. 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

  14. Efficient Database Caching NHibernate Caches

  15. 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

  16. Simpler Data Access Castle ActiveRecord

  17. 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; } }

  18. Integrated Validation Framework NHibernateValidator

  19. Using NHValidator • Get and Build it (NHContrib) • Add References • Register Event Listeners • in code or hibernate.cfg.xmlfile • Off and Running!

  20. Let’s Look at Some Code! NHibernateValidator Demo

  21. Death to String-Literals!!!! NHLambdaExtensions

  22. Using NHLambdaExtensions • Download the Assembly (googlecode) Add Reference to Assembly • Off and Running!

  23. LambdaExtensions In Action session.CreateCriteria<Customer>() .Add(Restrictions.Eq(“Firstname”, “Steve”) .List<Customer>(); session.CreateCriteria<Customer>() .Add<Customer>(c => c.Firstname == “Steve”) .List<Customer>();

  24. One Query Language to Rule Them All! NHLINQ (not LINQ to Nhibernate…yet!)

  25. Using NHLINQ • Download the Assembly (sourceforge) • v1.0  NH 2.1GA release • Add Reference to Assembly • Off and Running!

  26. 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(); } }

  27. StatefulNHibernate Session Management for ASP.NET WebForms NHibernate Burrow

  28. 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

  29. 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>

  30. Register Burrow HTTPModule <httpModules> <add name="NHibernate.Burrow.WebUtil.HttpModule” type="NHibernate.Burrow.WebUtil.WebUtilHTTPModule,NHibernate.Burrow.WebUtil"/> </httpModules>

  31. 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!

  32. Spatial Queries NHibernate Spatial

  33. Understanding Spatial Data Latitude / Longitude Coordinate Systems (Spatial Reference ID) SRID Projections

  34. Supported Spatial Engines • MS SQLServer 2008 • Includes SQLServer 2008 Express! • MySQL • PostGIS (PostGre-based) • Oracle (work-in-progress)

  35. 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!

  36. 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>

  37. 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!

  38. 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>

  39. 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>();

  40. Querying Unstructured Text Indices NHibernate Search

  41. 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

  42. 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

  43. 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!

  44. 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>

  45. 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());

  46. 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; } }

  47. 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>(); } } }

  48. Mapping and Configuration without XML Fluent NHibernate

  49. Using FluentNHibernate • Get it ( http://fluentnhibernate.org ) • Add References • Off and Running!

More Related