Active Record presentation at SNUG

Posted by Chris on October 03, 2006

Wednesday evening we had the last “SNUG”:http://www.snug.se/ meeting before summer. The meeting was hosted Dotway. We had a great turnout with around 30 people apart from us that arranged it, even though we informed about the meeting as late as two weeks earlier. First we had Jim Christensen from Commentor in Denmark tell us about the .NET Compact Framework and his experiences with mobile development. After that we had a break with Subway sandwiches and some great discussions. Finally I presented a session on the Active Record pattern that Martin Fowler describes in “P of EAA”:http://www.amazon.com/gp/product/0321127420/.

I have uploaded a zip file with the C# examples that I showed. They show how the Active Record pattern works in it’s most simple implementation and then how to use Castle project’s ActiveRecord framework to avoid having to do all the work yourself.

I also showed an example of how a dynamic language such as Ruby can take this even further for extreme productivity gains. The code below shows how to use the ActiveRecord implementation in Ruby on Rails by itself in a simple script.

require 'active_record'

ActiveRecord::Base.establish_connection(
  :adapter => 'sqlserver',
  :host => '127.0.0.1\SQL2005',
  :database => 'blogs',
  :username => 'blog_user',
  :password => 'secret')

class Blog < ActiveRecord::Base
  has_many :posts
end

class Post < ActiveRecord::Base
  belongs_to :blog
end

blog = Blog.new
blog.name = 'Chris Hedgate'
blog.uri = 'http://www.hedgate.net/'

post = Post.new
post.title = 'Hello, SNUG!'
post.contents = 'TBW'

blog.posts << post

blog.save

blogs = Blog.find_all

blogs.each do |blog|
  puts "#{blog.name} (#{blog.uri})"

  puts 'Posts:'
  blog.posts.each do |post|
    puts " -- #{post.title}"
  end

  puts ''
end

Finally, I promised to post some links to various things I talked about in the session.

* "Active Record pattern in P of EAA":http://www.martinfowler.com/eaaCatalog/activeRecord.html
* "Data Mapper pattern in P of EAA":http://www.martinfowler.com/eaaCatalog/dataMapper.html
* "Castle ActiveRecord implementation":http://www.castleproject.org/index.php/ActiveRecord
* "NHibernate":http://www.nhibernate.org/ Object-Relational Mapper
* "NHibernate Generics":http://www.ayende.com/projects/nhibernate-query-analyzer/generics.aspx, Ayende Rahien’s nice contrib for working with generics
* Mats Helanders tool "ObjectMapper":http://www.objectmapper.com/Home/Default.aspx

Thanks to all the people who attended the meeting. Kim, Daniel, Henrik and I look forward to seeing you at new SNUG meetings after the summer.

Trackbacks

Trackbacks are closed.

blog comments powered by Disqus