Generic Data Access in Microsoft.NET: a Compelling Example of Inheritance, Interfaces, and the Factory Method Design Pattern OOPSLA 2004 : Design Patterns.

Slides:



Advertisements
Similar presentations
Chapter 18 - Data sources and datasets 1 Outline How to create a data source How to use a data source How to use Query Builder to build a simple query.
Advertisements

Visual Basic.NET A look into the.NET Programming Model Bryan Jenks Integrated Ideas ©2005.
Tiger Computer Services Ltd Using Interfaces in.NET Simplifying access to multiple DB providers in.NET Liam
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.
15 Chapter 15 Web Database Development Database Systems: Design, Implementation, and Management, Fifth Edition, Rob and Coronel.
ADO. NET. What is “ADO.Net”? ADO.Net is a new object model for dealing with databases in.Net. Although some of the concepts are similar to the classical.
Performed by:Gidi Getter Svetlana Klinovsky Supervised by:Viktor Kulikov 08/03/2009.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Chapter 14 Database Connectivity and Web Technologies
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
Overview of Database Access in.Net Josh Bowen CIS 764-FS2008.
Introduction to ADO Entity Framework ir Denis VOITURON Source:
Visual Basic.NET A look into the.NET Programming Model Bryan Jenks Integrated Ideas ©2005.
Joe Hummel, PhD Dept of Mathematics and Computer Science Lake Forest College Lecture 8: WebForms — Web-based.
Architecture Of ASP.NET. What is ASP?  Server-side scripting technology.  Files containing HTML and scripting code.  Access via HTTP requests.  Scripting.
Joe Hummel, PhD Dept of Mathematics and Computer Science Lake Forest College
ADO.NET – part II August 2004 [ Marmagna Desai]. CONTENTS ADO vs ADO.NET ADO.NET – Managed providers Connecting to Database SqlConnection Selecting Database.
4-1 INTERNET DATABASE CONNECTOR Colorado Technical University IT420 Tim Peterson.
ODBC : Open Database Connectivity SNU OOPSLA Lab. October 2005.
PHP Data Object (PDO) Khaled Al-Sham’aa. What is PDO? PDO is a PHP extension to formalise PHP's database connections by creating a uniform interface.
Microsoft Visual Basic 2005: Reloaded Second Edition
ASP.NET  ASP.NET is a web development platform, which provides a programming model, a comprehensive software infrastructure and various services required.
COLD FUSION Deepak Sethi. What is it…. Cold fusion is a complete web application server mainly used for developing e-business applications. It allows.
Joe Hummel, PhD Dept of Mathematics and Computer Science Lake Forest College
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Needs for Accessing Database To make your web site more dynamic and maintainable, you can display information on your web pages that are retrieved from.
CS795/895: Introduction. Topics Distributed Systems –Availability –Performance –Web Services Security –Authentication –Authorization –Confidentiality.
Databases and Data Access  Introduction to ADO.NET  ADO.NET objects  ADP.NET namespaces  Differences between ADO and ADO.NET.
.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.
MySQL Connection using ADO.Net Connecting to MySQL from.NET Languages.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
Database, SQL, and ADO.NET- Part 1 Session 11 Mata kuliah: M0874 – Programming II Tahun: 2010.
Copyright ©2004 Virtusa Corporation | CONFIDENTIAL ADO.Net Basics Ruwan Wijesinghe Trainer.
CSCI 6962: Server-side Design and Programming Database Manipulation in ASP.
What is database?  Any Method for access info into Application from DataBase?  ODBC is standard for Accessing Data.  Problem with ODBC:  Information.
Joe Hummel, PhD Dept of Mathematics and Computer Science Lake Forest College
© 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:
Chapter 38 Persistence Framework with Patterns 1CS6359 Fall 2011 John Cole.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Joe Hummel, PhD Dept of Mathematics and Computer Science Lake Forest College
Domain Driven Web Development With WebJinn Sergei Kojarski College of Computer & Information Science Northeastern University joint work with David H. Lorenz.
ADO.NET connections1 Connecting to SQL Server and Oracle.
1 11/10/05CS360 Windows Programming ADO.NET. 2 11/10/05CS360 Windows Programming ADO.NET  Behind every great application is a database manager o Amazon.
1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query.
CS212: Object Oriented Analysis and Design Lecture 17: Virtual Functions.
PHP Workshop ‹#› PHP Data Object (PDO). PHP Workshop ‹#› What is PDO? PDO is a PHP extension to formalise PHP's database connections by creating a uniform.
PHP Workshop ‹#› أطلق إبداعك 2 أطلق إبداعك 2 مدرس معتمد من مركز زووم PHP Data Object (PDO)
Database Security Cmpe 226 Fall 2015 By Akanksha Jain Jerry Mengyuan Zheng.
Advanced Object-oriented Design Patterns Creational Design Patterns.
CS 325: Software Engineering March 19, 2015 Applying Patterns (Part B) Code Smells The Decorator Pattern The Observer Pattern The Template Method Pattern.
Introduction to Database C# MySQL onnect-C-to-MySQL 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.
.NET Data Access and Manipulation
Joe Hummel, the compiler is at your service Chicago Coder Conference, June 2016.
 ADO.NET is an object-oriented set of libraries that allows you to interact with data sources  Commonly, the data source is a database, but it could.
