180 likes | 356 Views
ORMapper : NHibernate. 11.12.2010 Henning Eiben eiben@busitec.de. Die meisten Anwendungen drehen sich um Daten Klassischer Datenbankzugriff ConnectionString SQL oder Stored Procedures ADO.Net DataSet , Command-Objekte, …. ORM. Anwendungen drehen sich Geschäftsobjekte nicht um Tabellen
E N D
ORMapper:NHibernate 11.12.2010 Henning Eiben eiben@busitec.de
Die meisten Anwendungen drehen sich um Daten • Klassischer Datenbankzugriff • ConnectionString • SQL oder StoredProcedures • ADO.Net • DataSet, Command-Objekte, … ORMapper: NHibernate
ORM • Anwendungen drehen sich Geschäftsobjekte nicht um Tabellen • ORM • Konvertieren von Tabellen zu Objekten • Weniger Code • Abstraktion ORMapper: NHibernate
Funktionsweise • Datenbank • MS-SQL, SQLLite, Oracle, MySQL, … • Konfiguration • XML, FluentNHibernate (API) • Geschäftsobjekte • Poco ORMapper: NHibernate
Demo ORMapper: NHibernate
Vorteile • Einheitliches Paradigma für Datenzugriffe • Keine wiederkehrenden Arbeiten • Skalierbarkeit, Integrität, Abstraktion ORMapper: NHibernate
XML-Konfirugation 1/2 <?xmlversion="1.0" encoding="utf-8" ?> <hibernate-configurationxmlns="urn:nhibernate-configuration-2.2" > <session-factoryname="NHibernate.Test"> <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> <propertyname="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> <propertyname="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> <propertyname="proxyfactory.factory_class"> NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle </property> </session-factory> </hibernate-configuration> ORMapper: NHibernate
XML-Konfiguration 2/2 <?xmlversion="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"> <generatorclass="assigned"/> </id> <property name="Name" column="CompanyName"/> </class> </hibernate-mapping> ORMapper: NHibernate
varconfiguration = newConfiguration(); configuration.Configure(); configuration.AddAssembly(typeof(Customer).Assembly); _sessionFactory = configuration.BuildSessionFactory(); using(varsession = _sessionFactory.OpenSession()) using(vartx = session.BeginTransaction()) { varcustomers = from mycustopmer in session.Query<Customer>() wheremycustopmer.Id == "ALFKI" selectmycustopmer; customerList.DataSource= customers; customerList.DataBind(); tx.Commit(); } ORMapper: NHibernate
Fluent Konfiguration 1/2 varfluentConfiguration = Fluently.Configure() .Database(MsSqlConfiguration.MsSql2008 .ConnectionString(c => c .Database("Northwind") .Server("localhost") .TrustedConnection() ) .ShowSql() ) .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Customer>()); ORMapper: NHibernate
Fluent Konfiguration 2/2 publicclassCustomerMapping : ClassMap<Customer> { ///<summary> ///Initializes a new instance of the <see cref="Customer"/> class. ///</summary> ///<remarks>Initializes a new OR-Mapping.</remarks> publicCustomerMapping() { Table("Customers"); Id(customer => customer.Id).Column("CustomerID").GeneratedBy.Assigned(); Map(c=>c.Name).Column("CompanyName"); } } ORMapper: NHibernate
Features • Abfragen • HQL • Criteria API • LINQ • Transaktionen • … ORMapper: NHibernate
Demo ORMapper: NHibernate
Queqies: LINQ var customers = from mycustomerin session.Query<Customer>() wheremycustomer.Id== "ALFKI" selectmycustomer; ORMapper: NHibernate
Queries: HQL var customers = session.CreateQuery("from Customer where Id='ALFKI'") .List<Customer>(); ORMapper: NHibernate
Queries: Criteria API varcustomers = session.CreateCriteria<Customer>() .Add(Expression.Eq("Id", "ALFKI")) .List<Customer>(); ORMapper: NHibernate
Verweise • http://nhforge.org • http://fluentnhibernate.org ORMapper: NHibernate
Danke Henning Eiben eMail/MSN: eiben@busitec.de ICQ: #3 64 70 70 ORMapper: NHibernate