1 / 18

ORMapper : NHibernate

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

derica
Download Presentation

ORMapper : NHibernate

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ORMapper:NHibernate 11.12.2010 Henning Eiben eiben@busitec.de

  2. Die meisten Anwendungen drehen sich um Daten • Klassischer Datenbankzugriff • ConnectionString • SQL oder StoredProcedures • ADO.Net • DataSet, Command-Objekte, … ORMapper: NHibernate

  3. ORM • Anwendungen drehen sich Geschäftsobjekte nicht um Tabellen • ORM • Konvertieren von Tabellen zu Objekten • Weniger Code • Abstraktion ORMapper: NHibernate

  4. Funktionsweise • Datenbank • MS-SQL, SQLLite, Oracle, MySQL, … • Konfiguration • XML, FluentNHibernate (API) • Geschäftsobjekte • Poco ORMapper: NHibernate

  5. Demo ORMapper: NHibernate

  6. Vorteile • Einheitliches Paradigma für Datenzugriffe • Keine wiederkehrenden Arbeiten • Skalierbarkeit, Integrität, Abstraktion ORMapper: NHibernate

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

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

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

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

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

  12. Features • Abfragen • HQL • Criteria API • LINQ • Transaktionen • … ORMapper: NHibernate

  13. Demo ORMapper: NHibernate

  14. Queqies: LINQ var customers = from mycustomerin session.Query<Customer>() wheremycustomer.Id== "ALFKI" selectmycustomer; ORMapper: NHibernate

  15. Queries: HQL var customers = session.CreateQuery("from Customer where Id='ALFKI'") .List<Customer>(); ORMapper: NHibernate

  16. Queries: Criteria API varcustomers = session.CreateCriteria<Customer>() .Add(Expression.Eq("Id", "ALFKI")) .List<Customer>(); ORMapper: NHibernate

  17. Verweise • http://nhforge.org • http://fluentnhibernate.org ORMapper: NHibernate

  18. Danke Henning Eiben eMail/MSN: eiben@busitec.de ICQ: #3 64 70 70 ORMapper: NHibernate

More Related