Foundations of Programming – pt 7 (sorta) – ActiveRecord

You may be wondering what happened to part 6. Well, it’s still being worked on and should be available early next week.

I wasn’t sure if there would be a part 7 and if so what it would be about – but I was heavily considering writing about an ActiveRecord implementation. Turns out that Kent Sharkey, beat me to it with his overview of Subsonic on DotNetSlackers.

I want to spend just a couple paragraphs trying to tie in Kent’s article with the Foundations series.

In Part 3 we talked about persistence and manually wrote our data access layer and mapping code. As I said at the time, doing that manually is fine for simple cases with few domain objects and straightforward mapping, but can quickly get out of hand. To solve the problem and keep us efficient, we can leverage existing O/R mappers to do all the mundane work. Some O/R mappers, like NHibernate which we’ll look at in Part 6, are unbelievably flexible and can be used to address very large systems (as systems grow in size, it’s common for their impedance mismatch to grow as well, which makes a flexible tool all the more important). Of course, NHibernate isn’t the simplest thing to configure and it has a relatively steep learning curve.

An alternative approach is to use the ActiveRecord (AR) design pattern. AR is still considered an O/R mapper, but it’s specifically targeted at more straightforward applications (which likely represent 95% of the work we’re all doing). AR is one of the two components that has made Ruby on Rails so popular and productive (the other is their first class MCV pattern implementation). There are two popular .NET implementations that I’m aware of: Subsonic and Castle ActiveRecord. Both the Castle and Subsonic line of products are open source, and come with a full range of products (MVC, ActiveRecord, Scaffolding, utility functions, etc) that integrate nicely with each other. Interestingly, Castle is implemented on top of NHibernate – which gives you a hit about just how flexible NHibernate really is.

With Castle ActiveRecord, you take your domain object and decorate it with attributes. You add the ActiveRecordAttribute to your class, and the PropertyAttribute to your properties. So, if you have a Car class with a Name property which are properly set up, Castle will automatically map to a table named Car and a column named Name.

It turns out that ActiveRecord implementations can be setup within minutes and handle 99% of all your data access needs. They can make you unbelievably productive by providing functionality and performance above that of DataSets, while maintaining your rich domain layer and allow you to easily write unit tests.

It’s still worthwhile to learn a more complex framework like NHibernate for a couple reasons. First, once you’re used to it, it too can be set up quickly and efficiently (avoiding upfront learning is a losing strategy). Secondly, NHibernate is a lot closer to important fundamental concepts. With AR implementations, a lot of the details are taken care of for you – which is great in most cases, but doesn’t teach you nearly as much. If you spend some time with NHibernate, you’ll learn about first and second level caching, identity map, concurrency and locking and various other concepts relating to persistence.

Enjoy Kent’s article, and I hope take the time to download and play with either Subsonic or Castle.

Happy holidays to all our CB readers and your families. 

 

 

This entry was posted in Foundations. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

9 Responses to Foundations of Programming – pt 7 (sorta) – ActiveRecord

  1. @DevKm

    Linq for NHibernate is nothing but a new query language for NHibernate. In a way it’s one less abstraction layer because your queries are expressed a bit more clearly in the language of the domain model instead of the criteria or HQL syntax.

  2. DevKm says:

    Linq to NHibernate? Just how many layes of abstractions do we really need?

  3. Ken Egozi says:

    @Brad:
    Since Castle’s AR is build upon NH, and since Hibernate gurus would say that Hibernate can map 99.9% of O/R scenarios, then that’s you way to go.

    @Karl:
    I, personally, use Castle AR only as a nice helper around NH. So my domain objects are POCOs with ActiveRecord attributes, and data access in repositories is being done by ActiveRecordMediator, which is a class within Castle to data-manipulate POCOs. I can also access NH execute delegates if I need to perform stuff in more fine-grained level.

    This way I gain:
    1. NH flexibility and power (lazy/cache/multi-criteria/whadever)
    2. AR simplicity (no need for those hbm.xml files)

    @Seb:
    Linq is huge. super cool thing.
    However, Linq for SQL (as OR/M) is too new and immature to be actually considered for production environments.

    Now Linq for NHibernate is a promising thing – a cool combination of great stuff

  4. karl says:

    Seb: why more appropriate?

  5. Seb says:

    Wouldn’t LINQ to SQL be more appropriate if you are going to look at specific implementations of OR mapping?

  6. @Brad,

    There’s one more conceptual choice beyond ActiveRecord approaches and DataMappers like NHibernate. Take a look at iBatis.Net for ultimate control over the database to object mapping. Albeit with more work on your part, but hey, it’s a legacy database to begin with.

  7. Brad says:

    Thanks Karl –

    Like Jim, I am digging into OR/M this weekend. From what I am finding, Subsonic & Castle ActiveRecord are really good with one-to-one relationships between the domain objects and the database’s tables, much like the Car example you are using in this series. Unfortunately, I am working on applications that use legacy database structures without much chance of the data structure changing. What is the best strategy for using an OR/M in this type of situation, where a domain object may be getting & updating data in multiple tables? I’m a total n00b at using the DDD approach, so I may be missing something obvious.

    Awesome series.

  8. Kent Sharkey says:

    Thank you, Mr. Editor-Sir for the plug (and for not ripping my article apart at the punctuation)

  9. Jim Bonnie says:

    thanks karl – I have been using SubSonic for some applications and plan on starting a project with castle active record this weekend. thanks for helping make the connection between the two.

    I am excited about spending sometime with monorail as well to get into the whole mvc thing.