A Solution to the Object-Relational Mismatch
Total Page:16
File Type:pdf, Size:1020Kb
Universidade do Minho Escola de Engenharia Miguel Esteves CazDataProvider: A solution to the object-relational mismatch Outubro de 2012 Universidade do Minho Escola de Engenharia Departamento de Informática Miguel Esteves CazDataProvider: A solution to the object-relational mismatch Dissertação de Mestrado Mestrado em Engenharia Informática Trabalho realizado sob orientação de Professor José Creissac Campos Outubro de 2012 To my parents... \Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice." Christopher Alexander Abstract Today, most software applications require mechanisms to store information persistently. For decades, Relational Database Management Systems (RDBMSs) have been the most common technology to provide efficient and reliable persistence. Due to the object-relational paradigm mismatch, object oriented applications that store data in relational databases have to deal with Object Relational Mapping (ORM) problems. Since the emerging of new ORM frameworks, there has been an attempt to lure developers for a radical paradigm shift. However, they still often have troubles finding the best persistence mechanism for their applications, especially when they have to bear with legacy database systems. The aim of this dissertation is to discuss the persistence problem on object oriented applications and find the best solutions. The main focus lies on the ORM limitations, patterns, technologies and alternatives. The project supporting this dissertation was implemented at Cachapuz under the Project Global Weighting Solutions (GWS). Essentially, the objectives of GWS were centred on finding the optimal persistence layer for CazFramework, mostly providing database interoperability with close-to-Structured Query Language (SQL) querying. Therefore, this work provides analyses on ORM patterns, frameworks, alternatives to ORM like Object-Oriented Database Management Systems (OODBMSs). It also describes the implementation of CazDataProvider, a .NET library tool providing database interoperability and dynamic query features. In the end, there is a performance comparison of all the technologies debated in this dissertation. The result of this dissertation provides guidance for adopting the best persistence technology or implement the most suitable ORM architectures. Key Words: ORM, SQL, RDBMS, Domain Model, ADO.NET, NHibernate, Entity Framework (EF). ii Resumo Hoje, a maioria dos aplica¸c~oesrequerem mecanismos para armazenar informa¸c~ao persistentemente. Durante d´ecadas, as RDBMSs t^emsido a tecnologia mais comum para fornecer persist^enciaeficiente e confi´avel.Devido `aincompatibilidade dos paradigmas objetos-relacional, as aplica¸c~oesorientadas a objetos que armazenam dados em bases de dados relacionais t^emde lidar com os problemas do ORM. Desde o surgimento de novas frameworks ORM, houve uma tentativa de atrair programadores para uma mudan¸caradical de paradigmas. No entanto, eles ainda t^em muitas vezes dificuldade em encontrar o melhor mecanismo de persist^enciapara as suas aplica¸c~oes,especialmente quando eles t^emde lidar com bases de dados legadss. O objetivo deste trabalho ´ediscutir o problema de persist^enciaem aplica¸c~oesorientadas a objetos e encontrar as melhores solu¸c~oes.O foco principal est´anas limita¸c~oes,padr~oes e tecnologias do ORM bem como suas alternativas. O projeto de apoio a esta disserta¸c~aofoi implementado na Cachapuz no ^ambitodo Projeto GWS. Essencialmente, os objetivos do GWS foram centrados em encontrar a camada de persist^enciaideal para a CazFramework, principalmente fornecendo interoperabilidade de base de dados e consultas em SQL. Portanto, este trabalho fornece an´alisessobre padr~oes,frameworks e alternativas ao ORM como OODBMS. Al´emdisso descreve a implementa¸c~aodo CazDataProvider, uma biblioteca .NET que fornece interoperabilidade de bases de dados e consultas din^amicas. No final, h´auma compara¸c~aode desempenho de todas as tecnologias discutidas nesta disserta¸c~ao. O resultado deste trabalho fornece orienta¸c~aopara adotar a melhor tecnologia de persist^enciaou implementar as arquiteturas ORM mais adequadas. Key Words: ORM, SQL, RDBMS, Domain Model, ADO.NET, NHibernate,EF. iv Acknowledgements It is with immense gratitude that I acknowledge the support and help of Prof Dr. Jos´e Creissac Campos who managed to carefully review my dissertation even on a tight sched- ule. I also thank Prof Dr. Ant´onioNestor Ribeiro for some solid and experienced advice. To my parents and girlfriend who have been very patient with me. I would like to thank Cachapuz for the scholarship and especially Eduardo Pereira for considering and helping the development of my work. Finally, I thank my friends particularly my colleague Ricardo Santos for all the dis- cussions and ideas we debated together at Cachapuz. v Acknowledgements Acknowledgements vi Contents 1 Introduction1 1.1 Context of Work................................2 1.2 Persistence Problem..............................4 1.3 Objectives....................................5 1.4 Structure of Dissertation............................6 2 Object Relational Mapping Theory7 2.1 Object Paradigm................................9 2.2 Relational Paradigm.............................. 11 2.3 ORM as a Paradigm.............................. 13 2.4 The ORM Commitment............................ 17 2.4.1 Inheritance............................... 18 2.4.1.1 Table-per-class........................ 18 2.4.1.2 Table-per-concrete-class................... 21 2.4.1.3 Table-per-class-family.................... 22 2.4.2 Associations............................... 23 2.4.3 Schema complications......................... 27 2.4.4 OID (object identity).......................... 28 2.4.5 Data retrieval.............................. 30 2.4.6 Partial-Object dilemma and Load Time trap............. 31 2.4.7 Transparent Persistence........................ 33 2.5 Alternatives to ORM.............................. 34 2.6 Conclusions................................... 40 3 Design Patterns for ORM 43 3.1 Domain Logic Patterns............................. 44 3.1.1 Transaction Script........................... 44 3.1.2 Table Module.............................. 47 vii CONTENTS CONTENTS 3.1.3 Domain Model............................. 54 3.1.4 Making a Decision........................... 58 3.2 Data Source Architectural Patterns...................... 61 3.2.1 Table Data Gateway.......................... 61 3.2.2 Active Record.............................. 62 3.2.3 Data Mapper.............................. 64 3.3 Object-Relational Behavioural Patterns.................... 68 3.3.1 Unit of Work.............................. 69 3.3.2 Identity Map.............................. 75 3.3.3 Lazy Load................................ 76 3.4 Object-Relational Metadata Mapping Patterns................ 82 3.4.1 Metadata Mapping........................... 82 3.4.2 Query Object.............................. 84 3.4.3 Repository................................ 86 3.5 Conclusions................................... 87 4 Object Relational Mapping Frameworks 89 4.1 Entity Framework................................ 90 4.1.1 Unit of Work.............................. 91 4.1.2 Optimistic Locking........................... 93 4.1.3 Code Customization.......................... 94 4.1.4 POCOs................................. 94 4.1.5 Testing EF............................... 96 4.1.5.1 Configuration and model testing.............. 96 4.1.5.2 Basic querying........................ 99 4.1.5.3 Eager and deferred load................... 100 4.1.6 Dynamism in EF............................ 102 4.2 NHibernate................................... 104 4.2.1 Unit of Work.............................. 106 4.2.2 Optimistic Locking........................... 108 4.2.3 Lazy Load................................ 108 4.2.4 Code Customization: Audit Logging................. 111 4.2.5 Testing NHibernate........................... 115 4.2.5.1 Simple Load and Identity Map............... 118 4.2.5.2 Linq-to-NHibernate join query examples.......... 119 4.2.5.3 Lazy Collections examples.................. 121 viii CONTENTS CONTENTS 4.2.5.4 Cascading Delete operations................. 125 4.2.5.5 HQL examples........................ 126 4.2.5.6 Dynamic LINQ........................ 129 4.2.5.7 Database Synchronization.................. 130 4.2.6 Dynamism in NHibernate....................... 132 4.3 Conclusions................................... 132 5 Implementation of CazDataProvider 135 5.1 Analysis of ClassBuilder............................ 136 5.1.1 Relational Domain Model....................... 137 5.1.2 Data Mapper.............................. 139 5.1.3 Unit of Work and Optimistic Locking................. 140 5.1.4 Audit Logging.............................. 143 5.1.5 Conclusions............................... 144 5.2 .NET Data Providers.............................. 146 5.3 Designing an Architecture........................... 151 5.3.1 Solution 1: Data Context Facade and Factory............ 152 5.3.2 Solution 2: Provider Factory...................... 154 5.3.3 Solution 3: Abstract Provider Factory................ 155 5.3.4 Solution 4: Provider Factory with Subclassing............ 156 5.3.5 Solution 5: Provider Factory with Template Method........ 159 5.3.6 Solution 6: Query Object....................... 163 5.4 Implementation................................