Chair of Software Engineering. Java and C# in depth. Carlo A. Furia, Bertrand Meyer. C#: Persistence



Similar documents
Serialization. Informatik IT-Uddannelse og IT-Udvikling

Comparing SQL and NOSQL databases

ASP.NET Programming with C# and SQL Server

NoSQL systems: introduction and data models. Riccardo Torlone Università Roma Tre

Overview of Databases On MacOS. Karl Kuehn Automation Engineer RethinkDB

A COMPARATIVE STUDY OF NOSQL DATA STORAGE MODELS FOR BIG DATA

Can the Elephants Handle the NoSQL Onslaught?

HBase A Comprehensive Introduction. James Chin, Zikai Wang Monday, March 14, 2011 CS 227 (Topics in Database Management) CIT 367

Preparing Your Data For Cloud

Cloud Scale Distributed Data Storage. Jürmo Mehine

Analytics March 2015 White paper. Why NoSQL? Your database options in the new non-relational world

Integrating Big Data into the Computing Curricula

Practical Cassandra. Vitalii

Chapter 11 Map-Reduce, Hadoop, HDFS, Hbase, MongoDB, Apache HIVE, and Related

MyOra 3.0. User Guide. SQL Tool for Oracle. Jayam Systems, LLC

Raima Database Manager Version 14.0 In-memory Database Engine

Beginning C# 5.0. Databases. Vidya Vrat Agarwal. Second Edition

Why NoSQL? Your database options in the new non- relational world IBM Cloudant 1

MongoDB in the NoSQL and SQL world. Horst Rechner Berlin,

extensible record stores document stores key-value stores Rick Cattel s clustering from Scalable SQL and NoSQL Data Stores SIGMOD Record, 2010

Using Object Database db4o as Storage Provider in Voldemort

Chapter 13. Introduction to SQL Programming Techniques. Database Programming: Techniques and Issues. SQL Programming. Database applications

Architectural patterns for building real time applications with Apache HBase. Andrew Purtell Committer and PMC, Apache HBase

Structured Data Storage

How To Write A Database Program

Big Data Analytics. Rasoul Karimi

Programming Hadoop 5-day, instructor-led BD-106. MapReduce Overview. Hadoop Overview

Advanced Data Management Technologies

How To Scale Out Of A Nosql Database

Understanding NoSQL on Microsoft Azure

Lecture Data Warehouse Systems

How To Improve Performance In A Database

Composite Data Virtualization Composite Data Virtualization And NOSQL Data Stores

NoSQL Databases. Polyglot Persistence

MongoDB Developer and Administrator Certification Course Agenda

Open Source Technologies on Microsoft Azure

BigData. An Overview of Several Approaches. David Mera 16/12/2013. Masaryk University Brno, Czech Republic

Database Management System Choices. Introduction To Database Systems CSE 373 Spring 2013

Database Master User Manual

NoSQL Databases. Nikos Parlavantzas

Not Relational Models For The Management of Large Amount of Astronomical Data. Bruno Martino (IASI/CNR), Memmo Federici (IAPS/INAF)

Data Modeling for Big Data

SQL Server for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach

Understanding NoSQL Technologies on Windows Azure

MyOra 3.5. User Guide. SQL Tool for Oracle. Kris Murthy

database abstraction layer database abstraction layers in PHP Lukas Smith BackendMedia

Geodatabase Programming with SQL

Data Tool Platform SQL Development Tools

A Brief Introduction to MySQL

Querying MongoDB without programming using FUNQL

ICE for Eclipse. Release 9.0.1

The Flink Big Data Analytics Platform. Marton Balassi, Gyula Fora" {mbalassi,

NoSQL. Thomas Neumann 1 / 22

Specifications of Paradox for Windows

SAP Business Objects Business Intelligence platform Document Version: 4.1 Support Package Data Federation Administration Tool Guide

Services. Relational. Databases & JDBC. Today. Relational. Databases SQL JDBC. Next Time. Services. Relational. Databases & JDBC. Today.

Comparison of the Frontier Distributed Database Caching System with NoSQL Databases

Spark ΕΡΓΑΣΤΗΡΙΟ 10. Prepared by George Nikolaides 4/19/2015 1

