• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
NHibernate and Friends - HDC2010
 

NHibernate and Friends - HDC2010

on

  • 1,455 views

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

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

Statistics

Views

Total Views
1,455
Views on SlideShare
1,448
Embed Views
7

Actions

Likes
0
Downloads
21
Comments
0

2 Embeds 7

http://www.linkedin.com 5
https://www.linkedin.com 2

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • 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 - - Lazy Loading - - Eager Loading
  • Repository Pattern

NHibernate and Friends - HDC2010 NHibernate and Friends - HDC2010 Presentation Transcript

  • NHibernate & Friends
    Lee Brandt
  • Code Monkey
    User Group Mentor (KS, MO)
    Visual C#
    @leebrandt
    http://www.leebrandt.me
  • Agenda
    Nhibernate
    Configuration
    Mapping
    Querying
    Fluent Nhibernate
    Configuration
    Mapping
    Linq2NHibernate
    Querying
    One, Big, Happy F@mily
  • 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
  • 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>
  • 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>
  • Fluent NHibernate
    Fluent Interfaces
    Write Mappings & Configuration in Code
    Automapping
    Convention over configuration
    Added Beer
  • 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();
  • 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
  • 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);
    }
    }
  • 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
  • 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’
  • Putting It All Together
  • Overall Architecture
    Interface
    NHibernate
    Repository
    Interface
    Typed Linq
    Repository
    Accessor
    Type
    Database
  • Application
    IWeaponRepository
    IVictimRepository
    Linq Weapon
    Repository
    IKillerRepository
    Linq
    Victim
    Repository
    Linq
    Killer
    Repository
    IRepository
    NHibernate
    Repository
    Database
  • Application
    IRepository<Victim>
    IRepository<Weapon>
    IRepository<Killer>
    IRepository<T>
    NHibernate
    Repository
    Database
  • Testability
  • 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();
    }
  • Let’s See it In Action
    DEMO!