DBMAN 8 Database Access Layers The ORM Pattern

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.
Bruce Scharlau, University of Aberdeen, 2012 Data storage options for mobiles Mobile Computing.
Chapter 10: Designing Databases
.NET Database Technologies: Open-Source Frameworks.
Chapter 10 ADO. What is ADO? ADO is a Microsoft technology ADO stands for ActiveX Data Objects ADO is a programming interface to access data in a database.
Object-Oriented Databases v OO systems associated with – graphical user interface (GUI) – powerful modeling techniques – advanced data management capabilities.
Object Relational Mapping. What does ORM do? Maps Object Model to Relational Model. Resolve impedance mismatch. Resolve mapping of scalar and non-scalar.
1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Session-01. Hibernate Framework ? Why we use Hibernate ?
ADO.NET – part II August 2004 [ Marmagna Desai]. CONTENTS ADO vs ADO.NET ADO.NET – Managed providers Connecting to Database SqlConnection Selecting Database.
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
ADO.NET A2 Teacher Up skilling LECTURE 3. What’s to come today? ADO.NET What is ADO.NET? ADO.NET Objects SqlConnection SqlCommand SqlDataReader DataSet.
Intro to C#.net and EF Ilan Shimshoni. The Three Faces of ADO.NET The connected layer – Directly connecting to the DB The disconnected layer – Using datasets.
CS 474 Database Design and Application Terminology Jan 11, 2000.
.NET Data Access and Manipulation ADO.NET. Overview What is ADO.NET? Disconnected vs. connected data access models ADO.NET Architecture ADO.NET Core Objects.
© 2007 by Prentice Hall 1 Introduction to databases.
1 Introduction to ADO.NET Microsoft ADO.NET 2.0 Step by Step Rebecca M Riordan Microsoft Press, 2006.
Database, SQL, and ADO.NET- Part 1 Session 11 Mata kuliah: M0874 – Programming II Tahun: 2010.
Database Design and Management CPTG /23/2015Chapter 12 of 38 Functions of a Database Store data Store data School: student records, class schedules,
ASP.NET Rina Zviel-Girshin Lecture 5
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Introduction to ADO.NET ADO.NET - Lesson 01  Training time: 10 minutes  Author:
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
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.
Efficient RDF Storage and Retrieval in Jena2 Written by: Kevin Wilkinson, Craig Sayers, Harumi Kuno, Dave Reynolds Presented by: Umer Fareed 파리드.
Database Management Systems (DBMS)
V 1.0 DBMAN 9 Inheritance Modeling + Relational Databases 1.
Mapping Objects ↔Relational DB. The Problem with Databases Databases store data in rows in tables, which are not like objects. We can simulate object.
V 1.0 OE-NIK HP 1 Advanced Programming Database access layers DbConnection/DbReader (Not required!) DataSet (Not required!) LINQ / Entity Framework.
ORM Basics Repository Pattern, Models, Entity Manager Ivan Yonkov Technical Trainer Software University
CS 440 Database Management Systems Stored procedures & OR mapping 1.
ADO .NET from. ADO .NET from “ADO .Net” Evolution/History of ADO.NET MICROSOFT .NET “ADO .Net” Evolution/History of ADO.NET History: Most applications.
Hibernate Java Persistence API. What is Persistence Persistence: The continued or prolonged existence of something. Most Applications Achieve Persistence.
Relational vs. Object Oriented Database Management System Syazwani Nur Dayana Nur Fatin Syafiqa M3cs2305B.
.NET Data Access and Manipulation
Introduction to Database Programming with Python Gary Stewart
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
OpenAccess ORM Advanced Topics Kevin Babcock April 9, 2009.
ASP.NET Programming with C# and SQL Server First Edition
Data Access with ADO.NET
Introduction to Database Processing with ADO.NET
Introduction to Database Processing with ADO.NET
“Introduction To Database and SQL”
Modeling Constraints Extracting constraints is what modeling is all about. But how do we express them? Examples: Keys: social security number uniquely.
Business System Development
Relational Model.
ADO.NET Framework.
Relational Database Management System
MongoDB Er. Shiva K. Shrestha ME Computer, NCIT
A very brief introduction
Object-Oriented Database Management System (ODBMS)
Overview of Data Access
Entity Framework By: Casey Griffin.
DBMAN 8 Inheritance Modeling in Relational Databases
Application Development Theory
ADO.NET Entity Framework
Overview of Data Access
CS 174: Server-Side Web Programming February 12 Class Meeting
“Introduction To Database and SQL”
File Systems and Databases
CS 440 Database Management Systems
Web programming and advanced development techniques
Data Model.
Chapter 10 ADO.
Introduction to Data Structure
Developing and testing enterprise Java applications
M S COLLEGE OF ART’S, COMM., SCI. & BMS Advance Web Programming
CMPE/SE 131 Software Engineering March 7 Class Meeting
Presentation transcript:

DBMAN 8 Database Access Layers The ORM Pattern Inheritance Modeling in Relational Databases SzaboZs

DBMAN 8 Database Access Layers The ORM Pattern Inheritance Modeling in Relational Databases SzaboZs

Application layers

DbConnection vs DataSet vs Entity Framework SQL-based access, SQL statements in strings Results as object arrays Fast, but VERY uncomfortable to use This lesson DataSet Usage of a strongly-typed OOP layer on top of the SQL layer GUI-centered, unique approach  reinvent the wheel, make it square, and make it work only in MS products… Entity Framework Use the ORM (Object Relational Mapping) principle Generic approach with well-built design patterns Tables are treated as in-memory collection of instances Programming 3 Az „Alapvető műveletek” listán fogunk mindegyik módszernél végigmenni

ADO.NET: DbConnection/DbReader Old-fashioned data access (connected mode) Advantage: fast, simple SQL statements Cons: hard to modify and to change technology/storage mode, must handle if the connection is lost, hard to manage the types Different implementations for different database servers Common base classes for the common operations Connection: DbConnection SQL/RPC execution: DbCommand Read SQL results: DbDataReader Descendant classes for the different database servers: SqlConnection (MSSQL System.Data.SqlClient), MySqlConnection (MySQL MySql.Data.MySqlClient), NpgsqlConnection (PostgreSQL - Npgsql), OracleConnection (Oracle System.Data.OracleClient / ODAC & ODP)

1. Initialization string connStr = @"Data Source=.\SQLEXPRESS;Initial Catalog=nikdb;User ID=nik;Password=kin"; SqlConnection conn; private void button15_Click(object sender, EventArgs e) { conn = new SqlConnection(connStr); conn.Open(); MessageBox.Show("CONNECTED"); }

1. Initialization (MySQL, Postgre) case server_type.mysql: FConnStr = "server = " + FServer + ";\n" + "database = " + FDb + ";\n" + "user id = " + FUser + ";\n" + "password = " + FPass + ";\n"; FDatabase_Connection = new MySqlConnection(FConnStr); break; case server_type.postgre: FDatabase_Connection = new NpgsqlConnection(FConnStr);

1. Initialization (Oracle) case server_type.oracle: FConnStr = "data source = " + FServer + ";\n" + "user id = " + FUser + ";\n" + "password = " + FPass + ";\n"; FDatabase_Connection = new OracleConnection(FConnStr); break;

2. INSERT private void button18_Click(object sender, EventArgs e) { SqlCommand comm = new SqlCommand("insert into EMP (ENAME, MGR, DEPTNO, EMPNO) values ('BILL', NULL, 20, 1000)", conn); SqlDataReader reader=comm.ExecuteReader(); MessageBox.Show(reader.RecordsAffected.ToString()); reader.Close(); } OR: ExecuteNonQuery

3. UPDATE private void button18_Click(object sender, EventArgs e) { SqlCommand comm = new SqlCommand("update EMP set ENAME='JOE' where EMPNO=1000", conn); SqlDataReader reader=comm.ExecuteReader(); MessageBox.Show(reader.RecordsAffected.ToString()); reader.Close(); } OR: ExecuteNonQuery

4. DELETE private void button18_Click(object sender, EventArgs e) { SqlCommand comm = new SqlCommand("delete from EMP where empno=1000", conn); SqlDataReader reader=comm.ExecuteReader(); MessageBox.Show(reader.RecordsAffected.ToString()); reader.Close(); } OR: ExecuteNonQuery