Introduction to Polyglot Persistence. Antonios Giannopoulos Database Administrator at ObjectRocket by Rackspace

Data Management in the Cloud

Hadoop Ecosystem B Y R A H I M A.

Big Systems, Big Data

Adam Rauch Partner, LabKey Software Extending LabKey Server Part 1: Retrieving and Presenting Data

MADOCA II Data Logging System Using NoSQL Database for SPring-8

Perl & NoSQL Focus on MongoDB. Jean-Marie Gouarné jmgdoc@cpan.org

MapReduce. MapReduce and SQL Injections. CS 3200 Final Lecture. Introduction. MapReduce. Programming Model. Example

Lecture 10: HBase! Claudia Hauff (Web Information Systems)!

Object Relational Database Mapping. Alex Boughton Spring 2011

Using IRDB in a Dot Net Project

Sharding with postgres_fdw

Easy-Cassandra User Guide

Big Data With Hadoop

Challenges for Data Driven Systems

Title. Syntax. stata.com. odbc Load, write, or view data from ODBC sources. List ODBC sources to which Stata can connect odbc list

Micro Focus Database Connectors

1. INTRODUCTION TO RDBMS

ENHANCEMENTS TO SQL SERVER COLUMN STORES. Anuhya Mallempati #

Unit Storage Structures 1. Storage Structures. Unit 4.3

NoSQL Database Options

Object Oriented Databases. OOAD Fall 2012 Arjun Gopalakrishna Bhavya Udayashankar

An Approach to Implement Map Reduce with NoSQL Databases

HDB++: HIGH AVAILABILITY WITH. l TANGO Meeting l 20 May 2015 l Reynald Bourtembourg

Big Data Management in the Clouds. Alexandru Costan IRISA / INSA Rennes (KerData team)

3.GETTING STARTED WITH ORACLE8i

Infrastructures for big data

InfiniteGraph: The Distributed Graph Database

Instant SQL Programming

Introducing DocumentDB

Open source, high performance database

Cloudera Certified Developer for Apache Hadoop


Facebook: Cassandra. Smruti R. Sarangi. Department of Computer Science Indian Institute of Technology New Delhi, India. Overview Design Evaluation

.NET User Group Bern

FIS GT.M Multi-purpose Universal NoSQL Database. K.S. Bhaskar Development Director, FIS +1 (610)

AD A O.N. ET E Access Data Object

Microsoft Azure Data Technologies: An Overview

multiparadigm programming Multiparadigm Data Storage for Enterprise Applications

Transcription:

Chair of Software Engineering Carlo A. Furia, Bertrand Meyer C#: Persistence

Outline C# Serialization Connecting to a RDBMS with ADO.NET LINQ (Language Integrated Queries) NoSQL Solutions for C# and Java 2

C# Serialization 3

.NET serialization Binary serialization To a stream; disk; memory; network Mark the class with the Serializable attribute Mark the fields you don t want to serialize with the NonSerialized attribute XML serialization Serializes only public fields and properties Used e.g. for Web Services (Data Contracts, describing the data to be exchanged) 4

Binary serialization restrictions Differently from Java, the Serializable attribute only applies to one class. If we want descendant and supplier classes to be serializable, we have to mark them as Serializable as well As in Java, constructors are not invoked at deserialization time, so inconsistent objects may be accepted into the system As in Java, the class private attributes are part of the class default serialized form (API) 5

.NET binary serialization example [Serializable] class ClassA { public int Field1 {set; get;} public ClassB Field2 {set; get;} [NonSerialized] private string field3;... } 6

