Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exploring the NHibernate Ecosystem Steve Bohlen Blog:

Similar presentations


Presentation on theme: "Exploring the NHibernate Ecosystem Steve Bohlen Blog:"— 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

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

5 Coming Up: A Tour

6 …Not a Deep Dive

7 Mapping the Universe

8 NHibernate Core External NHContrib 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

9 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

10 NHibernate Implementation Framework (plus a lot more)

11 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 implementation  Multiple, concurrent DB support  Lots more

12 Rhino IRepository public interface IRepository { // 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 ExecuteStoredProcedure (Converter converter, string sp_name, params Parameter[] parameters); bool Exists(); bool Exists(DetachedCriteria criteria); ICollection FindAll(params ICriterion[] criteria); ICollection FindAll(Order order, params ICriterion[] criteria); ICollection FindAll(Order[] orders, params ICriterion[] criteria); ICollection FindAll(DetachedCriteria criteria, params Order[] orders); ICollection FindAll(string namedQuery, params Parameter[] parameters); ICollection FindAll(int firstResult, int numberOfResults, params ICriterion[] criteria); ICollection FindAll(DetachedCriteria criteria, int firstResult, int maxResults, params Order[] orders); ICollection FindAll(int firstResult, int numberOfResults, Order selectionOrder, params ICriterion[] criteria); ICollection FindAll(int firstResult, int numberOfResults, string namedQuery, params Parameter[] parameters); ICollection 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 FutureGet(object id); FutureValue FutureLoad(object id); T Get(object id); T Load(object id); ICollection ReportAll (ProjectionList projectionList); ICollection ReportAll (DetachedCriteria criteria, ProjectionList projectionList); ICollection ReportAll (ProjectionList projectionList, params ICriterion[] criterion); ICollection ReportAll (ProjectionList projectionList, params Order[] orders); ICollection ReportAll (ProjectionList projectionList, bool distinctResults); ICollection ReportAll (string namedQuery, params Parameter[] parameters); ICollection ReportAll (DetachedCriteria criteria, ProjectionList projectionList, params Order[] orders); ICollection ReportAll (ProjectionList projectionList, Order[] orders, params ICriterion[] criteria); ProjT ReportOne (DetachedCriteria criteria, ProjectionList projectionList); ProjT ReportOne (ProjectionList projectionList, params ICriterion[] criteria); T Save(T entity); T SaveOrUpdate(T entity); T SaveOrUpdateCopy(T entity); void Update(T entity); }

13 Abstractions, Tools, and a WHOLE lot more!

14 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

15 Efficient Database Caching

16 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

17 Simpler Data Access

18 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; } }

19 Integrated Validation Framework

20 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!

21 Let’s Look at Some Code!

22 Death to String-Literals!!!!

23 Using NHLambdaExtensions 1. Download the Assembly (googlecode) Add Reference to Assembly 2. Off and Running!

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

25 One Query Language to Rule Them All!

26 Using NHLINQ 1. Download the Assembly (sourceforge) v1.0  NH 2.1GA release 2. Add Reference to Assembly 3. Off and Running!

27 NHLINQ in Action using (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(); } }

28 Stateful NHibernate Session Management for ASP.NET WebForms

29 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

30 Modify web.config for Burrow

31 Register Burrow HTTPModule

32 Burrow Conversation Pattern BurrowFramework bf = new BurrowFramework(); bf.CurrentConversation.SpanWithPostBacks(TransactionStrat egy.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!

33 Spatial Queries

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

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

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

37 NHSpatial: Dialect NHibernate.Driver.SqlClientDriver Server=(local);initial catalog=nhibernate;Integrated Security=SSPI 10 false NHibernate.Spatial.Dialect.MsSql2008SpatialDialect, NHibernate.Spatial.MsSql2008 true 60 true 1, false 0, yes 'Y', no 'N' NHibernate.ByteCode.LinFu.ProxyFactoryFactory,NHibernate.ByteCode.LinFu

38 Add Spatial Metadata Classes Configuration cfg = new Configuration(); cfg.Configure(); Metadata.AddMapping(cfg, MetadataClass.GeometryColumn); Metadata.AddMapping(cfg, MetadataClass.SpatialReferenceSystem); var sessionFactory = cfg.BuildSessionFactory(); //rest of your app here!

39 Add Geometry Type + Mapping using GeoAPI.Geometries public 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>

40 Perform Spatial Queries var 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("Bounda ries", new Point(-70.40, -33.24)))).List<Town>();

41 Querying Unstructured Text Indices

42 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

43 NHibernate Search NHibernateQuery Lucene.NET Document Index Relational Database Select all Customers who have more than 10 orders and whose comments on their Invoices contain the word “’dissatisfied”

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

45 Modify Configuration File NHibernate.Driver.SqlClientDriver Server=(local);initial catalog=nhibernate;Integrated Security=SSPI 10 false NHibernate.Dialect.MsSql2000Dialect true 60 true 1, false 0, yes 'Y', no 'N' NHibernate.ByteCode.LinFu.ProxyFactoryFactory,NHibernate.ByteCode.LinFu NHibernate.Search.Store.FSDirectoryProvider, NHibernate.Search c:\MyIndex event

46 Register Event Listeners //register in code var 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());

47 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; } }

48 Perform Indexed Queries using (var session = sessionFactory.OpenSession()) { using(var textsearch = Search.CreateFullTextSession(session)) { using (var tx = session.BeginTransaction()) { var results = textsearch.CreateFullTextQuery (“Title:Oredev").SetMaxResults(10).List (); }

49 Mapping and Configuration without XML

50 Using FluentNHibernate 1. Get it ( http://fluentnhibernate.org ) 2. Add References 3. Off and Running!

51 Sample Classes

52 XML Mappings

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

54 XML Configuration NHibernate.Driver.SqlClientDriver Server=(local)\sqlserver2005;initial catalog=FluentNHibernateDemo;user=sa;password=password 10 true NHibernate.Dialect.MsSql2005Dialect true 60 true 1, false 0, yes 'Y', no 'N' NHibernate.ByteCode.Castle.ProxyFactoryF actory, NHibernate.ByteCode.Castle

55 Fluent Configuration sessionFactory = Fluently.Configure().Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration.Ms Sql2005.ConnectionString(CONNSTRING).AdoNetBatchSize(10).ProxyFactoryFactory <NHibernate.ByteCode.Castle.ProxyFactoryFactory>().UseOuterJoin()).Mappings(m => m.FluentMappings.AddFromAssemblyOf<CustomerMap>()).BuildSessionFactory();

56 Convention Mapping  Enables Fluent NHibernate to ‘infer’ your mappings from your objects  Uses conventions Baked into FNH Overrides provided by yourself Identity field convention Many-to-many intermediate table convention Foreign-key id column convention Many, many more

57 Production-Class Profiling for ORMs

58 Metrics, Analysis, Recommendations

59 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

60 *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!

61 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

62 ~fini~


Download ppt "Exploring the NHibernate Ecosystem Steve Bohlen Blog:"

Similar presentations


Ads by Google