CSC/ECE 517 Fall 2010/ch2 2a mw: Difference between revisions
mNo edit summary |
mNo edit summary |
||
Line 76: | Line 76: | ||
*Enterprise Objects Framework | *Enterprise Objects Framework | ||
The biggest downside with EOF and WebObjects is that it is not free. The Server and development environment is fairly expensive. However, here is a quick look at how you would create an EOF model that represents the classes, attributes and the objects. | |||
[[Image:Eof-customer.png]] | |||
The code to create a new instance / database entry is fairly intuitive. | |||
<pre> | |||
EOEditingContext ec = new EOEditingContext(); | |||
Customer c = new Customer(); | |||
c.id = "1"; | |||
c.name = "Customer 1"; | |||
ec.add(c); | |||
ec.saveChanges(); | |||
</pre> | |||
==Programming using External Tools== | ==Programming using External Tools== |
Revision as of 03:35, 17 September 2010
Introduction
In the previous article for which the link does not exist yet, we learn that metaprogramming is where the computer code interacts with other programs as data and performs many of these interactions at compile time rather than runtime. This built-in interaction allows the programmers to take advantage of these capabilities and focus their time on the rest of their program logic instead of the details of some of the lower level coding. In this article, we will take a closer look at one of the styles of metaprogramming referred to as language extensions for object-relational mapping (ORM) through examples and some comparisons of language extensions and tools.
Language Extensions
Language extensions for ORM have often been traditionally classified as design patterns or software tools used to perform basic create, read, update and delete (C.R.U.D.) operations on relational databases, that is, until recent new approaches such as ActiveRecord have grown in popularity. ActiveRecord is not just a design pattern it is an increase of function to the active record pattern approach by adding inheritance and associations. Examining ActiveRecord and other language extensions will allow for comparisons of the ease of programming using these language extensions verses the conventional database oriented systems approach.
Ruby and ActiveRecord
All too often programmers are faced with the challenge of persisting objects from their program into a datastore. Custom code is created for this purpose that can be complex or difficult for others to understand as well as not seem natural. Applications that are designed to persist data have the need to know how the objects correspond to the information stored in these database tables. Ruby on Rails, first released in 2005, is able to provide a uniform method to help resolve these complicated issues without sacrificing function by using ActiveRecord (AR). AR is a persistence engine that comes as part of Ruby on Rails. It creates a 'persistable' domain model from business objects and database tables, where logic and data are presented as a unified package 3. AR is admired for its simplistic and elegant approach of removing these levels of complexity. It allows for a 'pluggable' solution to many different popular databases available today to include: MySQL, SQLite, SQL Server, PostgreSQL, and Oracle.
ActiveRecord uses a Single Table Inheritance to allow for inheritance capabilities and provides a set of macros for association relationships between classes of objects, such as belongs_to, has_one, has_many, etc. AR is not only a component of the Model view-controller (MVC) for Ruby on Rails but it is also a standalone ORM package for Ruby itself. Ruby, Ruby on Rails, and ActiveRecord continue to grow in popularity due to not only the curiosity of programmers but their ability to improve function and feature sets while maintaining the initial intent of the language, “trying to make Ruby natural, not simple” -- Yukihiro “matz” Matsumoto 8. Other languages have been able to learn from AR and have tried to add this for themselves. Let’s take a closer look at some other implementations that attempts to duplicate AR’s elegance.
Other Examples of Language Extensions
ADO.NET Entity Framework
ADO.NET Entity Framework, Microsoft's ORM, part of .NET 4.0 was first developed in 2008. The Entity Framework tries to remove ORM mismatches that often plague conventional database oriented programs. ADO.NET uses an Entity data model (EDM) and an Entity-Relationship data model to define the Relationships associated to each Entity. The EDM consists of a schema and a mapping specification. The schema defines the data types from the entities and the mapping provides the connections between the database scheme and the conceptual scheme. The Entity Framework has its own version of a query language called Entity SQL which continues to focus only on the conceptual entities and relationships as opposed to the actual database. It works with many popular databases available today to include: MySQL, SQLite, SQL Server, PostgreSQL, and Oracle.
Django
Django, is an ORM included in Django open source framework for Python. It follows a MVC architectural pattern and was originally released in 2005 with the primary goal of making complex, database driven websites easier to create and maintain. Instilled in its principles are emphasis on loose coupling, rapid development, don’t repeat yourself, and reusability (a.k.a. less code). The basis of its design is to encapsulate objects just like the ActiveRecord design pattern in models so that all the information needed can be stored in the model and knowledge of the database is not exposed. It works with many popular databases available today to include: MySQL, SQLite, MS SQL Server (use MySQL driver), PostgreSQL, and Oracle.
Enterprise Objects Framework
Lastly we will introduce Enterprise Objects Framework (EOF), Mac OS X/Java, part of Apple WebObjects. EOF is the oldest of the bunch, introduced in 1994 for a product call NeXTSTEP. It was indented to eliminate the interaction with the relational database and the Java or Objective-C objects. EOF was later integrated into WebObjects. An EOFModel contains the mappings from the database to the classes, class attributes, and objects. EOF also incorporates inheritance into its feature set to allow a more object oriented approach using Enterprise Objects to reflect the hierarchy. EOF uses Java Database Connectivity (JDBC) and Java Naming and Directory Interface (JNDI) to talk to databases that support that, such as Oracle, DB2, and MySQL.
Programming using Language Extensions
Several language extensions have been introduced in previous sections of the article. Let’s take a closer look at a specific scenario and show how each of them would implement their respective tables differently. We will demonstrate how each of the language extensions are used to create a Customer table. The Customer table is very simple; it will contain two fields (an id and name) both of which are strings.
- ActiveRecord
For ActiveRecord in Ruby on Rails, the syntax to create the table is contained within the Customer class that inherits from the ActiveRecord class.
class Customer < ActiveRecord::Migration def self.up create_table :customer do |c| c.string :name c.string :id end end def self.down drop_table :customer end end
Now create a new Customer in Ruby, update the attributes, and save the object. The .save method will actually update the database table.
n = Customer.new n.id = “1” n.name = “Customer 1” n.save
- ADO.NET Entity Framework
Gui tools to create the model It has the same structure of database which uses ADO.NET Entity Data Model.
- Django
from django.db import models class Customer(models.Model): name= models.CharField(max_length=200) id= models.CharField(max_length=200)
BEGIN; CREATE TABLE "Customer" ( "name" varchar(200) NOT NULL, "id" varchar(200) NOT NULL );
- Enterprise Objects Framework
The biggest downside with EOF and WebObjects is that it is not free. The Server and development environment is fairly expensive. However, here is a quick look at how you would create an EOF model that represents the classes, attributes and the objects.
The code to create a new instance / database entry is fairly intuitive.
EOEditingContext ec = new EOEditingContext(); Customer c = new Customer(); c.id = "1"; c.name = "Customer 1"; ec.add(c); ec.saveChanges();
Programming using External Tools
Which is better? Language Extensions or External Tools
Conclusion
What’s Next?
2b. Software for CRC cards. Software packages support the creation of CRC cards, and other artifacts of o-o design. Compare them on ease of use, and also look for evidence of how much easier they make the overall design task.
References
- Comparison of web application frameworks
- List of object-relational mapping software (Wikipedia.org)
- ActiveRecord Ruby (Wikipedia.org)
- ADO.NET Entity Framework (Wikipedia.org)
- Django (Wikipedia.org)
- Enterprise Objects Framework (Wikipedia.org)
- Programming Ruby - The Pragmatic Programmer's Guide
- Ruby – A Programmers Best Friend
- Django Project (Djangoproject.com)
- ADO.NET (Micorsoft.com)
- Enterprise Objects Framework (Apple.com)