Castle ActiveRecord -- How to determine Session Errors

13 views
Skip to first unread message

JakeS

unread,
Nov 18, 2009, 11:05:03 PM11/18/09
to Castle Project Users
I'm still struggling a lot with my data models. My database schema is
now turning out the way I'd like (I think), but now the site is not
saving any data! I perform a simple step that should insert a row
into a table and it gets rolled-back. I watched on NHProf and see the
INSERT statements entering -- and they are valid, then at the end it
simply says "rollback transaction".

I'm using the "Session per Request" pattern I found here--
http://using.castleproject.org/display/AR/Enable+Session+per+Request.
In the OnEndRequest event the scope.HasSessionError is true. But how
do I find out what the error actually is?

Markus Zywitza

unread,
Nov 20, 2009, 2:05:39 PM11/20/09
to castle-project-users
Did you catch any exceptions? What is the stacktrace?

-Markus

2009/11/19 JakeS <jakest...@gmail.com>:
> --
>
> You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
> To post to this group, send email to castle-pro...@googlegroups.com.
> To unsubscribe from this group, send email to castle-project-u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/castle-project-users?hl=.
>
>
>

JakeS

unread,
Nov 21, 2009, 10:45:16 AM11/21/09
to Castle Project Users
No, I didn't catch any exceptions. And there is no stack-trace, since
I didn't see the actual error anywhere, it was occuring in the
OnEndRequest of the http request.

I eventually tracked down the issue -- it had to do with Databinding
accessing properties on an object. Those properties did lookups to
find data, and the object had not been saved yet. Something like
this--

public IList<Projects> GetActiveProjects
{
get
{
return Projects.FindAll(
Restrictions.Eq("Owner", this),
Restrictions.Eq("Status", ProjectStatus.Active)
);
}
}

So databinding would access this property (even though it wasn't
actually being used anywhere on the view) before the object was
actually saved. And I couldn't find any way to tell if the object was
saved or prevent access to this property by the databinder. All I
could do was put in an ugly hack that checks for an Id of 0--

public IList<Projects> GetActiveProjects
{
get
{
if(this.Id==0){return null;}
return Projects.FindAll(
Restrictions.Eq("Owner", this),
Restrictions.Eq("Status", ProjectStatus.Active)
);
}
}

I must be doing something fundamentally wrong.

On Nov 20, 1:05 pm, Markus Zywitza <markus.zywi...@gmail.com> wrote:
> Did you catch any exceptions? What is the stacktrace?
>
> -Markus
>
> 2009/11/19 JakeS <jakesteven...@gmail.com>:

G. Richard Bellamy

unread,
Nov 21, 2009, 1:17:52 PM11/21/09
to castle-pro...@googlegroups.com
I would suggest moving all methods of this nature into IRepository objects,
and using the ActiveRecordMediator<T> base, rather than ActiveRecordBase<T>.

http://www.castleproject.org/ActiveRecord/documentation/trunk/advanced/media
tor.html

This provides better SOC.

JakeS

unread,
Nov 21, 2009, 4:40:31 PM11/21/09
to Castle Project Users
Thank you for the suggestion! This might be very basic, but I'm
curious about best practices for this moving forward.

So in my situation I have a User that has been assigned to a project.
I assume I would have a UserRepo, and ProjectRepo. Then I'd add
methods to the Repos that return these relationships, rather than a
property on the User model?

So in ProjectRepo...

public static IList<User> GetProjectsFor(User user)
{
return Projects.FindAll(
Restrictions.Eq("Owner", user),
Restrictions.Eq("Status", ProjectStatus.Active)
);
}

Am I on the right track?

On Nov 21, 12:17 pm, "G. Richard Bellamy" <rbell...@pteradigm.com>
wrote:
> I would suggest moving all methods of this nature into IRepository objects,
> and using the ActiveRecordMediator<T> base, rather than ActiveRecordBase<T>.
>
> http://www.castleproject.org/ActiveRecord/documentation/trunk/advance...

G. Richard Bellamy

unread,
Nov 21, 2009, 4:55:49 PM11/21/09
to castle-pro...@googlegroups.com
Yes, you're on the right track.

Make sure you read the bottom of that page... it talks about using the
ActiveRecordMediator to build custom repository types.

public class ProjectRepo : ActiveRecordMediator<Project>
{
public static Project[] GetProjectsForUser(User user)
{
return FindAll(new ICriterion[]{Restrictions.Eq("Owner", user),
Restrictions.Eq("Status", ProjectStatus.Active)};
}
}

Please note that Enums may need special handling when being saved/retrieved
from the database.

Also, take a look at the IRepository stuff in Rhino.Commons for ideas.

-rb

JakeS

unread,
Nov 21, 2009, 5:47:01 PM11/21/09
to Castle Project Users
Thank you. I'm slowly seeing the light.

On Nov 21, 3:55 pm, "G. Richard Bellamy" <rbell...@pteradigm.com>
wrote:
Reply all
Reply to author
Forward
0 new messages