0

I have been trying to create an ISessionFactory that has a list of filters as a property (so it can be specified in the XML configuration) and enables each of the filters whenever OpenSession() is called.

Unfortunately, I have been stymied at every turn. I've subclassed LocalSessionFactoryObject and SimpleDelegatingSessionFactory, mixed-and-matched every way I can think of, but there's always some syntax or run-time error that keeps it from working.

Can anyone give me an example of how to do this?

Thanks in advance.

[Update]

I've been asked to provide some code to illustrate my issue. I don't think that's really relevant to the question I'm asking, but I can elaborate:

I figured that to make sure the filters are enables whenever a new session is opened I'd have to have my own OpenSession method. It seemed the best way to do this was to subclass DelegatingSessionFactory, add the filter-list property and a method like this

public new ISession OpenSession()
{
    var rtn = base.OpenSession();
    foreach (var filter in filters)
        rtn.EnableFilter(filter);
    return rtn;
}

When I had Spring construct this as my ISessionFactory object, though, I got runtime errors about not having an exception translator. So, I figured I'm better off also subclassing LocalSessionFactoryObject and having it create an ISessionFactory of my new type with the filter list, rather than the default type. To do this I tried to override NewSessionFactory, but then I got a runtime error about not having a DbProvider defined, and when I tried to copy the code that handles this from LocalSessionFactoryObject I got a bunch of syntax errors because of the scope of some members...

In short, something that seemed like it should be simple -- and that in fact I rather expected many to have done before me -- turned in a coding safari. That's why I'm looking for someone who's already done it, or who at least understands the framework better than I do.

Michael
  • 1,351
  • 1
  • 11
  • 25
  • I am not 100% sure of the question you are asking here. Are you saying you have some filters defined on the XML mappings and want to enable them, not at the query level but at the session level? What are these `filters` you are mentioning? Can you provide us with more detail? – Rippo May 12 '14 at 12:42
  • Yes, I am trying to call session.EnableFilter() whenever the session is opened -- which is done by OpenSessionInView -- rather than having every method that uses the session include the EnableFilter() call. The filter itself is nothing special: "ACTIVE = 1". That's all. – Michael May 13 '14 at 14:07

1 Answers1

0

But do the filters need to be active always? If that is the case, then you should instead be using Where restrictions on the entity and/or collections.

Ricardo Peres
  • 13,724
  • 5
  • 57
  • 74
  • No, there are some rare occasions when we don't want to enable the filters. – Michael May 16 '14 at 14:56
  • I am looking for something very similar. Whenever the session created, filters should be enabled. If you got the solution please share it. – Balaji Jun 10 '15 at 00:47