Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 18 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
18
Dung lượng
321,61 KB
Nội dung
O/R Mapping in .NET P R E S E N T E R : M R . D O A N Q U A N G M I N H Agenda Introduction NHibernate .nettiers CodeSmith CodeSmith Demo Introduction O/R Mapping Is the abbreviation of Object-relational Mapping Used to map object-oriented programming objects to relational databases Ability Ability Perform CRUD action (Create, Read, Update, Delete) Support specifying object criteria (data range, ID value,…) Support caching, concurrency, and validation Two types O/R Mapping component Code Generation NHibernate (1) Introduction Come from Hibernate of Java Handles persisting plain .NET objects to and from an underlying relational database Automatically generates SQL by a XML description Automatically generates SQL by a XML description Feature Natural programming model Native .NET No build-time byte code enhancement Custom SQL Free/open source NHibernate (2) •Application: the app which want to store and retrieve object from db •Persistence Objects: object which to be Application NHIBERNATE hibernate properties XML Mapping Persistence Objects saved to db •NHibernate: A middle component help CRUD task Database propertiesproperties MappingMapping NHibernate - Getting started (1) Config NHibernate <configuration> <configSections> <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" /> </configSections> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> <property name="connection.connection_string">Data Source=.;Database=Northwind;Integrated Security=True;</property> <property name="connection.isolation">ReadCommitted</property> <property name="default_schema">Northwind.dbo</property> <! HBM Mapping Files > <mapping assembly="BasicSample.Core" /> </session-factory> </hibernate-configuration> <system.web> </system.web> </configuration> NHibernate - Getting started (2) Define class using System; namespace QuickStart { public class Cat { private string id; private string name; public Cat() public Cat() { } public string Id { get { return id; } set { id = value; } } public string Name { get { return name; } set { name = value; } } } } NHibernate - Getting started (2) Define mapping <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="QuickStart" assembly="QuickStart"> <class name="Cat" table="Cat"> <! A 32 hex character is our surrogate key. It's automatically generated by NHibernate with the UUID pattern. > < id name="Id"> < id name="Id"> <column name="CatId" sql-type="char(32)" not- null="true"/> <generator class="uuid.hex" /> </id> <! A cat has to have a name, but it shouldn' be too long. > <property name="Name"> <column name="Name" length="16" not-null="true" /> </property> <property name="Sex" /> <property name="Weight" /> </class> </hibernate-mapping> NHibernate - Getting started (3) Working with class ISessionFactory = new Configuration().Configure().BuildSessionFactory(); ISession session = sessionFactory.OpenSession (); ISession session = sessionFactory.OpenSession (); ITransaction tx = session.BeginTransaction(); Cat princess = new Cat(); princess.Name = "Princess"; princess.Sex = 'F'; princess.Weight = 7.4f; session.Save(princess); tx.Commit(); session.CloseSession(); NHibernate - Getting started (3) Using HQL (Hibernate Query Language) ITransaction tx = session.BeginTransaction(); IQuery query = session.CreateQuery("select c from Cat as c where c.Sex = :sex"); as c where c.Sex = :sex"); query.SetCharacter("sex", 'F'); foreach (Cat cat in query.Enumerable()) { Console.Out.WriteLine("Female Cat: " + cat.Name); } tx.Commit(); [...]... Northwind) Select table to generate code Select pattern type to implement in the Component Layer (choose Service Layer) Some other setting… Test the generated code Open with Visual Studio .nettiers Getting Started (2) Using DataSource There are many generated datasource : belong table and properties in table (PK, Index, ) DataSource has many features: many Select mathod, Sorting, Paging, .nettiers Getting... relationship objects Creates a full website project, administration web controls, DataSource controls and other .nettier (2) Data tier: contains custom business entities components and data access logic components Follow the Microsoft patterns & practices guide: Designing Data Tier Components and Passing Data Through Tiers .nettiers Getting Started (1) Setting up nettiers Download nettiers 2.2 from official... code generator Working with SQL Server, Oracle, Working with many templates: netTiers, CSLA, NHibernate, PLINQO, Wilson's ORMapper, APOSA, and more Version 2.6 is freeware Using CodeSmith Open CodeSmith Select template and generate Modify template: code style like asp.net Reference www.asp.net www.msdn.com http://www.hibernate.org/343.html http://nettiers.com/ Some others resource on Internet ... DataSource has many features: many Select mathod, Sorting, Paging, .nettiers Getting Started (2) nettiers API (abc)Service class: contains the methods that interact with DB (abc) class: the entity class mapping with DB TList: enhanced of List, contains new methods working with DB AccountService accountsService = new AccountsService(); TList list1 = accountsService.GetAll(); // Get all // Get... Get by Index TList list4 = accountsService.GetByAccountCreatedDate(new DateTime("1/1/2006")); Account accountEntity = new Account(); // create new entity accountEntity.AccountName = "MyAccountName“; accountEntity.CreatedDate = DateTime.Now; accountsService.Save(accountEntity); // insert or update, depend entity status accountList = accountsService.Delete(23); // delete by FK CodeSmith Introdution....nettier (1) Introduction Is a set of generation templates Allow create customized Application Tiers for Microsoft.Net applications Work together with CodeSmith Feature Generates a fully compliable solution along with separate projects and framework tiers Creates . O/R Mapping in .NET P R E S E N T E R : M R . D O A N Q U A N G M I N H Agenda Introduction NHibernate .nettiers CodeSmith CodeSmith Demo Introduction O/R Mapping Is. mathod, Sorting, Paging, Paging, .nettiers Getting Started (2) .nettiers API (abc)Service class: contains the methods that interact with DB (abc) class: the entity class mapping with DB . Support caching, concurrency, and validation Two types O/R Mapping component Code Generation NHibernate (1) Introduction Come from Hibernate of Java Handles persisting plain .NET objects