5. SELECT private void button14_Click(object sender, EventArgs e) { listBox1.Items.Clear(); SqlCommand comm = new SqlCommand("select * from EMP where sal>=3000 order by ename", conn); SqlDataReader reader = comm.ExecuteReader(); while (reader.Read()) listBox1.Items.Add(reader["ENAME"].ToString()); } reader.Close(); Cél: azon nevekkel feltölteni egy listboxot, akiknek a fizetése >= 3000

Problems SQL Injection: We must be extra cautious that the user input MUST NEVER be interpreted as an SQL command string uName = "root", uPass = "adminpass"; string sql = $"SELECT * FROM users WHERE username='{uName}' AND userpass=sha2('{uPass}'); uPass = "x') OR 1=1 OR 1<>sha2('x"; The business logic will contain the SQL code, that has to be changed when changing the server / dialect / data structure The business logic should NEVER depend on the data storage Solution: Prepared statements (SQL command parameters, today) Use a strongly typed, strongly OOP-based layer with same (or similar) functionality on top of the physical SQL layer (in Programming 3)

DBMAN 8 Database Access Layers The ORM Pattern (More details: Prog3) Inheritance Modeling in Relational Databases SzaboZs

ORM C#: Entity Framework, Java: Hibernate/JPA Python: Django ORM, SQLAlchemy Ruby on Rails PHP: Eloquent, Propel, Doctrine OE NIK, 2013

