AR: Using stored procedures for querying

0 views
Skip to first unread message

c.sokun

unread,
May 7, 2008, 10:53:11 PM5/7/08
to Castle Project Development List
I would love to be able to map to the view or stored proc the way AR
map object to table.
Have any one had experience with this?

Patrick Steele

unread,
May 7, 2008, 11:14:40 PM5/7/08
to castle-pro...@googlegroups.com
Yes. Define your AR class as you normally would for a table -- but
take into account two things:

1) Mark the class as Mutable = false

[ActiveRecord(Mutable=false)]
public class OneDayTotals : ActiveRecordBase<OneDayTotals>

2) Make sure your view returns a unique primary key for each row.

c.sokun

unread,
May 8, 2008, 12:03:25 AM5/8/08
to Castle Project Development List
Thanks Patrick, that work perfectly except when I call :
ActiveRecordStarter.CreateSchema();
OneDayTotals is created as a Table.

So what I am thinking now is to run this command :
ActiveRecordStarter.GenerateCreationScripts("create.sql");
then make change to the create.sql file so when I deploy my
application I would not run ActiveRecordStarter.CreateSchema(); but
ActiveRecordStarter.CreateSchemaFromFile("create.sql"); that would
give me more control.

Is that how thing should work? Or I missed any AR feature?

On May 8, 10:14 am, "Patrick Steele" <patrick.ste...@gmail.com> wrote:
> Yes. Define your AR class as you normally would for a table -- but
> take into account two things:
>
> 1) Mark the class as Mutable = false
>
> [ActiveRecord(Mutable=false)]
> public class OneDayTotals : ActiveRecordBase<OneDayTotals>
>
> 2) Make sure your view returns a unique primary key for each row.
>

Patrick Steele

unread,
May 8, 2008, 6:55:59 AM5/8/08
to castle-pro...@googlegroups.com
Oooo... Yeah, I never thought about that. My situation involved an
existing schema so I never used CreateSchema(). :)

Your solution to tweak the generate SQL file sounds fine. I wonder if
there's an attribute to tell ActiveRecord to skip the SQL generation?

Roy.L...@gmail.com

unread,
May 8, 2008, 9:02:44 AM5/8/08
to Castle Project Development List
I used the facilities of NHibernate 1.2, and did a
"RegisterCustomMapping" call. This could be done after you have
generated your schema. I contributed my solution as a patch to AR-156,
but it died on the vine. Read my blog entry for an idea of how it
could be done with an older version of Castle's ActiveRecord library.
The trickiest part is determining the order of parameters that will be
sent to your stored procedure. If NHibernate could stabilize this,
the rest is very straight-forward.

http://roytate.blogspot.com/2007/03/castle-activerecord-calling-stored.html

Roy Tate
Reply all
Reply to author
Forward
0 new messages