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.
| |
|---|---|
Tool | Castle ActiveRecord ... |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access
