ActiveRecord for .NET
Hi everyone!
ActiveRecord is a pattern for accessing objects stored in databases. The idea is that you map fields (columns) in a database table to attributes of an object. So if you have an object called Person that has the attributes Name and EmailAddress, you might have a database table called Person that has columns called Name and EmailAddress. An ActiveRecord layer assists you by doing all of this mapping automatically — or at least, it makes it easy.
Inspired by others, and by Ruby on Rails, I’ve written my own ActiveRecord class for .NET. My ActiveRecord is simple: It’s just one file. Include my ActiveRecordBase class in your project and enjoy!
This is how you use my ActiveRecordBase class (actual code from an actual application I wrote):
VB:
_
Public Class Idea
Inherits ActiveRecordBase(Of Idea)
'
' Note: FetchBy(Q.Gt("date_posted", dt), "date_posted") fetches
' all the records where the date_posted column has a value
' greater than dt, and sorts the result list by the values of
' the date_posteds found.
'
Public Shared Function FetchPostedSince(ByVal dt As DateTime) As ArrayList
Return FetchBy(Q.Gt("date_posted", dt), "date_posted")
End Function
Public ReadOnly Property Subject() As String
Get
Return Data("subject")
End Get
End Property
ReadOnly Property DatePosted() As DateTime
Get
Return Data("date_posted")
End Get
End Property
End Class
Some notes:
- Preface your data-bound class with the attribute
, specifying the table name and the identity (aka primary key) column. Optionally, specify the ConnectionString to use (by default, my code looks for a connection string called ActiveRecordConnectionString).
- Make your class inherit from ActiveRecordBase, and give it your class as the generic parameter (”(Of Idea)” here).
- In your instance methods, the
Data hash is available. Access it by specifying field names from your table.
- You can have my ActiveRecordBase write changes to fields. Simply set values to Data’s keys (E.g. Data(”subject”) = newSubject). Call the .Save() method to update the database.
- The following class (Shared) methods are defined, for getting single and multiple records: FetchOneBy, FetchBy.
- The Q class contains methods for constructing logic for selecting objects from the database. Q.Gt (the function used in the example above) says that “I want to get all objects with the date_posted field greater than [the supplied DateTime].”
Enjoy!!
Download ActiveRecordBase
Databases systems currently supported: MySQL (via MySQL Connector/NET) and MS SQL Server.
Hi Sean,
I’m visiting Minneapolis for the first time and I was poking around twitter looking for a tweetup or something and that’s how I found you. You seem like the kind of person that could point me in the direction of a good, low-key place near downtown to have a drink and maybe socialize… meet some people. If you have any advice… I’d be grateful and would return the favor if you’re every in NYC. I work at the Wall St. Journal as an inhouse SEO.
Cheers! Alexandra
oh that was stupid… I didn’t notice I was leaving a comment… thought this was an email form submit. Plz don’t post, tnx!
The download link doesn’t work…is this still avaialable?