21.4 Simplifying Data Access with Castle ActiveRecord

There are a handful of proven methods for data access, including the Data Access Layer (DAL) pattern, O/RM tools, or just coding the SQL statements yourself and retrieving the data using ADO.NET. ActiveRecord is a well-known pattern to access row-based data and wrap it into a class. Each row in the data table is represented by a class instance, and the class’s methods act on the whole set of rows. Suppose you have a table called Customer in your database. You’d create a class called Customer and add instance methods like Create( ), Update( ), and Delete( ), and static methods like FindById( ), FindByName( ), and FindAll( ). Then you’d code them to save the object state and retrieve it from the database.

Castle ActiveRecord provides a working implementation of the ActiveRecord pattern. With Castle ActiveRecord, your class just needs to extend ActiveRecordBase, and persistence methods like Create( ), Update( ), and Delete( ) are automatically provided. The base class also provides great functionality to easily manipulate the data. Under the hood, Castle ActiveRecord uses NHibernate for database interaction; however, a great advantage of Castle ActiveRecord is that you don’t have to work with XML mapping files—all class-to-table mapping is handled through clear, understandable attributes.

CastleActiveRecord at a Glance

Tool

Castle ActiveRecord ...

Get Windows Developer Power Tools now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.