views:

627

answers:

6

I'm starting a new project and i've recently found castle project activerecord, which seems like a GREAT solution, but at the same time, it looks like something realy unconventional. I was wondering, does this feeling comes from learning something new (and i should just get used to it) or is really bad practice?

+4  A: 

ActiveRecord is a design pattern first named by Martin Fowler in Patterns of Enterprise Application Architectures. It is fairly common and used extensively in the popular Ruby framework Rails.

It contrasts with the more usual style of development in the .Net world which is to use DAOs, and that perhaps explains why you're uneasy.

A suggestion: read the source code for some Ruby on Rails applications which are similar to your own projects, and evaluate how you like the design style that results from heavy use of ActiveRecord.

Morendil
Though this is the accepted answer, it's not quite the right answer, because it talks about the ActiveRecord pattern, but not about Castle ActiveRecord, which the questioner is referring to. Castle ActiveRecord can be used in a style similar to the ActiveRecord pattern, which intermingles the objects with the persistence method; but it can also be used in a style which leaves the objects distinct from the persistence layer, as I describe in my answer.
Kyralessa
+3  A: 

It's not a bad solution but it has it's downsides.

In Patterns of Enterprise Application Architecture Martin Fowler describes several ways of designing applications that are built on top of a database. These methods differ in the way the application is decoupled from the database. He also describes that more decoupling makes more complex applications possible. Active Record is described as a way to design simpler applications but for applications with more complex behaviour you need a Domain Model that is independent of the database and something like an object-relational mapper in between.

Mendelt
i would appreciate it if you could should some examples in which using active record is a bad solution.
Orentet
A: 

ActiveRecord works very well in Ruby, but it's not easily transferable to all languages. The central feat of AR is the metaphor of table=class, row=instance. This comes out quite elegant in Ruby, because classes are also objects. In other languages, classes are usually a special kind of construct, and then you have to go through all sorts of hoops to make it work like properly. This takes away some of the natural feel that it has in Ruby.

troelskn
A: 

No it's not bad practice. Even in .NET it's a fairly well established pattern now. SubSonic (http://subsonicproject.com) and LINQ to SQL also use the pattern.

Implementations of the pattern, such as Subsonic, are great for quickly and easily creating a data access layer that manages the CRUD for your application.

That doesn't mean it's a good solution for all systems. For large, complex systems you probably want to have less coupling to the data store.

Joe R
A: 

The mixture of the domain object with the service layer is the biggest bad practice (if you see that as a bad practice). You end up calling user.Save() which means if you want to change your ORM, you are reliant on this pattern. The 2 alternatives are a layer aka a set of facade classes to perform your CRUD operations, or to put this inside the domain object as something like

User.Service.Save(user);

If you're using .NET then ActiveRecord is obviously ActiveRecord based, Coolstorage, Subsonic and a few others.

Chris S
+4  A: 

Part of what felt weird to me about using ActiveRecord was having to inherit from ActiveRecordBase<T>, and having all those persistence methods on your object (Save and so forth).

But it turns out you don't have to! Instead of having, say:

[ActiveRecord]
class Customer : ActiveRecordBase<Customer> { }

You can just have

[ActiveRecord]
class Customer : inherit from whatever you want { }

and then use ActiveRecordMediator<Customer>. It has basically the same static methods that ActiveRecordBase<T> has, but this way you don't have to clutter your object model with them. If you don't need the various protected method event hooks in ActiveRecordBase<T>, this can make things simpler.

Kyralessa

related questions