NHibernate session management strategy?

81 views
Skip to first unread message

oll

unread,
Jul 2, 2009, 4:49:44 AM7/2/09
to masstransit-discuss
Is there a Windsor LifestyleType that would allow a new session to be
created per Consume call?

We have a session manager implementation used throughout our system
that will open a new session as specified by a LifestyleType (i.e.
Transient, PerThread, PerWebRequest). I would like to open a new
session whenever a message is consumed (i.e. Consume
(UserRegistrationRequest message){}, Consume(PasswordChanged message)
{}). Is this possible?

Thanks

Chris Patterson

unread,
Jul 5, 2009, 11:03:48 AM7/5/09
to masstrans...@googlegroups.com
Could you just take a dependency on ISession in your class that consumes the message and and set the lifestyle to Transient? A new consumer class instance will be pulled from the container for each message received. This is how we've done it before.

Chris Patterson



Dru Sellers

unread,
Jul 5, 2009, 2:39:15 PM7/5/09
to masstrans...@googlegroups.com
by default we don't ship with anything like this.
we do create a new consumer / message much like controllers in MVC frameworks tend to be one controller per HTTP request. in the web frameworks you would plug into the Begin/EndRequest and set you session up there. if masstransit exposed this kind of a concept then conceptually you could do the same thing as well. usually the goal here is to have a session created and then automatically closed (flushed, transacted) for you. you could acheive the same thing by having your IoC framework automatically wrap the consumer in a decorator that would manage this for you. but that's not quite as simple as a lifestyle. taking a dependency on the ISession would work since you would have one / consumer but that might expose ISession more than you want. MassTransit has a lower level concept of message filters that might fit your needs but I am not sure that it well.

Right now I would suggest the ISession dependency and then think about how you might add some hooks to the MT pipeline to do what you suggest via a lifecycle. the lifecycle class would have to know about MT and the IoC framework but it should be doable (it will just require some work).

hope this helped,
-d

oll

unread,
Jul 6, 2009, 8:40:48 AM7/6/09
to masstransit-discuss
Thanks for the replies.

A dependency on a transient ISession doesn't really work because you'd
really want classes further down the call stack to be using the same
session as the caller.

> ... then think about how
> you might add some hooks to the MT pipeline

I'm not familiar with the internals of MT, where would one start
looking to plug into the pipeline?

Thanks again.

On Jul 5, 7:39 pm, Dru Sellers <d...@drusellers.com> wrote:
> by default we don't ship with anything like this.we do create a new consumer
> / message much like controllers in MVC frameworks tend to be one controller
> per HTTP request. in the web frameworks you would plug into the
> Begin/EndRequest and set you session up there. if masstransit exposed this
> kind of a concept then conceptually you could do the same thing as well.
> usually the goal here is to have a session created and then automatically
> closed (flushed, transacted) for you. you could acheive the same thing by
> having your IoC framework automatically wrap the consumer in a decorator
> that would manage this for you. but that's not quite as simple as a
> lifestyle. taking a dependency on the ISession would work since you would
> have one / consumer but that might expose ISession more than you want.
> MassTransit has a lower level concept of message filters that might fit your
> needs but I am not sure that it well.
>
> Right now I would suggest the ISession dependency and then think about how
> you might add some hooks to the MT pipeline to do what you suggest via a
> lifecycle. the lifecycle class would have to know about MT and the IoC
> framework but it should be doable (it will just require some work).
>
> hope this helped,
> -d
>
> On Sun, Jul 5, 2009 at 10:03 AM, Chris Patterson
> <chrisfromtu...@gmail.com>wrote:
>
>
>
> > Could you just take a dependency on ISession in your class that consumes
> > the message and and set the lifestyle to Transient? A new consumer class
> > instance will be pulled from the container for each message received. This
> > is how we've done it before.
>
> > On Jul 2, 2009, at 3:49 AM, oll wrote:
>
> > Is there a Windsor LifestyleType that would allow a new session to be
> > created per Consume call?
>
> > We have a session manager implementation used throughout our system
> > that will open a new session as specified by a LifestyleType (i.e.
> > Transient, PerThread, PerWebRequest). I would like to open a new
> > session whenever a message is consumed (i.e. Consume
> > (UserRegistrationRequest message){}, Consume(PasswordChanged message)
> > {}). Is this possible?
>
> > Thanks
>
> > Chris Patterson
> > chrisfromtu...@gmail.com

Dru Sellers

unread,
Jul 6, 2009, 2:57:32 PM7/6/09
to masstrans...@googlegroups.com
Probably the Unit Tests for the Pipeline for learning about that.
are you using windsor?
you could build a consumer that would consume the message and start an ISession like the HttpRequest stuff and then delegate sending the message to the real consumer as well.
-d

Chris Patterson

unread,
Jul 6, 2009, 9:36:24 PM7/6/09
to masstrans...@googlegroups.com
Okay, I add this tonight. The syntax for the configuration is:

            LocalBus = ServiceBusConfigurator.New(x =>
                {
                    x.ReceiveFrom("loopback://localhost/mt_client");

                    x.BeforeConsumingMessage(() => { _before.Set(); });
                    x.AfterConsumingMessage(() => { _after.Set(); });
                });


The BeforeConsumingMessage and AfterConsumingMessage delegates will be called at those respective points in the message processing. It's a multicast, so you can call it multiple times if you have multiple things to call and it will just keep adding them to the chain.

I haven't build it into any of the facilities yet, but that should not be too difficult.

I'm thinking it might be useful to have something like IBeforeMessageInterceptor and IAfterMessageInterceptor that can just be scanned from the assembly and registered automatically, but I didn't get that far yet. I'm also not sure we have the capability in our simple IObjectBuilder/IServiceLocator to handle that and this extension point if fairly explicit.

Give it a shot, the unit tests pass, but I haven't put it into play in a project yet. It should allow you to setup your ISession/unit of work around the consumption of the message fairly easily.

Chris
Chris Patterson



Reply all
Reply to author
Forward
0 new messages