nhibernate and friends - hdc2010

20
NHibernate & Friends Lee Brandt

Upload: lee-brandt

Post on 14-Nov-2014

1.587 views

Category:

Technology


3 download

DESCRIPTION

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

TRANSCRIPT

Page 1: NHibernate and Friends - HDC2010

NHibernate & FriendsLee Brandt

Page 2: NHibernate and Friends - HDC2010

Visual C#

Code Monkey

User Group Mentor (KS, MO)

@leebrandt

http://www.leebrandt.me

Page 3: NHibernate and Friends - HDC2010

Killer• Killer• Killers

Weapons• IList<Weapon>• Weapons

Victims• IList<Victim>• Victims

Page 4: NHibernate and Friends - HDC2010

Agenda• Nhibernate– Configuration– Mapping– Querying

• Fluent Nhibernate– Configuration– Mapping

• Linq2NHibernate– Querying

• One, Big, Happy F@mily

Page 5: NHibernate and Friends - HDC2010

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

Page 6: NHibernate and Friends - HDC2010

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

Configuration

Page 7: NHibernate and Friends - HDC2010

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

Mapping

Page 8: NHibernate and Friends - HDC2010

Fluent NHibernate• Fluent Interfaces• Write Mappings & Configuration in Code• Automapping– Convention over configuration

• Added Beer

Page 9: NHibernate and Friends - HDC2010

VB Dim sessionFactory = Fluently.Configure() _ .Database(MsSqlConfiguration.MsSql2008 _ .ConnectionString(GetYouConnectionStringFromWhereEver)) _ .Mappings(Function(m) m.FluentMappings.AddFromAssemblyOf(Of Product)()) _ .BuildSessionFactory()

C# var sessionFactory = Fluently.Configure() .Database(SQLiteConfiguration.Standard .ConnectionString(cs=>cs.FromConnectionStringWithKey("Default"))

.Mappings(m => m.AutoMappings.Add( AutoMap.AssemblyOf<Killer>(new AutoMappingConfig())

.BuildSessionFactory();

Configuration

Page 10: NHibernate and Friends - HDC2010

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 SubEnd Class

Mapping (Visual Basic)

Page 11: NHibernate and Friends - HDC2010

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

Mapping (C#)

Page 12: NHibernate and Friends - HDC2010

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

Page 13: NHibernate and Friends - HDC2010

Dim products = Session.CreateCriteria(Of Person).List(Of Person)()var products = Session.CreateCriteria<Person>().List<Person)();

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.Linq(Of Person)()var products = Session.Linq<Person>();

SELECT * FROM People

Dim products = Session.Linq(Of Person)().Where(Function(p) p.Name.Contains(“eve”))

SELECT * FROM Products WHERE Name LIKE ‘%?%’; 0=‘eve’

var products = Session.Linq<Person>().Where(person=>person.Name.Contains(“eve”));

Page 14: NHibernate and Friends - HDC2010

Putting It All Together

Page 15: NHibernate and Friends - HDC2010

Overall Architecture

NHibernateRepository

Typed LinqRepository

AccessorType

In

te

rf

ac

e

In

te

rf

ac

e

Database

Page 16: NHibernate and Friends - HDC2010

IRepository

NHibernateRepository

Database

IKillerRepository

LinqKiller

Repository

IWeaponRepository

Linq Weapon

RepositoryIVictimRepository

LinqVictim

Repository

Application

Page 17: NHibernate and Friends - HDC2010

IRepository<T>

NHibernateRepository

Database

Application

IRepository<Killer> IRepository<Weapon> IRepository<Victim>

Page 18: NHibernate and Friends - HDC2010

Testability

Page 19: NHibernate and Friends - HDC2010

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, "[email protected]") .CheckList(p => p.Addresses, _listOfAddresses) .CheckReference(p => p.Company, _company) .VerifyTheMappings(); trans.Rollback(); }

Testability

Page 20: NHibernate and Friends - HDC2010

DEMO!

Let’s See it In Action