Your SlideShare is downloading. ×
NHibernate and Friends - HDC2010
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

NHibernate and Friends - HDC2010

1,237
views

Published on

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

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,237
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
24
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
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
  • Transcript

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