ActiveRecord and NH Filters

16 views
Skip to first unread message

Hamilton Verissimo

unread,
Nov 30, 2006, 7:42:35 AM11/30/06
to castle-pro...@googlegroups.com
I've checked the filters support on NH, that's exactly what I was
looking for the introduce the scope support. However I'm having a hard
time trying to come up with a simple API for it.

On NH you have to create a filter definition (defines the parameters)
and the filter itself which is per entity and defines the condition.

<class ...>
<filter name="abc" condtion="col = :somearg" />
</class>

<filter-def name="abc">
<filter-param name="somearg" type="bool" />
</filter-def>

To define the filter I can rely on an attribute, but I can't see how
to map the parameters names and types on the attributes. I'm also
considering a virtual method on ARBase that the ActiveRecord could
invoke during initialization to define the filters.

On the usage side, I'm thinking about some like:

using(Post.EnableScope("recentposts"))
{

}

using(Post.EnableScope("author", author.Id))
{

}

Any ideas?
Thanks

--
Cheers,
hamilton verissimo
ham...@castlestronghold.com
http://www.castlestronghold.com/

Hamilton Verissimo

unread,
Dec 4, 2006, 2:47:58 PM12/4/06
to castle-pro...@googlegroups.com
No ideas on this one? C'mon, it's a cool feature and deserves do the
added. If you're wondering, check
http://habtm.com/articles/2006/02/22/nested-with_scope

Ayende Rahien

unread,
Dec 4, 2006, 5:32:00 PM12/4/06
to castle-pro...@googlegroups.com
ActiveFilter filter = new ActiveFilter("recentPosts");
filter["date"] = DateTime.Today.AddDays(-7);
using(Post.EnableFilter(filter))
{

 //this is the best that I can think of.
}

On 11/30/06, Hamilton Verissimo <ham...@castlestronghold.com> wrote:

Ayende Rahien

unread,
Dec 4, 2006, 5:33:02 PM12/4/06
to castle-pro...@googlegroups.com
This is mainly because we are limited by what we can do with C#'s syntax, we can pass a dictionary, but that would be just ugly, IMP

Hamilton Verissimo

unread,
Dec 4, 2006, 5:40:13 PM12/4/06
to castle-pro...@googlegroups.com
But how would that work as the filter needs to be defined on the xml
mapping during the AR initialization?

