CSC/ECE 517 Fall 2010/ch2 2a mw: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
mNo edit summary
No edit summary
 
(37 intermediate revisions by 2 users not shown)
Line 1: Line 1:
==Introduction==
==Introduction==
In the previous article [final S24 link here], we learn that metaprogramming is where the computer code that interacts with other programs, such as databases,  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 of the styles of metaprogramming referred to as language extensions for [http://en.wikipedia.org/wiki/Object-relational_mapping object-relational mapping (ORM)].
In the [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch2_S24 previous article for which the link does not exist yet], we learn that [http://en.wikipedia.org/wiki/Metaprogramming 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 [http://en.wikipedia.org/wiki/Object-relational_mapping object-relational mapping] through examples and some comparisons of language extensions and tools.
 
==Object-Relational Mapping==
Object-Relational Mapping (ORM) is a methodology for managing data between object oriented systems and relational databases.  The premise of the concept is to provide a universal method to access data in a database.  This is beneficial for all programming languages that can use objects to store and retrieve data from the database.  There are many languages that are using ORM as a technique to manage database data.  In the rest of the article, we will discuss ORM and some of the languages in use today.


==Language Extensions==
==Language Extensions==
Language extensions for ORM have often been traditionally classified as design patterns or software tools used to perform basic [http://en.wikipedia.org/wiki/Create,_read,_update_and_delete create, read, update and delete (C.R.U.D.)] operations on relational databases, that is, until recent new approaches such as [http://en.wikipedia.org/wiki/ActiveRecord#Ruby 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 [http://en.wikipedia.org/wiki/Inheritance_%28computer_science%29 inheritance] and [http://en.wikipedia.org/wiki/Association_%28object-oriented_programming%29 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.  [http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails], first released in 2005, is able to provide a uniform method to help resolve these complicated issues without sacrificing function or knowledge of these objects 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''" [http://en.wikipedia.org/wiki/ActiveRecord#Ruby 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. 


Language extensions for ORM have often been classified as design patterns or software tools used to perform [http://en.wikipedia.org/wiki/Create,_read,_update_and_delete create, read, update and delete (C.R.U.D.)] operations on relational databases, that is, until recent new approaches such as [http://en.wikipedia.org/wiki/ActiveRecord#Ruby ActiveRecord ].  ActiveRecord is not just a design pattern it is an increase of function to the active record pattern approach.  ActiveRecord is tightly integrated support that is built into the Ruby on Rails frameworkIn this article we will examine other language extensions that try to use this same approach and then we will compare the ease of programming using these language extensions with the ease of programming using external tools.
ActiveRecord uses a [http://en.wikipedia.org/wiki/Single_Table_Inheritance 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 [http://en.wikipedia.org/wiki/Model-view-controller Model view-controller (MVC)] for Ruby on Rails but it is also a standalone ORM package for Ruby itselfRuby, 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 [http://www.ruby-lang.org/en/about/ 8]Other languages have been able to learn from AR and have tried to add this capability for themselves.  Let’s take a closer look at some other implementations that attempts to duplicate AR’s elegance.


==ActiveRecord==
==Other Examples of Language Extensions==
==Other Examples of Language Extensions==
===ADO.NET Entity Framework===
[http://en.wikipedia.org/wiki/ADO.NET_Entity_Framework ADO.NET Entity Framework], Microsoft's ORM, part of [http://en.wikipedia.org/wiki/.NET_Framework .NET] 4.0 was first developed in 2008.  The Entity Framework tries to remove ORM mismatches that often plague conventional database oriented programs by using 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===
[http://en.wikipedia.org/wiki/Django_%28web_framework%29 Django], is an ORM included in Django open source framework for Python.  It follows a MVC [http://en.wikipedia.org/wiki/Architectural_pattern_%28computer_science%29 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, [http://en.wikipedia.org/wiki/Don%27t_repeat_yourself 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.


* Java - Enterprise Objects Framework, Mac OS X/Java, part of Apple WebObjects
===Enterprise Objects Framework===
* .NET - ADO.NET Entity Framework, Microsoft's ORM, part of .Net 4.0
Lastly we will introduce [http://en.wikipedia.org/wiki/Enterprise_Objects_Framework Enterprise Objects Framework (EOF)], Mac OS X/Java, part of Apple [http://en.wikipedia.org/wiki/WebObjects WebObjects].  EOF is the oldest of the bunch, introduced in 1994 for a product call [http://en.wikipedia.org/wiki/NeXTSTEP NeXTSTEP].  It was indented to eliminate the interaction with the [http://en.wikipedia.org/wiki/Relational_database 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 [http://en.wikipedia.org/wiki/Jdbc Java Database Connectivity (JDBC)] and [http://en.wikipedia.org/wiki/JNDI Java Naming and Directory Interface (JNDI)] to talk to databases that support that, such as Oracle, DB2, and MySQL.
* Python - Django, (ORM is included in Django framework) open source


==Programming using Language Extensions==
==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 table.  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).


==Programming using External Tools==  
*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.  No external tools are required to generate the Ruby code, only the Ruby interpreter.
 
<pre>
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
</pre>
 
Now create a new Customer in Ruby, update the attributes, and save the object.  The .save method will actually update the database table.
 
<pre>
n = Customer.new
n.id = “1”
n.name = “Customer 1”
n.save
</pre>
 
*ADO.NET Entity Framework
 
ADO.NET requires that you use the [http://msdn.microsoft.com/en-us/library/bb738483.aspx Entity Model Designer] which is part of [http://msdn.microsoft.com/en-us/vstudio/default.aspx Microsoft Visual Studio].  The Designer allows you to create your model which includes any entities and associations.
 
[[Image:Ado-customer.png]]
 
The Designer will ultimately generate an XML file that represents the Entity Data Model.
<pre>
<!-- A snippit from the xml file – shows the Customer definion -->
<!-- There are not associations in out example -->
<EntityType Name = "Customer">
    <Property Name = "Name" Type = "String" />
    <Property Name = "id" Type = "String" Nullable = "false" />
</EntityType>
</pre>
 
In code for the Customer object, the setter on the class would look something like this.
<pre>
set
{
  this.CategoryReference.EntityKey = new EntityKey("CustomerEntities.Category", "id", “name”);
}
</pre>
 
*Django
 
In Django, a model is defined to represent a customer.
<pre>
  from django.db import models
  class Customer(models.Model):
    name= models.CharField(max_length=200)
</pre>
 
After creating a model for a customer it will generate a corresponding table.
Note that “id” is included as part of the model, so this might cause some unexpected issues if you planned to use id differently.
 
<pre>
  BEGIN;
  CREATE TABLE myapp_customer (
    "id" serial NOT NULL PRIMARY KEY,
          "name" varchar(200),
  );
</pre>
 
Create a new instance of a Customer and save it.
 
<pre>
c = Customer(name=’Customer 1’)
c.save()
c.id    # Returns the ID of your new object.
</pre>
 
*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.
 
Generate the Customer definition using the EOModler UI.
 
[[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>
 
As you saw in some of the examples above, 2 of the 4 language extensions require external tools to generate the hooks in to the language. 


==Conclusion==
==Conclusion==
Depending on what language you plan to use for your application will dictate what extensions and tools you will use.  If you are driven by cost, then some of the choices are obvious to remove, otherwise you might be driven by you familiarity with a particular language or existing tool set.  Either way one can see how all of the languages have attempted to simplify the process of accessing data in an external datastore.  There is no clear victor in these comparisons, and we should believe the situation will dictate which language to use.
==What’s Next?==
In the [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch2_2b next article for which the link does not exist yet], we will look at another style of metaprogramming, CRC Cards, one of the simplest methods of doing object oriented design.  These cards are a way to capture the initial relationships between objects for a given system.  CRC cards will help determine the evolution of these relationships before any code is written.  You can learn very quickly about CRC cards by taking a close look at the software used to create CRC cards, and other artifacts of o-o design.


==References==
==References==


#[http://en.wikipedia.org/wiki/Comparison_of_web_application_frameworks Comparison of web application frameworks]
#[http://en.wikipedia.org/wiki/Comparison_of_web_application_frameworks Comparison of web application frameworks]
#[http://en.wikipedia.org/wiki/List_of_object-relational_mapping_software List of object-relational mapping software]
#[http://en.wikipedia.org/wiki/List_of_object-relational_mapping_software List of object-relational mapping software (Wikipedia.org)]
#[http://en.wikipedia.org/wiki/ActiveRecord#Ruby ActiveRecord Ruby (Wikipedia.org)]
#[http://en.wikipedia.org/wiki/ADO.NET_Entity_Framework ADO.NET Entity Framework (Wikipedia.org)]
#[http://en.wikipedia.org/wiki/Django_%28web_framework%29 Django (Wikipedia.org)]
#[http://en.wikipedia.org/wiki/Enterprise_Objects_Framework Enterprise Objects Framework (Wikipedia.org)]
#[http://ruby-doc.org/docs/ProgrammingRuby/ Programming Ruby - The Pragmatic Programmer's Guide]
#[http://www.ruby-lang.org/en/about/ Ruby – A Programmers Best Friend]
#[http://www.djangoproject.com/ Django Project (Djangoproject.com)]
#[http://msdn.microsoft.com/en-us/library/e80y5yhx%28v=VS.71%29.aspx ADO.NET (Micorsoft.com)]
#[http://developer.apple.com/legacy/mac/library/documentation/LegacyTechnologies/WebObjects/WebObjects_4.5/System/Documentation/Developer/EnterpriseObjects/DevGuide/EOFDevGuide.pdf Enterprise Objects Framework (Apple.com)]

Latest revision as of 01:45, 2 October 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 through examples and some comparisons of language extensions and tools.

Object-Relational Mapping

Object-Relational Mapping (ORM) is a methodology for managing data between object oriented systems and relational databases. The premise of the concept is to provide a universal method to access data in a database. This is beneficial for all programming languages that can use objects to store and retrieve data from the database. There are many languages that are using ORM as a technique to manage database data. In the rest of the article, we will discuss ORM and some of the languages in use today.

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 or knowledge of these objects 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 capability 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 by using 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 table. 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).

  • 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. No external tools are required to generate the Ruby code, only the Ruby interpreter.

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

ADO.NET requires that you use the Entity Model Designer which is part of Microsoft Visual Studio. The Designer allows you to create your model which includes any entities and associations.

The Designer will ultimately generate an XML file that represents the Entity Data Model.

<!-- A snippit from the xml file – shows the Customer definion -->
<!-- There are not associations in out example -->
<EntityType Name = "Customer">
     <Property Name = "Name" Type = "String" /> 
     <Property Name = "id" Type = "String" Nullable = "false" />
</EntityType>
 

In code for the Customer object, the setter on the class would look something like this.

set
{
   this.CategoryReference.EntityKey = new EntityKey("CustomerEntities.Category", "id", “name”);
}
  • Django

In Django, a model is defined to represent a customer.

  from django.db import models
  class Customer(models.Model):
    name= models.CharField(max_length=200)

After creating a model for a customer it will generate a corresponding table. Note that “id” is included as part of the model, so this might cause some unexpected issues if you planned to use id differently.

  BEGIN;
  CREATE TABLE myapp_customer (
    "id" serial NOT NULL PRIMARY KEY,
          "name" varchar(200),
  );

Create a new instance of a Customer and save it.

c = Customer(name=’Customer 1’)
c.save()
c.id     # Returns the ID of your new object.
  • 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.

Generate the Customer definition using the EOModler UI.

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();

As you saw in some of the examples above, 2 of the 4 language extensions require external tools to generate the hooks in to the language.

Conclusion

Depending on what language you plan to use for your application will dictate what extensions and tools you will use. If you are driven by cost, then some of the choices are obvious to remove, otherwise you might be driven by you familiarity with a particular language or existing tool set. Either way one can see how all of the languages have attempted to simplify the process of accessing data in an external datastore. There is no clear victor in these comparisons, and we should believe the situation will dictate which language to use.

What’s Next?

In the next article for which the link does not exist yet, we will look at another style of metaprogramming, CRC Cards, one of the simplest methods of doing object oriented design. These cards are a way to capture the initial relationships between objects for a given system. CRC cards will help determine the evolution of these relationships before any code is written. You can learn very quickly about CRC cards by taking a close look at the software used to create CRC cards, and other artifacts of o-o design.

References

  1. Comparison of web application frameworks
  2. List of object-relational mapping software (Wikipedia.org)
  3. ActiveRecord Ruby (Wikipedia.org)
  4. ADO.NET Entity Framework (Wikipedia.org)
  5. Django (Wikipedia.org)
  6. Enterprise Objects Framework (Wikipedia.org)
  7. Programming Ruby - The Pragmatic Programmer's Guide
  8. Ruby – A Programmers Best Friend
  9. Django Project (Djangoproject.com)
  10. ADO.NET (Micorsoft.com)
  11. Enterprise Objects Framework (Apple.com)