Thanks Miguel,
That helps alot! Just one question: how do you handle the ISession in
unit tests? Do you use injection or do you just create the ISession
manually and then pass it around?
Dave
On Nov 18, 6:42Â am, Miguel Madero <
m...@miguelmadero.com> wrote:
> That's the same link that I referred to. I forgot to mentioned about
> disposing the ISession. This is important if you don't use the
> InRequestScope method.
> In the App you will have to add the following (from the blog post):
>
> Â public MvcApplication()
> Â Â Â Â {
> Â Â Â Â Â Â this.EndRequest += MvcApplication_EndRequest;
> Â Â Â Â }
>
> Â Â Â Â private void MvcApplication_EndRequest(object sender,
> System.EventArgs e)
> Â Â Â Â {
> Â Â Â Â Â Â if (Context.Items.Contains(NHibernateModule.SESSION_KEY))
> Â Â Â Â Â Â {
> Â Â Â Â Â Â Â Â NHibernate.ISession Session = (NHibernate.ISession)
> Context.Items[NHibernateModule.SESSION_KEY];
> Â Â Â Â Â Â Â Â Session.Dispose();
> Â Â Â Â Â Â Â Â Context.Items[NHibernateModule.SESSION_KEY] = null;
> Â Â Â Â Â Â }
>
> Â Â Â Â }
>
> On Wed, Nov 18, 2009 at 11:19 PM, Jason Dentler <
jasondent...@gmail.com>wrote:
>
>
>
> > Hi Dave,
>
> > I think this is what you need:
>
> >
http://jasondentler.com/blog/2009/08/part-7-nhibernate-and-ninject-fo...
>
> > Thanks,
> > Jason
>
> > On Wed, Nov 18, 2009 at 5:56 AM, Miguel Madero <
m...@miguelmadero.com>wrote:
>
> >> You sort of described one of my previous projects.
>
> >> Disclaimer: I don't have a lot of experience with NHibernate, so I'm not
> >> sure I took the right approach. This was a really small/pet project and it
> >> just worked for our scenario, so don't take my advice blindly.
>
> >> 1. We have our MvcApplication class which inherits from
> >> NinjectHttpApplication. I overrode the CreateKernel method and call
> >> kernel.Load("CompanyName.*.dll"). That will load all the Modules from any
> >> referenced assembly that match the pattern and look for all of the types
> >> that implement INinjectModule. This will make all the modules in all of my
> >> assemblies self discoverable.
> >> 2. I have an NHibernateModule
> >> Â Â public class NHibernateModule : NinjectModule
> >> Â Â {
> >> Â Â Â Â public override void Load()
> >> Â Â Â Â {
> >> Â Â Â Â Â Â var configuration = new NHibernateConfiguration();
>
> >> Bind<ISessionFactory>().ToConstant(configuration.GetSessionFactory());
> >> Â Â Â Â Â Â Bind<ISession>().ToMethod(x =>
> >> x.Kernel.Get<ISessionFactory>().OpenSession()).InRequestScope();
> >> Â Â Â Â }
> >> Â Â }
> >> Â Â I'll go into details of the NHConfiguration. The other two lines of
> >> code are just setting bindings. Constant for the Session Factory and a new
> >> Session (aquired from the SessionFactory) on a RequestScope. I tested this
> >> and it worked well on our project, but this blog post<
http://nhforge.org/blogs/nhibernate/archive/2009/08/29/part-7-nhibern...>mentiones that InRequestScope doesn't work on Ninject 1.5 and this would be
> >> On Wed, Nov 18, 2009 at 8:28 AM, Jarrett Meyer <
jarrettme...@gmail.com>wrote:
>
> >>> For clarification, you have SomeAssemblyA.dll and SomeAssemblyB.dll, and
> >>> each has a NinjectModule or StandardModule inside that Assembly?
>
> >>> In my current project, I have multiple assemblies, but none of the
> >>> base-level assemblies have any reference to Ninject. They all use
> >>> plain-old-ctor injection.
>
> >>> In my top-level application, I
> >>> have LinqToSqlDataContextModule and WebContextModule. Each of these modules
> >>> builds up injection bindings for their respective assemblies.
>
> >>> private static IKernel BuildKernel()
> >>> {
> >>> Â Â Â Â Â Â var modules = new INinjectModule[]
> >>> Â Â Â Â Â Â {
> >>> Â Â Â Â Â Â Â Â // Data context for LINQ-to-SQL database operations.
> >>> Â Â Â Â Â Â Â Â new LinqToSqlDataContextModule(() =>
> >>> Â Â Â Â Â Â Â Â Â Â {
> >>> Â Â Â Â Â Â Â Â Â Â Â Â var builder = new LinqToSqlContextBuilder();
> >>> Â Â Â Â Â Â Â Â Â Â Â Â return builder.Build(new EbirsDataContext());
> >>> Â Â Â Â Â Â Â Â Â Â }),
> >>> Â Â Â Â Â Â Â Â // Builds up all of my presentation-layer dependencies
> >>> Â Â Â Â Â Â Â Â new WebContextModule(),
> >>> Â Â Â Â Â Â Â Â // Builds up all of my business-layer dependencies
> >>> (mostly event observers)
> >>> Â Â Â Â Â Â Â Â new BusinessModule()
> >>> Â Â Â Â Â Â };
>
> >>> --
> >>> Jarrett Meyer
> >>> Email:
jarrettme...@gmail.com
> >>> Web: JarrettMeyer.com