ORM Basics Repository Pattern, Models, Entity Manager Ivan Yonkov Technical Trainer Software University

Slides:



Advertisements
Similar presentations
ORM Technologies and Entity Framework (EF)
Advertisements

Software Quality Assurance QA Engineering, Testing, Bug Tracking, Test Automation Software University Technical Trainers SoftUni Team.
Using MongoDB with.NET Welcome to the JSON-stores world SoftUni Team Technical Trainers Software University
AngularJS Services Built-in and Custom Services SoftUni Team Technical Trainers Software University
Software University Curriculum, Courses, Exams, Jobs SoftUni Team Technical Trainers Software University
PHP Basics Course Introduction SoftUni Team Technical Trainers Software University
Fundamentals SoftUni Welcome to Software University SoftUni Team Technical Trainers Software University
Project Tracking Tools Trello, Asana, Basecamp, GitHub Issue Tracker, TRAC SoftUni Team Technical Trainers Software University
AngularJS Directives Defining Custom Directives SoftUni Team Technical Trainers Software University
Software Testing Lifecycle Exit Criteria Evaluation, Continuous Integration Ivan Yonkov Technical Trainer Software University.
Design Patterns: Structural Design Patterns
NoSQL Databases NoSQL Concepts SoftUni Team Technical Trainers Software University
Composer packages Installing and Using Composer, Packagist, Packaging your code Mario Peshev Technical Trainer Software University
Consuming REST Services from C# SoftUni Team Technical Trainers Software University
MVC Advanced & Reflection Reflection, Parsing Dynamic Data, Asynchronous Requests SoftUni Team Technical Trainers Software University
Database APIs and Wrappers
Entity Framework Performance SoftUni Team Technical Trainers Software University
Microsoft Azure SoftUni Team Technical Trainers Software University
Svetlin Nakov Technical Trainer Software University
Build Processes and Continuous Integration Automating Build Processes Software University Technical Trainers SoftUni Team.
Processing Redis with.NET How to Operate with Redis Databases SoftUni Team Technical Trainers Software University
Project Tracking Tools Trello, Asana, Basecamp, GitHub Issue Tracker, TRAC Angel Georgiev Part-time Trainer Software University
MVC Concepts Basics Model-View-Controller (MVC) Concepts for Web Developers SoftUni Team Technical Trainers Software University
Test-Driven Development Learn the "Test First" Approach to Coding SoftUni Team Technical Trainers Software University
ORM Technologies and Entity Framework (EF) ORM Concepts, Entity Framework, DbContext, CRUD Operations SoftUni Team Technical Trainers Software University.
Trainers Team Ivan Yonkov Rated in the top 7% at Stack Overflow
AMD and RequireJS Splitting JavaScript Code into Dependent Modules Software University Technical Trainers SoftUni Team.
Using SQL Connecting, Retrieving Data, Executing SQL Commands, … Svetlin Nakov Technical Trainer Software University
Asynchronous Web Services Writing Asynchronous Web Services SoftUni Team Technical Trainers Software University
Sessions and Cookies State Management, Cookies, Sessions, Hidden Fields SoftUni Team Technical Trainers Software University
Jekyll Static Site Generator Template-Based Site Generation Svetlin Nakov Technical Trainer Software University
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool The problem fixed by ORM Advantage Hibernate Hibernate Basic –Hibernate sessionFactory –Hibernate Session.
Forms Overview, Query string, Submitting arrays, PHP & HTML, Input types, Redirecting the user Mario Peshev Technical Trainer Software.
High-Quality Code: Course Introduction Course Introduction SoftUni Team Technical Trainers Software University
Design Patterns: Structural Design Patterns General and reusable solutions to common problems in software design Software University
Advanced C# Course Introduction SoftUni Team Technical Trainers Software University
Object-Oriented Programming Course Introduction Svetlin Nakov Technical Trainer Software University
Mocking with Moq Tools for Easier Unit Testing SoftUni Team Technical Trainers Software University
Mocking with Moq Mocking tools for easier unit testing Svetlin Nakov Technical Trainer Software University
Test-Driven Development Learn the "Test First" Approach to Coding Svetlin Nakov Technical Trainer Software University
Programming for Beginners Course Introduction SoftUni Team Technical Trainers Software University
Sets, Dictionaries SoftUni Team Technical Trainers Software University
High-Quality Code: Course Introduction Course Introduction SoftUni Team Technical Trainers Software University
Functional Programming Data Aggregation and Nested Queries Ivan Yonkov Technical Trainer Software University
Software Technologies Course Overview SoftUni Team Technical Trainers Software University
Programming Fundamentals Course Introduction SoftUni Team Technical Trainers Software University
Doctrine The PHP ORM SoftUni Team Technical Trainers Software University
Team Work and Personal Skills Course Introduction Angel Georgiev Part-time Trainer Software University
Creating Content Defining Topic, Creating Technical Training Materials SoftUni Team Technical Trainers Software University
First Steps in PHP Creating Very Simple PHP Scripts SoftUni Team Technical Trainers Software University
Inheritance Class Hierarchies SoftUni Team Technical Trainers Software University
Stacks and Queues Processing Sequences of Elements SoftUni Team Technical Trainers Software University
Java OOP Advanced Course Introduction SoftUni Team Technical Trainers Software University
Entity Framework (EF) ORM and Entity Framework Code First. CRUD Operations SoftUni Team Technical Trainers Software University
Introduction to Entity framework
Introduction to MVC SoftUni Team Introduction to MVC
Deploying Web Application
PHP MVC Frameworks Course Introduction SoftUni Team Technical Trainers
Introduction to Entity Framework
ASP.NET Integration Testing
ASP.NET Unit Testing Unit Testing Web API SoftUni Team ASP.NET
EF Relations Object Composition
Entity Framework: Code First
Databases advanced Course Introduction SoftUni Team Databases advanced
Entity Framework: Relations
MVC Architecture, Symfony Framework for PHP Web Apps
Transactions in Entity Framework
Databases Advanced Course Introduction SoftUni Team Databases Advanced
Best practices and architecture
Extending functionality using Collections
Presentation transcript:

