I have used this approach many times without an issue.
public class NhibernateFacility
: AbstractFacility
{
protected override void Init()
{
ReplaceLifecycleModelInspector();
Kernel
.Register(Component
.For<Configuration>()
.LifeStyle.Singleton
.UsingFactoryMethod(notUsed => new
Configuration.Configure()))
.Register(Component
.For<ISessionFactory>()
.LifeStyle.Singleton
.UsingFactoryMethod(k =>
k.Resolve<Configuration>().BuildSessionFactory()))
.Register(Component
.For<ISession>()
.LifeStyle.Transient
.UsingFactoryMethod(k =>
k.Resolve<ISessionFactory>().GetCurrentSession()));
}
private void ReplaceLifecycleModelInspector()
{
var builder = Kernel.ComponentModelBuilder;
var contributor = builder.Contributors.First(c =>
typeof(LifecycleModelInspector) == c.GetType());
builder.RemoveContributor(contributor);
builder.AddContributor(new
LifecycleModelInspectorOmittingNhibernateSession());
}
}
public class LifecycleModelInspectorOmittingNhibernateSession
: LifecycleModelInspector
{
public override void ProcessModel(IKernel kernel, ComponentModel
model)
{
if (typeof (ISession).IsAssignableFrom(model.Service)) return;
base.ProcessModel(kernel, model);
}
}
I'm using factory methods lazy load the creation of the configuration
and session factory. this isn't required, but I like it. I am required
to use a factory method for session, since there isn't a concrete
implementation of the session. I could also use a
SubDependencyReslover, but I find factory methods are simple for a
single session factory implementation. If I need more than 1 session
factory i need to use a SubDependencyReslover because factory methods
do not work with 2+ components with the same interface. I also need to
include the FactorySupportFacility to use the kernel's factory method.
since the session is resolving the session I need to account for the
Dispose Burden. that is resolved with the
LifecycleModelInspectorOmittingNhibernateSession sub class.
there is still the issue of managing when to open/disposing the
session (along with transaction management) but that doesn't directly
relate to the container. for the web I use session per view. for
service bus I use message per consumer/handler. I haven't designed a
desktop app, but for that I have read session per form works well.
On Apr 21, 11:56 am, Bobby Fallaha <[email protected]> wrote:
> Hey there,
>
> i am having issues with configuring NHibernate with castle windsor. I
> am looking for an example to follow. Do you guys have any example i
> can look at?
>
> thanks
>
> --
> You received this message because you are subscribed to the Google Groups
> "Castle Project Users" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group
> athttp://groups.google.com/group/castle-project-users?hl=en.
--
You received this message because you are subscribed to the Google Groups
"Castle Project Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/castle-project-users?hl=en.