Another option (which I think it's ugly but - maybe - would work)

[Scope("name", "condition here", new String[] { "max_age" }, new
Type[] { typeof(int) } )]


On 12/4/06, Ayende Rahien <aye...@ayende.com> wrote:
> ActiveFilter filter = new ActiveFilter("recentPosts");
> filter["date"] = DateTime.Today.AddDays(-7);
> using(Post.EnableFilter(filter))
> {
> //this is the best that I can think of.
> }
>

Ayende Rahien

unread,
Dec 4, 2006, 5:45:55 PM12/4/06
to castle-pro...@googlegroups.com
[Filter("recentPosts", Condition = "Date> :date"]
[Filter.Parameter("date", typeof(DateTime)]

??

Hamilton Verissimo

unread,
Dec 4, 2006, 5:50:04 PM12/4/06
to castle-pro...@googlegroups.com
That's better, but you can have more than one filter associated with a class.

[Filter("recentPosts", Condition = "Date> :date"]

[Filter.Parameter("recentPosts", "date", typeof(DateTime)]
[Filter("other", Condition = "Date> :date"]
[Filter.Parameter("other", "name", typeof(String)]


On 12/4/06, Ayende Rahien <aye...@ayende.com> wrote:
> [Filter("recentPosts", Condition = "Date> :date"]
> [Filter.Parameter("date", typeof(DateTime)]

Ayende Rahien

unread,
Dec 4, 2006, 5:59:13 PM12/4/06
to castle-pro...@googlegroups.com
[Filter("recentPosts", Condition = "Date> :date"]
[Filter.Parameter(Filter = "recentPosts", "date", typeof(DateTime)]

[Filter("other", Condition = "Date> :date"]
[Filter.Parameter(Filter = "other", "name", typeof(String)]

Where Filter= is optional if there is only one query.

On 12/5/06, Hamilton Verissimo <ham...@castlestronghold.com> wrote:

Hamilton Verissimo

unread,
Dec 4, 2006, 6:20:29 PM12/4/06
to castle-pro...@googlegroups.com
Cool! Were getting somewhere! :-)

josh robb

unread,
Dec 5, 2006, 6:17:39 AM12/5/06
to castle-pro...@googlegroups.com
What about:

public class RecentPostsFilter : IARFilter {

public RecentPostsFilter(DateTime theDate) {}

public DateTime Date { get; set; }

public string IFilter.Name { get {return "recentPosts";}}
public Dictionary<string,object> IFilter.Parameters {}
}

[Filter(typeof(RecentPostsFilter)]

RecentPostsFilter filter = new RecentPostsFilter(DateTime.AddDays(-7);

using(Post.EnableFilter(filter) {

}

I realise this is a lot more typing - but the advantages I see are:

1. Strongly typed access to parameters.
2. Raises the concept of "filter" to it's own class - which is where
it belongs IMO. (DDD - Evans talks about Specifications.)

j.

Ayende Rahien

unread,
Dec 5, 2006, 6:21:18 AM12/5/06
to castle-pro...@googlegroups.com
I like this idea.

On 12/5/06, josh robb <josh...@fastmail.fm> wrote:

What about:

public class RecentPostsFilter : IARFilter {

     public RecentPostsFilter(DateTime theDate) {}

     public DateTime Date { get; set; }

     public string IFilter.Name { get {return "recentPosts";}}
     public Dictionary<string,object> IFilter.Parameters {}
}

[Filter(typeof(RecentPostsFilter)]

RecentPostsFilter filter = new RecentPostsFilter(DateTime.AddDays(-7);

using(Post.EnableFilter (filter) {

}

I realise this is a lot more typing - but the advantages I see are:

1. Strongly typed access to parameters.
2. Raises the concept of "filter" to it's own class - which is where
it belongs IMO. (DDD - Evans talks about Specifications.)

j.

On 12/4/06, Hamilton Verissimo <ham...@castlestronghold.com> wrote:
>
> Cool! Were getting somewhere! :-)
>
> On 12/4/06, Ayende Rahien <aye...@ayende.com> wrote:
> > [Filter("recentPosts", Condition = "Date> :date"]
> > [Filter.Parameter (Filter = "recentPosts", "date", typeof(DateTime)]

Hamilton Verissimo

unread,
Dec 5, 2006, 7:09:20 AM12/5/06
to castle-pro...@googlegroups.com
Very nice. Thanks

On 12/5/06, josh robb <josh...@fastmail.fm> wrote:
>

josh robb

unread,
Dec 5, 2006, 7:19:48 AM12/5/06
to castle-pro...@googlegroups.com
Just had a thought.

It might make more sense to have the filtername on the attribute
rather than a static readonly member of the class. This would allow
the filter implementation to be reused more easily.

e.g.

class DateRangeFilter : IFilter {
public DateRangeFilter(DateTime from, DateTime to) {}
}

[Filter(typeof(DateRangeFilter), "activeDate")]
class Bar

[Filter(typeof(DateRangeFilter), "liveDate")]
clas Foo

j.

Hamilton Verissimo

unread,
Dec 5, 2006, 7:25:17 AM12/5/06
to castle-pro...@googlegroups.com
Something I don't like about NHibernate filters is that it seems to
use columns names, instead of the properties/fields. Shall we "handle"
that to use properties/fields?

Ernst Naezer

unread,
Dec 5, 2006, 7:28:46 AM12/5/06
to castle-pro...@googlegroups.com
I quess Properties + Fields would be a little easier, in that case you can also runtime detect the parameters types
but it does make it different than NH.

> > > > > > [Filter.Parameter ("recentPosts", "date", typeof(DateTime)]
Reply all
Reply to author
Forward
0 new messages