An introduce to n hibernate (part 1) pub

23
LOGO An Introduce to NHibernate PART 1 J 九九

Transcript of An introduce to n hibernate (part 1) pub

Page 1: An introduce to n hibernate (part 1) pub

LOGO

An Introduce to NHibernatePART 1

J 九日

Page 2: An introduce to n hibernate (part 1) pub

内容

SQL Helper vs. NHibernate Object-Relational Mapping

关系模型 为什么使用 ORM?

Hibernate 含义NHibernate Mapping核心类型Exercise: ExBlogger

Page 3: An introduce to n hibernate (part 1) pub

SQL Helper vs. NHibernate

设计数据表

设计数据模型

编写 SQL

创建数据对象

上层应用 /Business Logic

Working with SQL Helper

以数据表为核心 模型仅为装载数据 底层可能包含业务逻辑 底层与上层关系紧密 受 PetShop4 的影响

“Data-Driven Design”

太暧昧

前期的设计很可能面目全非

Page 4: An introduce to n hibernate (part 1) pub

SQL Helper vs. NHibernate

Working with NHibernate

设计应用服务契约

设计资源库契约

NHibernate 资源库实现

设计数据表

各种应用程序

Cache

Search

NoSQL 以业务对象为核心 资源库不关心业务逻辑 应用层不关心契约实现 促进面向契约编程

Domain-Driven Design

解决 O-R 不对称,转移主要关注点!

设计业务领域对象

Page 5: An introduce to n hibernate (part 1) pub

Object-Relational Mapping

Relational Model

Relation

Relation

Relation

Page 6: An introduce to n hibernate (part 1) pub

Object-Relational Mapping

Relational Model Concepts

关系数据库( Relational Database )是建立在关系模型上的数据库; 一个数据表“就是”一个关系( Relation ); 表(关系)之间通过“键”连接;

Page 7: An introduce to n hibernate (part 1) pub

Object-Relational Mapping

Why ORM ?

Modeling mismatch

Productivity and

maintainability

PerformanceDatabase

independency

ORM

Page 8: An introduce to n hibernate (part 1) pub

Hibernate

Hibernate: 休眠、冬眠

活动的熊 冬眠的熊Hibernate

Page 9: An introduce to n hibernate (part 1) pub

Hibernate

Hibernate: a Java ORM Framework

对象 关系Hibernate

冬眠的熊Hibernate

活动的熊

关注点Hibernate: Java

NHibernate: .NET

让开发者更关注对象的设计!

Page 10: An introduce to n hibernate (part 1) pub

NHibernate Mapping

Mapping

嵌入的资源

Page 11: An introduce to n hibernate (part 1) pub

Core NHibernate Types

Configuration ISessionFactory

ISession

IQuery

Build (Once)

ISession……

Open Session

ISQLQuery ICriteria IQueryOver

HQL SQL

IQueryable

Create

Dynamically LINQ

Page 12: An introduce to n hibernate (part 1) pub

Core NHibernate Types

NHibernate.Cfg.Configuration

// 配置 Configuration 对象Configuration Configure()Configuration Configure(string fileName)Configuration Configure(Assembly assembly, string resourceName)Configuration Configure(XmlReader textReader)// 构建 ISessionFactory 的实例ISessionFactory BuildSessionFactory()

配置文件

Page 13: An introduce to n hibernate (part 1) pub

Core NHibernate Types

NHibernate.ISessionFactory

//Create a database connection and open a <c>ISession</c> on itISession OpenSession()ISession OpenSession(IDbConnection conn)ISession OpenSession(IInterceptor sessionLocalInterceptor)

NHibernate.ISession

object Save(object obj)void Delete(object obj)void Update(object obj)ICriteria CreateCriteria<T>()IQueryOver<T,T> QueryOver<T>()IQuery CreateQuery(string queryString)ISQLQuery CreateSQLQuery(string queryString)IQueryable<T> Query<T>(this ISession session)

Page 14: An introduce to n hibernate (part 1) pub

Core NHibernate Types

Criteria Query

ISession 查询示例:查询所有用户

Session.CreateCriteria<User>().List<User>();

Strong-typed Criteria Query

Session.QueryOver<User>().List<User>();

Query with Hibernate Query Language

Session.CreateQuery("from User").List<User>();

Native SQL Query

Session.CreateSQLQuery("select * from Users") .SetResultTransformer(Transformers.AliasToBean<User>()) .List<User>();

Session.Query<User>().ToList<User>();

NHibernate Linq Query

Page 15: An introduce to n hibernate (part 1) pub

Exercise:ExBlogger

目标:多用户博客程序

访客可以注册成用户用户可以创建自己的博客用户可以在自己的博客里写帖子用户可以评论帖子使用 NHibernate 框架持久化对象

Page 16: An introduce to n hibernate (part 1) pub

Exercise:ExBlogger

访客

博客用户

博客

帖子

评论

注册创建

发表

Page 17: An introduce to n hibernate (part 1) pub

Exercise:ExBlogger

Visitor

User

Blog

Post

Comment

Register Create

Create

Create

Ubiquitous Language领域通用语言

Page 18: An introduce to n hibernate (part 1) pub

Exercise:ExBlogger

应用服务契约的制定与测试(略)已确定对象的创建工厂已确定对象的职责下一步:制定 / 实现资源库契约( Repository Contracts/Interfaces )

Page 19: An introduce to n hibernate (part 1) pub

Exercise:ExBlogger

IRepository<T>

int Save(T entity);void Update(T entity);T Get(int id);IList<T> GetAll();void Delete(int id);

IUsersRepository:IRepository<User>

IBlogsRepository:IRepository<Blog>

IPostsRepository:IRepository<Post>

ICommentsRepository:IRepository<Comment>

通用持久化方法

自定义持久化方法

Page 20: An introduce to n hibernate (part 1) pub

Exercise:ExBlogger

创建并存储用户

var user = BlogRegistrar.GetInstance().CreateUser(“Mike", "pwd");var id = _usersRepository.Save(user);

var users = _usersRepository.GetAll();

获取所有用户

Page 21: An introduce to n hibernate (part 1) pub

Exercise:ExBlogger

创建并存储博客

var blog = user.CreateBlog("foo", "bar");_blogsRepository.Save(blog);

var post = user.CreatePost("Hello World", "This my first post!") .On(blog);_postsRepository.Save(post);

创建并存储帖子

Page 22: An introduce to n hibernate (part 1) pub

Exercise:ExBlogger

获取评论列表:组装自定义的 DTO

var comment = user.CreateComment(" 沙发 ").On(post);_commentsRepository.Save(comment);

创建并存储评论

Page 23: An introduce to n hibernate (part 1) pub

Thanks !

示例代码 SVN 地址:http://ex-blogger.googlecode.com/svn/trunk/

推荐资料:

李永京博客http://www.cnblogs.com/lyj/

领域驱动设计精简版http://www.infoq.com/cn/minibooks/domain-driven-design-quickly