Friday, January 9, 2009

Database Abstraction and Encapsulation

Database Abstraction

Database Abstraction and Encapsulation Database Abstraction: A layer of communication that unifies communication between an application and multiple types of databases (eg. MySQL, Oracle, MSSQL, etc) Database Encapsulation: An abstraction of the database that hides the underlying table structure. Definition: A layer of communication that unifies communication between an application and multiple types of databases (eg. MySQL, Oracle, MSSQL, etc) Image Sourc Allows for better portability of applications. Useful when connecting to multiple types of databases at the same time. Necessary for applications that are seeking wide distribution among many different environments. (eg. CMS) Normally provides excellent caching support. Since applications are not calling the SQL statements directly, the performance suffers greatly. Features of the abstraction layer are degraded to the lowest common feature set. Languages of the abstraction layer are a mixture of standard SQL and custom abstraction layer functions. Consensus among many: Generally not needed in custom applications. Too limited due to only being able to use the lowest common denominator of features. Condensing database transactions into their own classes is usually the best solution. Enter database encapsulation!! Definition: An abstraction of the database that hides the underlying table structure. Organizes all data related operations in one place. Objects -> Tables -> Objects Definition: An abstraction of the database that hides the underlying table structure. Image Sourc Definition: An abstraction of the database that hides the underlying table structure. Image Source Views Stored Procedures Packages Triggers Definition:“virtual” table that is a composite of multiple real tables. Pros: Structure of the real tables can be changed, but the application code can stay the same. (AKA: provides a layer of abstraction to the database application) Multiple views can contain the same data without redundant information in the real tables. Cons?: UPDATE, INSERT, and DELETE cannot be used on a view. Definition: A function that takes arguments to execute predefined SQL statements. Image Sourc Pros: Wicked fast. Hides the structure of the underlying database. Cons: Database dependent. Another codebase to manage. Definition: A collection of procedures and variables that can be logically grouped together. Can be thought of as an object. Eg. EMPLOYEE Package EMPLOYEE.fire_employee(integer dept_id) EMPLOYEE.hire_employee(integer dept_id) EMPLOYEE.salary_raise(float percent_raise) Definition: A collection of procedures and variables that are logically grouped together. Image Sourc Pros: Collects all operations of an entity into a single object. Fast because the entire package is stored in memory instead of on the hard disk. Cons: Again, these are very heavily database dependent. Again, another code base to manage. Description: Like stored procedures, but executed automatically when an INSERT, UPDATE, or DELETE is called. Image Source Pros: Allows enforcement of referential integrity. Provide transparent event logging. Maintain table replicates. Cons: Cascading triggers can cause hard to diagnose problems. Again, triggers can be database dependent. Again, another code base to manage. Cascading Triggers Image Source Views can be easily cached. Stored Procedures are compiled and stored in database. Triggers are complied and stored in the database. A secondary code base that has to be managed. Harder to switch databases. Do the performance benefits and the amount of granular control outweigh the lack of portability and complexity of an encapsulated database? Is it easier to create application code that encapsulates database operations? Eg.) Functions that return queries that a view would normally return. Database Triggers: http://dbserv.sinp.msu.ru/DOC/server/doc/SCN73/ch15.htm Database Encapsulation: http://www.agiledata.org/essays/implementationStrategies.h Oracle Packages: http://thinkoracle.blogspot.com/2005/10/oracle-packages.htm Stored Procedures and Packages: http://stanford.edu/dept/itss/docs/oracle/10g/server.101/b10 Writing Portable SQL: http://www.internationaldeveloper.com/Writing_Portable_SQL Database Layers Must Die: http://jeremy.zawodny.com/blog/archives/002194.html

No comments:

Post a Comment