An Introduce to NHibernate           PART 1           LOGO            J九日
内容SQL Helper vs. NHibernateObject-Relational Mapping   关系模型   为什么使用 ORM?Hibernate 含义NHibernate Mapping核心类型Exercise...
SQL Helper vs. NHibernateWorking with SQL Helper                           上层应用/Business Logic          太暧昧               ...
SQL Helper vs. NHibernateWorking with NHibernate                                     各种应用程序    解决O-R不对                    ...
Object-Relational MappingRelational Model      Relation                   Relation      Relation
Object-Relational MappingRelational Model Concepts  关系数据库(Relational Database)是建立在关系模型上的数据库;  一个数据表“就是”一个关系(Relation); ...
Object-Relational MappingWhy ORM ?                             Productivity             Modeling                          ...
HibernateHibernate: 休眠、冬眠 活动的熊              Hibernate   冬眠的熊
HibernateHibernate: a Java ORM Framework        对象                 Hibernate   关系活动的熊                       Hibernate   冬眠...
NHibernate Mapping           Mapping嵌入的资源
Core NHibernate TypesConfiguration          Build (Once)           Open SessionISession         ISession                  ...
Core NHibernate TypesNHibernate.Cfg.Configuration// 配置 Configuration 对象Configuration Configure()Configuration Configure(st...
Core NHibernate TypesNHibernate.ISessionFactory//Create   a database connection and open a <c>ISession</c> on itISession  ...
Core NHibernate TypesISession查询示例:查询所有用户Criteria QuerySession.CreateCriteria<User>().List<User>();Strong-typed Criteria Qu...
Exercise:ExBlogger目标:多用户博客程序访客可以注册成用户用户可以创建自己的博客用户可以在自己的博客里写帖子用户可以评论帖子使用NHibernate框架持久化对象
Exercise:ExBlogger  访客                     博客       注册               创建       博客用户          写    帖子              发表       ...
Exercise:ExBlogger      Visitor                                     Blog          Register                          Create...
Exercise:ExBlogger应用服务契约的制定与测试(略)已确定对象的创建工厂已确定对象的职责下一步:制定/实现资源库契约(Repository Contracts/Interfaces)
Exercise:ExBloggerIRepository<T>int Save(T entity);void Update(T entity);T Get(int id);                 通用持久化方法IList<T> Ge...
Exercise:ExBlogger创建并存储用户var user =BlogRegistrar.GetInstance().CreateUser(“Mike", "pwd");var id = _usersRepository.Save(us...
Exercise:ExBlogger创建并存储博客var blog = user.CreateBlog("foo", "bar");_blogsRepository.Save(blog);创建并存储帖子var post = user.Creat...
Exercise:ExBlogger创建并存储评论 var comment = user.CreateComment("沙发").On(post);_commentsRepository.Save(comment);获取评论列表:组装自定义的DTO
Thanks !示例代码SVN地址:http://ex-blogger.googlecode.com/svn/trunk/推荐资料:李永京博客http://www.cnblogs.com/lyj/领域驱动设计精简版http://www.info...
Upcoming SlideShare
Loading in …5
×

An introduce to n hibernate (part 1) pub

1,001 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,001
On SlideShare
0
From Embeds
0
Number of Embeds
87
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

An introduce to n hibernate (part 1) pub

  1. 1. An Introduce to NHibernate PART 1 LOGO J九日
  2. 2. 内容SQL Helper vs. NHibernateObject-Relational Mapping  关系模型  为什么使用 ORM?Hibernate 含义NHibernate Mapping核心类型Exercise: ExBlogger
  3. 3. SQL Helper vs. NHibernateWorking with SQL Helper 上层应用/Business Logic 太暧昧 创建数据对象 编写SQL 设计数据模型  以数据表为核心 设计数据表  模型仅为装载数据  底层可能包含业务逻辑  底层与上层关系紧密 前期的设计很  受PetShop4的影响 可能面目全非 “Data-Driven Design”
  4. 4. SQL Helper vs. NHibernateWorking with NHibernate 各种应用程序 解决O-R不对 设计应用 称,转移主 服务契约 要关注点! 设计业务 Cache 领域对象 Search 设计资源 库契约 NoSQL  以业务对象为核心 NHibernate 资  资源库不关心业务逻辑 源库实现  应用层不关心契约实现  促进面向契约编程 设计数据表 Domain-Driven Design
  5. 5. Object-Relational MappingRelational Model Relation Relation Relation
  6. 6. Object-Relational MappingRelational Model Concepts  关系数据库(Relational Database)是建立在关系模型上的数据库;  一个数据表“就是”一个关系(Relation);  表(关系)之间通过“键”连接;
  7. 7. Object-Relational MappingWhy ORM ? Productivity Modeling and mismatch maintainability ORM Database Performance independency
  8. 8. HibernateHibernate: 休眠、冬眠 活动的熊 Hibernate 冬眠的熊
  9. 9. HibernateHibernate: a Java ORM Framework 对象 Hibernate 关系活动的熊 Hibernate 冬眠的熊 关注点
  10. 10. NHibernate Mapping Mapping嵌入的资源
  11. 11. Core NHibernate TypesConfiguration Build (Once) Open SessionISession ISession … Create …IQuery ISQLQuery ICriteria IQueryOver IQueryable HQL SQL Dynamically LINQ
  12. 12. Core NHibernate TypesNHibernate.Cfg.Configuration// 配置 Configuration 对象Configuration Configure()Configuration Configure(string fileName)Configuration Configure(Assembly assembly, string resourceName)Configuration Configure(XmlReader textReader)// 构建 ISessionFactory 的实例ISessionFactory BuildSessionFactory() 配 置 文 件
  13. 13. Core NHibernate TypesNHibernate.ISessionFactory//Create a database connection and open a <c>ISession</c> on itISession OpenSession()ISession OpenSession(IDbConnection conn)ISession OpenSession(IInterceptor sessionLocalInterceptor)NHibernate.ISessionobject 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)
  14. 14. Core NHibernate TypesISession查询示例:查询所有用户Criteria QuerySession.CreateCriteria<User>().List<User>();Strong-typed Criteria QuerySession.QueryOver<User>().List<User>();NHibernate Linq QuerySession.Query<User>().ToList<User>();Query with Hibernate Query LanguageSession.CreateQuery("from User").List<User>();Native SQL QuerySession.CreateSQLQuery("select * from Users") .SetResultTransformer(Transformers.AliasToBean<User>()) .List<User>();
  15. 15. Exercise:ExBlogger目标:多用户博客程序访客可以注册成用户用户可以创建自己的博客用户可以在自己的博客里写帖子用户可以评论帖子使用NHibernate框架持久化对象
  16. 16. Exercise:ExBlogger 访客 博客 注册 创建 博客用户 写 帖子 发表 评论
  17. 17. Exercise:ExBlogger Visitor Blog Register Create User Create Post CreateUbiquitous Language领域通用语言 Comment
  18. 18. Exercise:ExBlogger应用服务契约的制定与测试(略)已确定对象的创建工厂已确定对象的职责下一步:制定/实现资源库契约(Repository Contracts/Interfaces)
  19. 19. Exercise:ExBloggerIRepository<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>
  20. 20. Exercise:ExBlogger创建并存储用户var user =BlogRegistrar.GetInstance().CreateUser(“Mike", "pwd");var id = _usersRepository.Save(user);获取所有用户var users = _usersRepository.GetAll();
  21. 21. 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);
  22. 22. Exercise:ExBlogger创建并存储评论 var comment = user.CreateComment("沙发").On(post);_commentsRepository.Save(comment);获取评论列表:组装自定义的DTO
  23. 23. Thanks !示例代码SVN地址:http://ex-blogger.googlecode.com/svn/trunk/推荐资料:李永京博客http://www.cnblogs.com/lyj/领域驱动设计精简版http://www.infoq.com/cn/minibooks/domain-driven-design-quickly

×