Download presentation
Presentation is loading. Please wait.
Published byJuliana Rocha Guterres Modified over 6 years ago
1
DBMAN 8 Database Access Layers The ORM Pattern
Inheritance Modeling in Relational Databases SzaboZs
2
DBMAN 8 Database Access Layers The ORM Pattern
Inheritance Modeling in Relational Databases SzaboZs
3
Application layers
4
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
5
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)
6
1. Initialization string connStr 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"); }
7
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);
8
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;
9
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
10
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
11
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
12
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
13
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)
14
DBMAN 8 Database Access Layers The ORM Pattern (More details: Prog3)
Inheritance Modeling in Relational Databases SzaboZs
15
ORM C#: Entity Framework, Java: Hibernate/JPA
Python: Django ORM, SQLAlchemy Ruby on Rails PHP: Eloquent, Propel, Doctrine OE NIK, 2013
16
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
17
Example for the SQL-free approach
OE NIK, 2013
18
Example for the SQL-free approach
OE NIK, 2013
19
Java8 + Hibernate + Stream API
20
ORM in .NET = Entity Framework
SzaboZs
21
Active Record / Data Mapper
OE NIK, 2013
22
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
23
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” 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
24
ORM speed (C#)
25
ORM speed (PHP)
26
DBMAN 8 Database Access Layers The ORM Pattern
Inheritance Modeling in Relational Databases SzaboZs
27
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
28
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
29
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
30
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
31
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. SzaboZs
32
1. Single Table Inheritance
SzaboZs
33
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
34
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
35
2. Concrete Table Inheritance
SzaboZs
36
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
37
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
38
3. Class Table Inheritance
SzaboZs
39
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
40
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
41
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
42
4. Generic Table Structure
SzaboZs
43
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
44
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
45
SzaboZs
46
SzaboZs
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.