ASP.NET Programming with C# and SQL Server First Edition
Data Access with ADO.NET
PHP Training at GoLogica in Bangalore
ADO.NEXT Advances in Data Access for 2008
An Introduction to Entity Framework
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Introduction of Week 11 Return assignment 9-1 Collect assignment 10-1
Chapter 10 ADO.
Tutorial 6 PHP & MySQL Li Xu
CS 325: Software Engineering
Updating Databases With Open SQL
M S COLLEGE OF ART’S, COMM., SCI. & BMS Advance Web Programming
Updating Databases With Open SQL
Presentation transcript:

Generic Data Access in Microsoft.NET: a Compelling Example of Inheritance, Interfaces, and the Factory Method Design Pattern OOPSLA 2004 : Design Patterns and Objects First Workshop Joe Hummel, PhD Dept of Math/CS Lake Forest College

2 OOPSLA 2004 Introductions… Joe Hummel, PhD –Chicago, USA – –web: I wear 2 hats: –Academic: Associate Prof. of CS at Lake Forest College ( PhD from UC-Irvine (Optimizing Compilers, 1998) –Industry: professional trainer (1-5 day workshops, webcasts, conferences) specializing in Microsoft Windows development

3 OOPSLA 2004 Part 1 The problem…

4 OOPSLA 2004 Database access in.NET is vendor-specific A different set of classes for each technology: –ODBC –OLEDB –SQL Server –Oracle –DB2 –… Saving grace: –Each set of classes is based on a common design: Connection object to open connection with DB Command object for executing SQL DataReader object for reading records etc. DB

5 OOPSLA 2004 Naïve result Developers create a distinct class for each DB type: MSAccessDB SQLServerDB OracleDB MySQLDB

6 OOPSLA 2004 Goal Apply good OOD to obtain reusable, maintainable result: GenericDataAccess MSAccessDBSQLServerDBOracleDB MySQLDB

7 OOPSLA 2004 Killer example? Real Important Inheritance Interfaces Factory Method design pattern

8 OOPSLA 2004 Part 2 From naïve to gOOd solution…

9 OOPSLA 2004 Outline 4-step process: 1.identify generic vs. vendor-specific data access code 2.apply Factory Method design pattern 3.use interfaces to define common design across Microsoft’s classes 4.use inheritance to: –ensure our generic & concrete data access classes integrate properly –enable single, polymorphic access to different types of databases GenericDataAccess db; db = new ConcreteDataAccessClass (…); db.Insert(…); db.Update(…); GenericDataAccess db; db = new ConcreteDataAccessClass (…); db.Insert(…); db.Update(…);

10 OOPSLA 2004 Step 1 Identify generic vs. vendor-specific… System.Data.SqlClient.SqlConnection conn; System.Data.SqlClient.SqlCommand cmd; System.Data.SqlClient.SqlDataReader reader; conn = new System.Data.SqlClient.SqlConnection(“connection info…”); cmd = new System.Data.SqlClient.SqlCommand(“Select * …”, conn); conn.Open(); // open connection to DB reader = cmd.ExecuteReader(); // execute SQL and return object for accessing DB… while ( reader.Read() ) // for each record… {. } conn.Close(); System.Data.SqlClient.SqlConnection conn; System.Data.SqlClient.SqlCommand cmd; System.Data.SqlClient.SqlDataReader reader; conn = new System.Data.SqlClient.SqlConnection(“connection info…”); cmd = new System.Data.SqlClient.SqlCommand(“Select * …”, conn); conn.Open(); // open connection to DB reader = cmd.ExecuteReader(); // execute SQL and return object for accessing DB… while ( reader.Read() ) // for each record… {. } conn.Close(); SQL Server

11 OOPSLA 2004 Step 2 Apply Factory Method design pattern… ??? conn; ??? cmd; ??? reader; conn = CreateConnection(“connection info…”); cmd = CreateCommand(“Select * …”, conn); conn.Open(); // open connection to DB reader = cmd.ExecuteReader(); // execute SQL and return object for accessing DB… while ( reader.Read() ) // for each record… {. } conn.Close(); ??? conn; ??? cmd; ??? reader; conn = CreateConnection(“connection info…”); cmd = CreateCommand(“Select * …”, conn); conn.Open(); // open connection to DB reader = cmd.ExecuteReader(); // execute SQL and return object for accessing DB… while ( reader.Read() ) // for each record… {. } conn.Close(); private ??? CreateConnection(string connectInfo) { return new System.Data.SqlClient.SqlConnection(connectInfo); }. private ??? CreateConnection(string connectInfo) { return new System.Data.SqlClient.SqlConnection(connectInfo); }.

12 OOPSLA 2004 Step 3 Use interfaces to define common design across DB classes… System.Data.IDbConnection conn; System.Data.IDbCommand cmd; System.Data.IDataReader reader; conn = CreateConnection(“connection info…”); cmd = CreateCommand(“Select * …”, conn); conn.Open(); // open connection to DB reader = cmd.ExecuteReader(); // execute SQL and return object for accessing DB… while ( reader.Read() ) // for each record… {. } conn.Close(); System.Data.IDbConnection conn; System.Data.IDbCommand cmd; System.Data.IDataReader reader; conn = CreateConnection(“connection info…”); cmd = CreateCommand(“Select * …”, conn); conn.Open(); // open connection to DB reader = cmd.ExecuteReader(); // execute SQL and return object for accessing DB… while ( reader.Read() ) // for each record… {. } conn.Close(); private IDbConnection CreateConnection(string connectInfo) { return new System.Data.SqlClient.SqlConnection(connectInfo); }. private IDbConnection CreateConnection(string connectInfo) { return new System.Data.SqlClient.SqlConnection(connectInfo); }.

13 OOPSLA 2004 Step 4 Use inheritance & abstract base class to enforce design… public abstract class GenericDataAccess { public void Insert(…) { … } public void Update(…) { … } protected abstract IDbConnection CreateConnection(string info); protected abstract IDbCommand CreateCommand(string SQL, IDbConnection conn); } public abstract class GenericDataAccess { public void Insert(…) { … } public void Update(…) { … } protected abstract IDbConnection CreateConnection(string info); protected abstract IDbCommand CreateCommand(string SQL, IDbConnection conn); } public class SQLServerDB : GenericDataAccess { protected IDbConnection CreateConnection(string info) { return new System.Data.SqlClient.SqlConnection(info); }. public class SQLServerDB : GenericDataAccess { protected IDbConnection CreateConnection(string info) { return new System.Data.SqlClient.SqlConnection(info); }.

14 OOPSLA 2004 End result? Reusable, maintainable data access hierarchy! Single, polymorphic database access! GenericDataAccess db1, db2, db3; db1 = new SQLServerDB (…); db2 = new MSAccessDB (…); db3 = new OracleDB (…); db1.Insert(…); db2.Update(…); db3.Delete(…); GenericDataAccess db1, db2, db3; db1 = new SQLServerDB (…); db2 = new MSAccessDB (…); db3 = new OracleDB (…); db1.Insert(…); db2.Update(…); db3.Delete(…); GenericDataAccess MSAccessDBSQLServerDBOracleDB

15 OOPSLA 2004 Part 3 Extensions…

16 OOPSLA 2004 Possible extensions Specialized exception handling: –different databases respond differently to error conditions –define generic exception handler in base class –derived classes can override & specialize if desired –Template Method design pattern Data access code in.NET really consists of 2 levels: –low-level code that performs database access: execute Action query (insert, update, delete) server-side Select query client-side Select query –high-level code for domain-specific stuff: validation, build SQL, etc. –redesign data access hierarchy to take this into account…

17 OOPSLA 2004 Other possible extensions (These are from the workshop discussion…) Abstract Factory pattern: –derived classes are really producing objects from a family of classes –should recognize this via Abstract Factory pattern Configuration pattern: –make system more dynamic by reading class names from a file –apply Configuration pattern

18 OOPSLA 2004 Application in CS1 / CS2? Perhaps a similar problem involving files instead of DBs… –we give students a set of classes for reading / writing text files –they apply 4 steps outlined earlier.txt TxtReader.html HtmlReader.xml XmlReader

19 OOPSLA 2004 Other ideas for CS1 / CS2 I’ve used the following successfully in CS1 / CS2 sequence: –compilers use AST as an internal representation –AST (abstract syntax tree) involves: non-trivial inheritance hierarchy Visitor design pattern –I had students build a recursive-descent parser I provided lexer they designed AST & built parser ASTNode Stmt Expr WhileAssignIfBinaryLiteral

20 OOPSLA 2004 That's it! Questions? Discussion? Thank you!