Post on 29-Jan-2016
description
ORMapper:NHibernate
11.12.2010Henning Eiben
eiben@busitec.de
ORMapper: NHibernate 2
• Die meisten Anwendungen drehen sich um Daten
• Klassischer Datenbankzugriff– ConnectionString– SQL oder Stored Procedures– ADO.Net• DataSet, Command-Objekte, …
11.12.2010
ORMapper: NHibernate 3
ORM
• Anwendungen drehen sich Geschäftsobjekte nicht um Tabellen
• ORM– Konvertieren von Tabellen zu Objekten– Weniger Code– Abstraktion
11.12.2010
ORMapper: NHibernate 4
Funktionsweise
• Datenbank– MS-SQL, SQLLite, Oracle, MySQL, …
• Konfiguration– XML, FluentNHibernate (API)
• Geschäftsobjekte– Poco
11.12.2010
ORMapper: NHibernate 511.12.2010
Demo
ORMapper: NHibernate 6
Vorteile
• Einheitliches Paradigma für Datenzugriffe
• Keine wiederkehrenden Arbeiten
• Skalierbarkeit, Integrität, Abstraktion
11.12.2010
ORMapper: NHibernate 7
XML-Konfirugation 1/2<?xml version="1.0" encoding="utf-8" ?><hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" > <session-factory name="NHibernate.Test"> <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> <property name="connection.connection_string"> Server=(local);initial catalog=NorthWind;Integrated Security=SSPI </property> <property name="adonet.batch_size">10</property> <property name="show_sql">true</property> <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property> <property name="use_outer_join">true</property> <property name="command_timeout">60</property> <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property> <property name="proxyfactory.factory_class"> NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle </property> </session-factory></hibernate-configuration>
11.12.2010
ORMapper: NHibernate 8
XML-Konfiguration 2/2<?xml version="1.0" encoding="utf-8" ?><hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate3" namespace="NHibernate3.Entities" default-lazy="true" default-access="property"> <class name="Customer" table="Customers"> <id name="Id" column="CustomerID" unsaved-value="0"> <generator class="assigned"/> </id>
<property name="Name" column="CompanyName"/> </class> </hibernate-mapping>
11.12.2010
ORMapper: NHibernate 9
var configuration = new Configuration();configuration.Configure();configuration.AddAssembly(typeof (Customer).Assembly);
_sessionFactory = configuration.BuildSessionFactory();
using (var session = _sessionFactory.OpenSession())using (var tx = session.BeginTransaction()){ var customers = from mycustopmer in session.Query<Customer>() where mycustopmer.Id == "ALFKI" select mycustopmer;
customerList.DataSource = customers; customerList.DataBind();
tx.Commit();}
11.12.2010
ORMapper: NHibernate 10
Fluent Konfiguration 1/2
var fluentConfiguration = Fluently.Configure() .Database(MsSqlConfiguration.MsSql2008 .ConnectionString(c => c .Database("Northwind") .Server("localhost") .TrustedConnection() ) .ShowSql() ) .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Customer>());11.12.2010
ORMapper: NHibernate 11
Fluent Konfiguration 2/2public class CustomerMapping : ClassMap<Customer>{ /// <summary> /// Initializes a new instance of the <see cref="Customer"/> class. /// </summary> /// <remarks>Initializes a new OR-Mapping.</remarks> public CustomerMapping() { Table("Customers"); Id(customer => customer.Id).Column("CustomerID").GeneratedBy.Assigned(); Map(c=>c.Name).Column("CompanyName"); }}
11.12.2010
ORMapper: NHibernate 12
Features
• Abfragen– HQL– Criteria API– LINQ
• Transaktionen
• …
11.12.2010
ORMapper: NHibernate 1311.12.2010
Demo
ORMapper: NHibernate 14
Queqies: LINQ
var customers = from mycustomer in session.Query<Customer>() where mycustomer.Id == "ALFKI" select mycustomer;
11.12.2010
ORMapper: NHibernate 15
Queries: HQL
var customers = session.CreateQuery("from Customer where Id='ALFKI'") .List<Customer>();
11.12.2010
ORMapper: NHibernate 16
Queries: Criteria API
var customers = session.CreateCriteria<Customer>() .Add(Expression.Eq("Id", "ALFKI")) .List<Customer>();
11.12.2010
ORMapper: NHibernate 17
Verweise
• http://nhforge.org
• http://fluentnhibernate.org
11.12.2010
18
Danke
Henning Eiben
eMail/MSN: eiben@busitec.deICQ: #3 64 70 70
11.12.2010 ORMapper: NHibernate