Sample serialization class ClientClass { //suppose we pass an object of type ClassA } public void serialize(object target) { //exception handling omitted } IFormatter frmt = new BinaryFormatter(); Stream str= new FileStream( filename, FileMode.Create, FileAccess.Write, FileShare.None); frmt.serialize(str, target); str.close(); 7

Sample deserialization class ClientClass {//exception handling omitted public Object deserialize() { IFormatter frmt = new BinaryFormatter(); Stream str= new FileStream( filename, FileMode.Open, FileAccess.Read, FileShare.Read); Object o = frmt.deserialize(str); str.close(); return o; }} 8

Providing a custom serialized form 1 1. Implement ISerializable 2. Implement callback, to be used at serialization time public virtual void GetObjectData(SerializationInfo inf, StreamingContext sc) { } inf.addvalue( key1,field1); inf.addvalue( key2,field2); 9

Providing a custom serialized form 2 3. Provide a specific constructor intended for deserialization protected ClassA(SerializationInfo inf, StreamingContext sc){ } Field1 = inf.getint32( key1 ); Field2 = (ClassB)inf.GetValue ( key2, typeof(classb)); 10

Serialization and Security Serialization allows access to private fields To protect the GetObjectData method you should use the SecurityPermissionAttribute with two flags: [SecurityPermissionAttribute( SecurityAction.Demand, SerializationFormatter=true)] public virtual void GetObjectData(SerializationInfo inf, StreamingContext sc) {...} Default: local machine can access private fields, intranet or internet-downloaded code cannot Hint: don t serialize sensitive information 11

Providing a custom serialized form 3 Objects are reconstructed from the inside-out: deserialization order of complex object structures matters. Implement IDeserializationCallback to do something after the standard (non-xml) deserialization process happened method OnDeserialization (Object sender) will be called after deserialization The functionality for sender (the object that initiated the callback) is not currently implemented 12

Deserialization and Schema Evolution 1 Version Tolerant Serialization (VTS) is a set of features enabled for Serializable classes An added attribute in a new version does not cause an exception anymore (is this really an improvement?) A field can be marked with the [OptionalField]attribute so that it does not trigger an exception if not present when deserializing OnSerializing, OnSerialized, OnDeserializing, OnDeserialized attributes are also available to tag methods doing something before and after (de)serialization 13

Deserialization and Schema Evolution 2 Among others, Microsoft suggests as best practices : Never remove a serialized field Never change the name or type of a serialized field When adding a new field, apply the [OptionalField] attribute Basically, try to evolve your classes as little as possible 14

Connecting to a RDBMS with ADO.NET 15

ADO.NET! Library to interact with data sources (databases, text files, Excel spreadsheets, XML files) We are now focusing on relational databases There are different Data Providers (API data provider names (DPN) listed) Odbc OleDb (Excel, Access) Oracle Sql (Microsoft SQL Server) Bdp (generic access) 16

ADO.NET Objects! DPNConnection (i.e. OdbcConnection, OleDbConnection, SqlConnection, ) DPNCommand (send SQL statements to db) DPNDataReader (result of a query as a read-only sequential stream) DataSet (in-memory representation of relational data) DPNDataAdapter (defined for each table in a DataSet, can work offline, load and persist in single batches) 17

Handling a connection SqlConnection con = new SqlConnection ( Data Source=myServerAddress; Initial Catalog=myDBName; User ID=anID; Password=aPassword ); con.open() Here a connection is established, if possible 18

ADO.NET connection pooling SQL Server can create many connection pools One per distinct connection string One per Windows Identity (if integrated security is used) One per process One per application domain 19

Database operations A DPNCommand object encapsulates a db operation SqlCommand cmd = new SQLCommand(queryString, con) For query results use DPNDataReader SqlDataReader rdr = cmd.executereader(); For inserts, updates and deletes create first a DPNCommand passing an SQL string, and then use cmd.executenonquery(); 20

Handling query result sets The DPNDataReader object iterates through the result Method Read()moves the cursor to the next row, returning false when there are no more rows Can use a string indexer to retrieve column values string name=(string)rdr[ Person_Name ] Can also use getters methods to get column values of the type specified in the getter name (e.g. GetDateTime) 21

Cleaning up Always remember to close the connection in a finally block, even if you are using some connection pool facilities finally{ // If you have read data if (rdr!=null) { rdr.close();} // Very important!!! if (con!=null) { con.close();} }... 22

Prepared statements Encapsulated, pre-compiled queries More readable, more portable Favor query optimization SqlCommand cmd = new SqlCommand ( select * from Courses where year = @Year, con); SqlParameter param = new SqlParameter(); param.parametername = @Year ; param.value = courseyear; cmd.parameters.add(param); rdr = cmd.executereader(); 23

Spring.NET Spring.NET supports ADO.NET enterprise applications Connection strings and parameter management Provider-independent exceptions, mapped to db-specific errors Classes like AdoTemplate to handle the core workflow Developers only need to implement callback interfaces to handle the mapping 24

LINQ (Language Integrated Queries) 25

Object-relational impedance mismatch Object oriented applications and relational databases implement different mathematical models A mapping layer is therefore necessary to represent objects as tables and vice versa Mapping objects to tables may be difficult and error prone From here stems the idea of ORM s and data mappers 26

Language Integrated Queries (LINQ) Access data from different sources in a unified manner Express data access logic in C#, enabling execution in a completely different environment Reduce the impedance mismatch between data models, e.g. LINQ to objects LINQ to SQL LINQ to XML 27

Sequences An unbounded data structure providing one element at the time DataReader (sequence of rows from a db) Stream (sequence of bytes) TextReader (sequence of characters) IEnumerable<T> interface models sequences Useful to think of query expressions as sequences 28

LINQ to objects: a sample query IEnumerable<string> customernames = from customer in customers //step 1: source where customer.data > 200000 //step 2: filter select customer.name; // step 3: projection It starts with the empty sequence and then transforms it into other sequences according to the following steps obtaining: 1. A sequence containing all customers in customers 2. A sequence containing customers satisfying 1. having the value of field Data > 200000 3. A sequence satisfying 1. and 2. containing the customer s names 29

Query result! Id Name Country Data 123 Reto Switzerland 100000 132 Cecilia Italy 300000 213 Hauke Germany 200000 231 Nadia Russia 400000 312 Yu China 500000 321 Viswanathan India 600000 30

Query basic structure from rangevariable datasource where filter-expression select projection-expression; rangevariable is an identifier with an optional type name: used to refer to data consistently within the query datasource is a source of a sequence of data filter-expression there can be many where in conjunction projection-expression 31

Ordering data from rangevariable datasource where filter-expression orderby ordering-expression orderingkind select projection-expression; orderingkind can be descending or omitted (ascending) There can be multiple pairs ordering-expression orderingkind, separated by comma. Each one is interpreted in relation to the previous (e.g.: order by ascending id and, within each id, by descending age) 32

LINQ to SQL Queries are consistent with LINQ to Objects The compiler helps validating the queries Only supports SQLServer A good ORM solution (but always check the generated SQL!) 33

NoSQL Solutions for C# and Java 34

http://geekandpoke.typepad.com/geekandpoke/2011/01/nosql.html 35

What s wrong with Relational DBs? Established solution Great with read-heavy web sites Scale horizontally by adding web servers Scale vertically by adding cores/cpus/ram to support higher DB traffic on the (only) DB instance Data have structure (schema) and are validated against it ACID transactions SQL is (mostly) a standard 36

Web 2.0 a.k.a. Social Web Poses new challenges that relational DBs don t address well: New kinds of traffic profiles (e.g. massive, viral variations in traffic) Switch from read-heavy to balanced read-write web sites Queries involving the absence of data Large quantities of text and images 37

Not Only SQL Movement began in 2009, now becoming a commodity" Non-relational databases would be more appropriate " " Key-Value" " Wide Column Store (HBase, Cassandra, SimpleDB)" " Document" And also XML DBs, OODBs, Graph DBs, Multi-model" 38

Commonalities of NoSQL solutions Scale horizontally very well (no RAM sharing) for large number of simple read/write operations (Web 2.0)" (Mostly) No fixed schema, no standard query language" ACID within a node, eventually consistent across cluster" Terminology is generally inconsistent, but " " All NoSQL systems store scalars (integers, strings), BLOBs, attribute-value pairs" " Some NoSQL systems store data structures (tuples, documents, objects)" 39

Key-Value Data Stores Basically hash tables " Examples: Redis, RaptorDB " Applied successfully for distributed content caching" Offer insert, delete, and index lookup" Use Map Reduce (Google patent) for indexing and searching " " Map: extract sets of key-value pairs" " Reduce: merge and sort pairs to show results" 40

Document Data Stores Store documents made up of tagged elements" Allow as values scalars, lists, other nested documents" Secondary indexes, simple query mechanisms" No schema (attribute names defined at runtime)" Examples: CoachDB, MongoDb, " Used successfully for Web 2.0 applications" 41