ORM Basics Repository Pattern, Models, Entity Manager Ivan Yonkov Technical Trainer Software University

Table of Contents 1.ORM Technologies – Basic Concepts 2.Data Access Classes 3.Repository Pattern 4.Examining DB Schema 5.Mapping Tables to Classes 6.Entity Manager 2

Introduction to ORM Technologies What is Object-Relational Mapping (ORM)?

4  Object-Relational Mapping (ORM) is a programming technique for automatic mapping data and schema  Between relational database tables and object-oriented classes and objects  ORM creates a "virtual object database"  Can be used from within the programming language (C# or Java…)  ORM frameworks automate the ORM process  A.k.a. Object-Relational Persistence Frameworks ORM Technologies

ORM Frameworks  ORM frameworks typically provide the following functionality:  Creating object model by database schema (DB first model)  Creating database schema by object model (code first model)  Querying data by OO API (e.g. LINQ queries, Criteria API)  Data manipulation operations  CRUD – create, retrieve, update, delete  ORM frameworks automatically generate SQL to perform the requested data operations 5

ORM Advantages and Disadvantages  Object-relational mapping (ORM) advantages  Developer productivity: writing less code  Abstract from differences between object and relational world  Complexity hidden within the ORM  Manageability of the CRUD operations for complex relationships  Easier maintainability  Disadvantages:  Reduced performance (due to overhead or incorrect ORM use)  Reduces flexibility (some operations are hard for implementing) 6

Data Access Layer

8  Data Access Layer (DAL) is a layer of an application which simplifies access to data in a persistent storage (database, files…)  Usually done in an object oriented manner where an object returns necessary persisted data Data Access Layer

9  In applications where DAL is implemented in order to build an ORM, it usually implements the Active Record Pattern  Active Record Pattern obligates objects to provide interface for common CRUD operations  Insert  Delete  Update  Reads data by specifying columns and values Data Access Layer (2)

10  Pseudo code of an Active Record Object API Data Access Layer (3) $user = new User(); $user->insert([“name” => “Ivan”, “nickname” => “RoYaL”]); $user->save(); $user = Users::findFirst(“nickname”, “RoYaL”); $user->update(“ ”, $user->update(“age”, “22”); $user->save(); $users = Users::findAll(“name”, “pesho”); $users->delete();

11 Data Access Layer (4)

12 Data Access Layer (5)

Repository Pattern

14  The Repository Pattern upgrades the Active Record in terms of Data Access in order to provide strong object oriented access to Data Access Objects Access Objects (a.k.a Entities)  Objects implementing the pattern usually have methods for querying the data different ways and methods for finalizing the query  filterById(), filterByUsername(), filterBy ()  findOne(), find(), delete() Repository Pattern

15 Repository Pattern (1)

16 Repository Pattern (2) $repository = new UsersRepository(); $users = $repository ->filterByUsername(“ivan”) ->find(); // returns User[] foreach ($users as $user) { } $repository->filterByUsername(“pesho”)->delete(); // deletes all peshos $abv = $repository->filterBy (Criteria::ENDS_WITH, foreach ($abv as $user) { echo $abv->getUsername(). “ ”; }

17 Repository Pattern (3)

Examining Database Schema Getting Table Names and Metainformation

19  In order to create repository classes we need to examine the database schema.  It’s hard and unmaintainable to do it manually  Every time the schema changes, the classes need to change their interface as well  The process have to be automated somehow Examining Database Schema

20  Getting table names in MySQL Examining Database Schema (1)  Getting columns meta information SHOW TABLES; SHOW COLUMNS FROM `tableName`;

Mapping Tables to Classes

22  New files have to be created via file_put_contents() or fwrite()  Files represent one repository and one model per table  Table names are got from the SHOW TABLES; statement  Looping over table columns to create filtering methods in the Repository and properties in the Model  Common logic is abstracted. Mapping Tables to Classes

Entity Manager

24  Entity Manager is an object that is associated with a persistent context  Maintains the repositories and entities  Attaches, detaches entities  Exposes fluent API Entity Manager

25  PHP Data Persistence With Doctrine ORM (M. Romer) PHP Data Persistence With Doctrine ORM (M. Romer) Resources

? ? ? ? ? ? ? ? ? ORM Basics

License  This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" licenseCreative Commons Attribution- NonCommercial-ShareAlike 4.0 International 27

Free Software University  Software University Foundation – softuni.orgsoftuni.org  Software University – High-Quality Education, Profession and Job for Software Developers  softuni.bg softuni.bg  Software Facebook  facebook.com/SoftwareUniversity facebook.com/SoftwareUniversity  Software YouTube  youtube.com/SoftwareUniversity youtube.com/SoftwareUniversity  Software University Forums – forum.softuni.bgforum.softuni.bg