Agile Development with IoC and ORM James Kovacs JamesKovacs.com |

Slides:



Advertisements
Similar presentations
Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates.
Advertisements

An Object/Relational Mapping tool Free and open source Simplifies storage of object data in a relational database Removes the need to write and maintain.
Michael Pizzo Software Architect Data Programmability Microsoft Corporation.
NHibernate Object/Relational Persistence for.NET.
Keys, Referential Integrity and PHP One to Many on the Web.
.NET Database Technologies: Open-Source Frameworks.
Convention-over-Configuration in an Agile World James Kovacs JamesKovacs.com |
Fast Track to ColdFusion 9. Getting Started with ColdFusion Understanding Dynamic Web Pages ColdFusion Benchmark Introducing the ColdFusion Language Introducing.
Session-02.
A tour of new features introducing LINQ. Agenda of LINQ Presentation We have features for every step of the way LINQ Fundamentals Anonymous Functions/Lambda.
Convention-over-Configuration in an Agile World James Kovacs
Discover, Master, InfluenceSlide 1 SQL Server Compact Edition and the Entity Framework Rob Sanders Readify.
An Introduction to Hibernate Matt Secoske
Data Access Patterns. Motivation Most software systems require persistent data (i.e. data that persists between program executions). In general, distributing.
CSE446 S OFTWARE Q UALITY M ANAGEMENT Spring 2014 Yazılım ve Uyguluma Geliştirme Yöneticisi Orhan Başar Evren.
Neo.NET Entity Objects Design Goals Copyright © Erik Dörnenburg – Last updated: May 2004.
Entity Framework Code First End to End
Agenda What is Hibernate Spring Integration Questions Overview
.NET Database Technologies: Entity Framework additional notes – part 2.
ASP.NET Programming with C# and SQL Server First Edition
JPA Java Persistence API. Introduction The Java Persistence API provides an object/relational mapping facility for managing relational data in Java applications.
The Persistence Ignorant Domain Model James Kovacs
Data Access Patterns Some of the problems with data access from OO programs: 1.Data source and OO program use different data modelling concepts 2.Decoupling.
NHibernate in Action Web Seminar at UMLChina By Pierre Henri Kuaté 2008/08/27
Databases in Visual Studio. Database in VisualStudio An MS SQL database are built in Visual studio The Name can be something like ”(localdb)\Projects”
Chapter 15: Using LINQ to Access Data in C# Programs.
CHAPTER 14 USING RELATIONAL DATABASES TO PROVIDE OBJECT PERSISTENCE (ONLINE) © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database.
.NET Database Technologies: Data Models and Patterns.
Object/Relational Mapping with Hibernate Practical ORM.
1 Part 2: EJB Persistency Jianguo Lu. 2 Object Persistency A persistent object is one that can automatically store and retrieve itself in permanent storage.
Object-to-Relational Mapping: The Crossing Chasms Pattern and Implementation Considerations Use of Meta Data in the Java Persistence Layer Presented by.
Hibernate Persistence. What is Persistence Persist data to database or other storage.  In OO world, persistence means persist object to external storage.
Intoduction to NHibernate. Agenda Overview of NHibernate Models and Mappings Configuration Sessions and Transactions Queries.
Hibernate Introduction - 1 Present by Eric Yu. BeanSoft | 2 Content Understanding object/relational persistence Introduction Architecture The core interfaces.
Java Data Persistence Using Hibernate Jack Gardner October 2004.
Object-Relational Mapping with Hibernate Brian Sam-Bodden Principal Partner Integrallis Software, LLC. August 1 - 5, 2005.
An Introduction to Object/Relational Persistence and Hibernate Yi Li
Oct * Brad Tutterow. VS 2008.NET 3.5LINQ Entity Framework  The ADO.NET Entity Framework is part of Microsoft’s next generation of.NET technologies.
Data Driven Designs 99% of enterprise applications operate on database data or at least interface databases. Most common DBMS are Microsoft SQL Server,
Intoduction to NHibernate. Agenda Overview of NHibernate Models and Mappings Configuration Sessions and Transactions Queries.
Topic : Hibernate 3:Advanced ORM Kaster Nurmukan.
Intoduction to NHibernate. Agenda Overview of NHibernate Models and Mappings Configuration Sessions and Transactions Queries.
Apr. 8, 2002Calibration Database Browser Workshop1 Database Access Using D0OM H. Greenlee Calibration Database Browser Workshop Apr. 8, 2002.
Database Management Supplement 1. 2 I. The Hierarchy of Data Database File (Entity, Table) Record (info for a specific entity, Row) Field (Attribute,
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool The problem fixed by ORM Advantage Hibernate Hibernate Basic –Hibernate sessionFactory –Hibernate Session.
All information's of PLINQO in this Document, I got it from: So, you could visit the link above to research.
Copyright © 2013 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill/Irwin APPENDIX C DESIGNING DATABASES APPENDIX C DESIGNING DATABASES.
Session 1 Module 1: Introduction to Data Integrity
Hibernate Basics. Basics What is persistence? Persistence in Object Oriented Applications? Paradigm Mismatch.  The Problem of Granularity.  The Problem.
Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool Used in data layer of applications Implements JPA.
Chapter 3: Relational Databases
Hibernate Thuy, Le Huu. Pentalog VN. Agenda Hibernate Annotations Improving performance – Lazy loading – Fetching Strategies – Dynamic insert, dynamic.
Sean Chambers.  ORM stands for Object Relational Mapper  Maps your POCO (plain old clr objects) to your relational model using XML config  Relieves.
Singleton Academy, Pune. Course syllabus Singleton Academy Pune – Course Syllabus1.
ORM Basics Repository Pattern, Models, Entity Manager Ivan Yonkov Technical Trainer Software University
 Who I am  What is NHibernate? ◦ Object Relational Mapper ◦ A framework for building advanced frameworks ◦ Ported from Java ◦ A wide NHibernate ecosystem.
CS422 Principles of Database Systems Object-Relational Mapping (ORM) Chengyu Sun California State University, Los Angeles.
CS520 Web Programming Object-Relational Mapping with Hibernate and JPA Chengyu Sun California State University, Los Angeles.
CS520 Web Programming Object-Relational Mapping with Hibernate and JPA (I) Chengyu Sun California State University, Los Angeles.
XP Chapter 1 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Level 2 Objectives: Understanding and Creating Table.
OpenAccess ORM Advanced Topics Kevin Babcock April 9, 2009.
Introduction to Entity framework
Don't Know Jack About Object-Relational Mapping?
Chengyu Sun California State University, Los Angeles
Introduction to Entity Framework
Entity Framework By: Casey Griffin.
ADO.NET Entity Framework Marcus Tillett
ADO.NET Entity Framework
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Agile Development with IoC and ORM James Kovacs JamesKovacs.com |

QUICK SURVEY

INTRODUCTION TO IOC

Dependency Inversion High-level modules should not depend on low-level modules. Both should depend on abstractions. »Robert C. Martin

Dependency Injection Dependencies are provided to objects via constructor or properties  Constructor injection  Setter injection Prefer constructor injection for required dependencies Prefer setter injection for optional dependencies

Lab: Poor Man’s Dependency Injection Create a CustomerService that depends on a CustomerRepository Use Poor Man’s to wire them together

Inversion of Control Containers Hashtable of interface vs. implementing type  In simplest form, basically: Dictionary Full-fledged containers offer a lot more...

Popular Containers Windsor  StructureMap  Spring.NET  Unity  Ninject  Autofac 

Why Use Popular Container? Wider configuration options  XML, code, script Lifetime management  Singleton, transient, per-thread, or pooled Auto-wiring dependencies Run-time configurability Plug-ins

Lab: Castle Windsor Change your previous lab to use Castle Windsor

One Assembly to Rule Them All Common Service Locator  public interface IServiceLocator : IServiceProvider { object GetInstance(Type serviceType); object GetInstance(Type serviceType, string key); IEnumerable GetAllInstances(Type serviceType); TService GetInstance (); TService GetInstance (string key); IEnumerable GetAllInstances (); }

Resources Loosen Up: Tame Your Software Dependencies for More Flexible Apps, MSDN Magazine March 2008  Bricks and Mortar: Building a Castle, CoDe Magazine May/June 2009  Castle Windsor  The Bookshelf 

INTRODUCTION TO O/RM

OO and Relational Worlds OO Object-based Unidirectional associations Pointer from owner Inheritance Polymorphism Many-to-many Relational Set-based Bidirectional associations FK on owned No inheritance No polymorphism Join tables

NHIBERNATE

NHibernate Quickstart Create hibernate.cfg.xml or use app.config var cfg = new Configuration(); cfg.Configure(); var sf = cfg.BuildSessionFactory(); using(var s = sf.OpenSession()) using(var tx = s.BeginTransaction()) { var c = s.Get (42); tx.Commit(); }

NHibernate API ConfigurationClass for bootstrapping NHibernate ISessionFactoryFactory for creating sessions ISessionRoughly analogous to a database connection ITransactionAbstracts underlying transaction semantics IQueryString-based query API aka HQL ICriteriaObject-based query API aka Criteria LINQLINQ query API for NHibernate

Patterns in NHibernate Data Mapper Identity Map Unit of Work Lazy Loading And many more from Martin Fowler’s Patterns of Enterprise Application Architcture

Intellisense for NHibernate Drop XSD files  nhibernate-mapping.xsd  nhibernate-configuration.xsd Into  C:\Program Files \Microsoft Visual Studio 9.0\xml\schemas Or  C:\Program Files (x86)\Microsoft Visual Studio 9.0\xml\schemas

Lab: Initial Setup Install Subversion client Install VisualSVN (optional) Download from Subversion Install XSD files Install ReSharper templates Install NHibernate Plug-in 1.0 Create empty database

CONFIGURATION

NHibernate Configuration hibernate.cfg.xml App.config or Web.config Run-time

DEMO Configuring NHibernate

Lab: Configuration Create an application Reference assemblies Add a configuration file Configure NHibernate

MAPPING

Mapping Basics Class Definitions Mapping Metadata Database Schema

Mapping Metadata XML files (hbm.xml) NHibernate.Mapping.Attributes in NHContrib Castle ActiveRecord Fluent NHibernate  ClassMap  Automaps

DEMO Mapping, Schema Generation, and Simple Fetches

Lab: Mapping Create a class with simple properties Map the class using ClassMap Export the database schema Insert some data using session.Save(obj) Retrieve the saved data using session.Get () and session.Load ()

RELATIONSHIPS

Understanding Relationships  Two tables, Customer and Person, share same PK inside  Two tables, Customer and Order, with a CustomerId on the Order table on Order  Two tables, Customer and Order, with a FK on Order pointing back to its parent Customer  Two tables with a joining table  Joining table has two FKs, one to each table

One-to-One Associations Use element  Standard FK in parent table to child table  E.g. Person HAS-A HomeAddress   where HomeAddress is an entity with its own PK Avoid element  Used for two tables that share the same primary key  Typically better mapped using  Inheritance   More information in NHibernate documentation

Sets, Lists, and Bags... Oh My! Set  Unordered collection of unique elements  Mapped using Iesi.Collections.Generic.ISet List  Ordered collection of non-unique elements  Mapped using System.Collections.Generic.IList Bag  Unordered collection of non-unique elements  Mapped using System.Collections.Generic.IList Others, though not commonly used  Map (Hashtable or Dictionary )  Array  Primitive-Array  IdBag

Cascades Tells NHibernate how to handle child entities Options:  none – no cascades (default)  all – cascade saves, updates, and deletes  save-update - cascade saves and updates  delete – cascade deletes  all-delete-orphan - same as all and delete orphaned rows Can specify default-cascade in hbm.xml file

Lazy Loading Default for associations in NHibernate 1.2+ Requires open ISession Fetching strategies  Select, outer-join Avoiding the N+1 SELECT problem

Understanding Inverse=“true” Relational model  Bidirectional associations using one FK OO model  Unidirectional associations using references Bidirectional associations in the OO  Two unidirectional associations with the same data Inverse=“true” tells NHibernate which one to ignore Prevents duplicate updates of FK Prevents FK violations “Which table owns the FK?”  and collection  inverse is collection   choose either N.B. Cascades are an orthogonal concept!

DEMO Mapping Associations

Lab: Collections Add a collection to your class Map the collection Load and save items to the collection

INHERITANCE

Mapping Inheritance Relationships Person -Id -Name Customer -RewardStatus Employee -Office

Single table inheritance Uses discriminator column One table contains all possible columns All derived class columns must be nullable Person Id (PK) Name RewardStatus (null) Office (null)

Concrete table inheritance Table per concrete class with complete columns Uses subclass Customer Id (PK) Name RewardStatus Employee Id (PK) Name Office

Class table inheritance Table per class with diff of columns Uses joined-subclass Customer Id (PK/FK) RewardStatus Employee Id (PK/FK) Office Person Id (PK) Name

DEMO Mapping Inheritance Relationships

Lab: Inheritance Add a derived class Map the relationship using concrete-table inheritance Save instances of base and derived types Examine the saved rows of data EXTRA: Try mapping with single-table and class-table inheritance

QUERYING

Querying ISession.Get (id) and ISession.Load (id) Hibernate Query Language (HQL) Criteria API Example Queries LINQ to NHibernate

DEMO Querying with HQL

Lab: HQL Queries Query for customers:  named is “John”  names begin with “S” Query for orders:  shipped to Auburn  placed in the last 6 months dex.html#queryhql

DEMO Querying with Criteria

Lab: Criteria Queries Query for customers:  named is “John”  names begin with “S” Query for orders:  shipped to Auburn  placed in the last 6 months

DEMO Querying with Examples

Lab: LINQ Find Addresses in Canada Find Customers with LastName starting with S

DEMO Filters, Aggregations, and Projections

Lab: Filters, Aggregations, and Projections Page through SalesOrders for all Walter in the database using a Filter Number of addresses per city Determine the average tax paid on all orders

THE LEVEL 2 CACHE

NHibernate’s Level 2 Cache Cache providers  Hashtable (testing only)  ASP.NET Cache  Prevalence Cache  Memcache Entity cache and query cache  NHibernate.Cache.HashtableCacheProvider  NHibernate.Caches.SysCache.SysCacheProvide r, NHibernate.Caches.SysCache ...

Entity Cache Via class or collection mappings  Via hibernate.cfg.xml  or Stores serialized entities, not objects Entities in 2 nd level cache not shared between sessions

Query Cache In hibernate.cfg.xml, cache.use_query_cache=true Only contains identifiers, not serialized entities IQuery.SetCacheable(true) IQuery.SetCacheRegion(string) IQuery.SetForceCacheRefresh(true)

WRAPPING IT UP

Resources NHibernate ( ) NHUsers Google Group ( ) NHibernate FAQ (

Questions James Kovacs