ORM layers Raw data access layer to execute true SQL commands (commands, parameters, results  DbCommand, …) Dialect-independent conversion: execute operations regardless of the actual SQL dialect underneath Object persistence: query results translated into objects and collections, can be persisted across operations Table  Class mapping SQL-free application: write data operations in a non-sql way, either in lambda expressions (c#/java) or using a new query language (doctrine) The actual ORM: The impression of working with an in-memory data structure represented as an object graph OE NIK, 2013

Example for the SQL-free approach OE NIK, 2013

Example for the SQL-free approach OE NIK, 2013

Java8 + Hibernate + Stream API

ORM in .NET = Entity Framework SzaboZs

Active Record / Data Mapper OE NIK, 2013

Active Record / Data Mapper Simple Easy to learn Better suited for CRUD, as CRUD is built into the data objects Coupled DB Performance bottlenecks Data Mapper Flexibility (class / table isn’t necessarily 1:1) Better suited for DDD / SOLID Can be faster in SOME cases (good configuration!) Hard to configure/set-up CRUD is executed via a repository A lot better to use with unit tests OE NIK, 2013

ORM = Anti-Pattern? “ORM is a terrible anti-pattern that violates all principles of object-oriented programming, tearing objects apart and turning them into dumb and passive data bags. There is no excuse for ORM existence in any application” http://www.yegor256.com/2014/12/01/orm-offensive-anti-pattern.html I do not agree with this article – nor does most of the developers: Hibernate for Java, ActiveRecord for Ruby on Rails, Doctrine for PHP, and SQLAlchemy for Python ... And Entity Framework for C# But it must be noted, that ORM frameworks tend to be slow / weak in funcionality / bad in memory management!!! SzaboZs

ORM speed (C#)

ORM speed (PHP)

DBMAN 8 Database Access Layers The ORM Pattern Inheritance Modeling in Relational Databases SzaboZs

OOP OOP = Classes  Subclasses  Objects ; There can be various relations between classes/objects Association/Dependency: “accidental” connection (e.g. used as a parameter) Aggregation: “strong association”, separate lifetimes Composition: “strong aggregation”, connected lifetimes Inheritance: specialization Instance: create an instance from a class Question: how to map these OOP relations in tables? How does the Data Mapper (Table  Class mapping) work? SzaboZs

RDBMS common relations No need to know the latin names: in an ER, we usually see the English version of the relationship names Holonymy (HAS-A) and Meronymy (PART-OF) tree <HAS-A/CONTAINS> branch finger <PART-OF> hand Hyponymy/Hypernymy (IS-A/TYPE-OF): Hyponym <IS-A> hypernym Subtype, subclass <IS-A> Supertype, superclass CarClass <IS-A> VehicleClass INSTANCE-OF: BMW 116i <IS-AN-INSTANCE-OF> CarClass SzaboZs

HAS-A / PART-OF In the OOP world: a simple data field/parameter with the type of “Something” or “List<Something>” Same implementation used for all the different forms of Dependency/Association/Aggregation/Composition, the logical meaning of the relationship is independent from the implementation In an RDBMS, These relationships are described using simple FK connections In an ER diagram x CONTAINS y / x BELONGS TO y 1:N or M:N  depends on the actual meaningful objects and the relationship type SzaboZs

INSTANCE-OF / IS-A INSTANCE-OF: instantiation bmw116i <INSTANCE-OF> car In DB: usually we have a type-table, an instance-table, and then 1:N is good for most of the languages N:M if multiple base types or all the implemented interfaces must be stored as well (also, see later: generic table approach) IS-A: inheritance car <IS-A> vehicle <IS-A> gameObject In DB: not so trivial… Same data fields for base and descendant classes, but tables are not inherited… SzaboZs

OOP mapping to map (v): The act of determining how objects and their relationships are persisted in permanent data storage, in this case relational databases.   mapping (n): The definition of how an object’s property or a relationship is persisted in permanent storage. http://www.agiledata.org/essays/mappingObjects.html http://www.ibm.com/developerworks/library/ws-mapping-to-rdb/ https://martinfowler.com/eaaCatalog/ SzaboZs

1. Single Table Inheritance SzaboZs

1. Single Table Inheritance - Advantages Simple approach. Easy to add new classes, you just need to add new columns for the additional data. Supports polymorphism by simply changing the type of the row. Data access is fast because the data is in one table. Ad-hoc reporting is very easy because all of the data is found in one table. SzaboZs

1. Single Table Inheritance - Disadvantages Coupling within the class hierarchy is increased because all classes are mapped to the same table A change in one class will affect the table which can then affect the other classes in the hierarchy. Space potentially wasted Indicating the type becomes complex when significant overlap between types exists. Table can grow quickly for large hierarchies.  ideal for simple and/or shallow class hierarchies where there is little or no overlap between the types within the hierarchy. SzaboZs

2. Concrete Table Inheritance SzaboZs

2. Concrete Table Inheritance - Advantages Easy to do ad-hoc reporting as all the data you need about a single class is stored in only one table.  Good performance to access a single object’s data. SzaboZs

2. Concrete Table Inheritance - Disadvantages When you modify a class you need to modify its table and the table of any of its subclasses.  Whenever an object changes its role, you need to copy the data into the appropriate table and assign it a new ID value (or perhaps you could reuse the existing ID value – GUID???).  It is difficult to support multiple roles and still maintain data integrity. For example, where would you store the name of someone who plays two sports?  When changing types and/or overlap between types is rare. SzaboZs

3. Class Table Inheritance SzaboZs

3. Class Table Inheritance – Advantages Easy to understand because of the one-to-one mapping.  Supports polymorphism very well as you have records in the appropriate tables for each type.  Very easy to modify superclasses and add new subclasses as you merely need to modify/add one table. Data size grows in direct proportion to growth in the number of objects. SzaboZs

3. Class Table Inheritance – Disadvantages There are many tables in the database, one for every class (plus tables to maintain relationships).  Potentially takes longer to read and write data using this technique because you need to access multiple tables.  (can be improved using striped RAID!)  Ad-hoc reporting on your database is difficult, unless you add views to simulate the desired concrete tables.  When there is significant overlap between types or when changing types is common. SzaboZs

One table per hierarchy One table per concrete class Comparison Factors to Consider One table per hierarchy One table per concrete class One table per class Ad hoc reporting Simple Medium Medium / Difficult Ease of implementation Difficult Ease of data access Medium / Simple Coupling Very high High Low Speed of data access Fast Medium / Fast Support for polymorphism SzaboZs

4. Generic Table Structure SzaboZs

4. Generic Table Structure – Advantages Only structure to support multiple inheritance (e.g. C++) Works very well when database access is encapsulated by a robust persistence framework. It can be extended to provide metadata to support a wide range of other mapping strategies, including relationship and instance mappings It is incredibly flexible, enabling you to quickly change the structure of the objects SzaboZs

4. Generic Table Structure – Disadvantages Very advanced technique that can be difficult to implement at first. It only works for small amounts of data because you need to access many database rows to build a single object. You will likely want to build a small administration application to maintain the meta data.  Reporting against this data can be very difficult due to the need to access several rows to obtain the data for a single object.  For complex applications that work with small amounts of data, or where you data access isn’t very common or you can pre-load data into caches. SzaboZs

SzaboZs

SzaboZs