NHibernate and Friends - HDC2010

1,566 views
1,436 views

Published on

Using NHibernate Fluent NHibernate and Linq2NHibernate together as your data access layer.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,566
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
24
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Photo: Warner Bros. Pictures
  • ORMs In General - Map Relational Data Tables to Programmable Objects - Ease of use - Keep me out of writing bunch of sprocs for basic operations - Keep me in the Code IDE (Visual Studio) - Make my DAL more testableNHibernate in General - Widest Acceptance/Usage - - Easier to find help when you need it - - Someone’s already done that
  • XML Configuration
  • XML Class Mapping
  • Fluent Configuration
  • Fluent Class Mapping
  • Basics - Create Mappings from POCOs to Match Tables - - XML - - In Code (Fluent) - - Auto Mapping - Set Up Connection to Data Source - - XML - - In Code (Fluent)Make Calls to ORM - Generate SQL (Native) for database queries - - Parameterized SQL - - - Prevent SQL Injection- - CRUD operations already there- - Specific queries - - - Criteria Queries - - - HQL - - - Linq to NHibernate (3.5+) - - - - Lazy Evaluation - - - - IQueryable<T> - - Lazy Loading - - Eager Loading
  • Repository Pattern
  • NHibernate and Friends - HDC2010

    1. 1. NHibernate & Friends<br />Lee Brandt<br />
    2. 2. Code Monkey<br />User Group Mentor (KS, MO)<br />Visual C#<br />@leebrandt<br />http://www.leebrandt.me<br />
    3. 3.
    4. 4. Agenda<br />Nhibernate<br />Configuration<br />Mapping<br />Querying<br />Fluent Nhibernate<br />Configuration<br />Mapping<br />Linq2NHibernate<br />Querying<br />One, Big, Happy F@mily<br />
    5. 5. NHibernate<br />Object-Relational Mapper (ORM)<br />Map Objects in Code to Relational Data<br />Uses XML for Mappings & Configuration<br />Compiles XML with DLLs<br />It’s Still Like Pizza<br />
    6. 6. Configuration<br /> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"><br /> <session-factory><br /> <property name="connection.provider"><br />NHibernate.Connection.DriverConnectionProvider<br /> </property><br /> <property name="connection.driver_class"><br />NHibernate.Driver.SqlClientDriver<br /> </property><br /> <property name="connection.connection_string"><br />FullOnDatabaseConnectionStringLikeYouNormallyPutInTheWebConfig<br /> </property><br /> <property name="dialect"><br />NHibernate.Dialect.MsSql2008Dialect<br /> </property><br /> <property name="proxyfactory.factory_class"><br />NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle<br /> </property><br /> </session-factory><br /> </hibernate-configuration><br />
    7. 7. Mapping<br /><?xml version="1.0" encoding="utf-8" ?> <br /><hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly=“MyApp" namespace=“MyApp.Domain"> <br /> <class name=“Killer" table=“Killers"> <br /> <id name=“KillerId"> <br /> <generator class="guid"/> <br /> </id> <br /> <property name=“Name"/> <br /> <property name=“Bio" type="StringClob"><br /> <column name=“Biography" sql-type="ntext"/><br /> </property> <br /> <set name=“Weapons” > <br /> <key column=“KillerId"/> <br /> <one-to-many class=“Weapon"/> <br /> </set> <br /> <set name=“Victims” ><br /> <key column=“KillerId "/> <br /> <one-to-many class=“Victim"/> <br /> </set> <br /> </class> <br /></hibernate-mapping><br />
    8. 8. Fluent NHibernate<br />Fluent Interfaces<br />Write Mappings & Configuration in Code<br />Automapping<br />Convention over configuration<br />Added Beer<br />
    9. 9. Configuration<br />VB<br />Dim sessionFactory = Fluently.Configure() _<br /> .Database(MsSqlConfiguration.MsSql2008 _<br /> .ConnectionString(GetYouConnectionStringFromWhereEver)) _<br /> .Mappings(Function(m) m.FluentMappings.AddFromAssemblyOf(Of Product)()) _<br /> .BuildSessionFactory()<br />C#<br />varsessionFactory=Fluently.Configure()<br />.Database(SQLiteConfiguration.Standard<br />.ConnectionString(cs=>cs.FromConnectionStringWithKey("Default"))<br /> .Mappings(m =><br />m.AutoMappings.Add(<br />AutoMap.AssemblyOf<Killer>(new AutoMappingConfig())<br />.BuildSessionFactory();<br />
    10. 10. Mapping (Visual Basic)<br />Public Class ProductMap<br /> Inherits ClassMap(Of Product)<br /> Public Sub New()<br /> Table(“People")<br /> Id(Function(m) m.PersonID).GeneratedBy.Assigned()<br /> Map(Function(m) m.Name)<br />Map(Function(m) m.Bio).Column(“Biography”) _<br /> .CustomType(“StringClob”) _<br /> .CustomSqlType(“ntext”)<br />HasManyToMany(Function(m) m.GetWeapons()) _<br /> .Table(“KillerWeapons”) _<br /> .ParentKeyColumn(“KillerId”) _<br /> .ChildKeyColumn(“WeaponId”)<br />HasMany(Function(m) m.GetVictims()) _<br /> .Table(“Victims”) _<br /> .KeyColumn(“KillerId”)<br /> End Sub<br />End Class<br />
    11. 11. Mapping (C#)<br />public class KillerMap : ClassMap<Killer><br /> {<br /> public KillerMap()<br /> {<br /> Table("Killers");<br /> Schema("KillerApp");<br />Id(k =>k.ID).GeneratedBy.Identity();<br /> Map(p =>p.Name).Column(“KillerName");<br /> Map(p =>p.RealName);<br />HasMany(p =>p.Victims).Table("Victims").KeyColumn("Killer_Id");<br />HasMany(p =>p.Weapons);<br /> }<br /> }<br />
    12. 12. LINQ 2 NHibernate<br />Problem with Querying in Nhibernate<br />Au Natural<br />HQL<br />ICriteria<br />LINQ to NHibernate<br />Stay In Code<br />Test Queries Without Touching the Database<br />The Salad<br />
    13. 13. Dim query = Session.CreateQuery(“From People”)<br />Dim products As IList(Of Person) = query.List(Of Person)()<br />var query = Session.CreateQuery(“From People”);<br />var products = query.List<Person>();<br />Dim products = Session.CreateCriteria(Of Person).List(Of Person)()<br />var products = Session.CreateCriteria<Person>().List<Person)();<br />Dim products = Session.Linq(Of Person)()<br />var products = Session.Linq<Person>();<br />SELECT * FROM People<br />var products = Session.Linq<Person>()<br />.Where(person=>person.Name.Contains(“eve”));<br />Dim products = Session.Linq(Of Person)()<br />.Where(Function(p) p.Name.Contains(“eve”))<br />SELECT * FROM Products WHERE Name LIKE ‘%?%’; 0=‘eve’<br />
    14. 14. Putting It All Together<br />
    15. 15. Overall Architecture<br />Interface<br />NHibernate<br />Repository<br />Interface<br />Typed Linq<br />Repository<br />Accessor<br />Type<br />Database<br />
    16. 16. Application<br />IWeaponRepository<br />IVictimRepository<br />Linq Weapon<br />Repository<br />IKillerRepository<br />Linq<br />Victim<br />Repository<br />Linq<br />Killer<br />Repository<br />IRepository<br />NHibernate<br />Repository<br />Database<br />
    17. 17. Application<br />IRepository<Victim><br />IRepository<Weapon><br />IRepository<Killer><br />IRepository<T><br />NHibernate<br />Repository<br />Database<br />
    18. 18. Testability<br />
    19. 19. Testability<br />using (var session = _repository.Session)<br /> {<br />var trans =session.BeginTransaction();<br /> new PersistenceSpecification<Member>(session)<br />.CheckProperty(p =>p.FirstName, "First")<br />.CheckProperty(p =>p.LastName, "Last")<br />.CheckProperty(p =>p.Email, "e@mail.com")<br />.CheckList(p => p.Addresses, _listOfAddresses)<br />.CheckReference(p => p.Company, _company)<br />.VerifyTheMappings();<br />trans.Rollback();<br />}<br />
    20. 20. Let’s See it In Action<br />DEMO!<br />

    ×