Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So, I apologize that we did not get the NHibernate with StructureMap example done earlier. Eventually, I would like to publish it in the StructureMap documentation, but I need some feedback first. You can see the full example on my blog:</p> <p><a href="http://trason.net/journal/2009/10/7/bootstrapping-nhibernate-with-structuremap.html" rel="noreferrer">http://trason.net/journal/2009/10/7/bootstrapping-nhibernate-with-structuremap.html</a></p> <p>That being said, I can hit the highlights here. There is an NHibernateRegistry that makes available four things: an NHibernate.Configuration (as a Singleton), an ISessionFactory (as a Singleton), an ISession (scoped Hybrid (HttpContext if available, falling back to Thread local storage)), and a very simple IUnitOfWork. Also, there is an HttpModule to manage the UnitOfWork per web request.</p> <p>Here is the code for the NHibernateRegistry:</p> <pre><code>using NHibernate; using NHibernate.ByteCode.Castle; using NHibernate.Cfg; using NHibernate.Dialect; using NHibernate.Driver; using NHibernateBootstrap.Core.Domain; using StructureMap.Attributes; using StructureMap.Configuration.DSL; using Environment=NHibernate.Cfg.Environment; namespace NHibernateBootstrap.Core.Persistence { public class NHibernateRegistry : Registry { public NHibernateRegistry() { var cfg = new Configuration() .SetProperty(Environment.ReleaseConnections, "on_close") .SetProperty(Environment.Dialect, typeof(SQLiteDialect).AssemblyQualifiedName) .SetProperty(Environment.ConnectionDriver, typeof(SQLite20Driver).AssemblyQualifiedName) .SetProperty(Environment.ConnectionString, "data source=bootstrap.sqlite;Version=3") .SetProperty(Environment.ProxyFactoryFactoryClass, typeof(ProxyFactoryFactory).AssemblyQualifiedName) .AddAssembly(typeof(Blog).Assembly); var sessionFactory = cfg.BuildSessionFactory(); ForRequestedType&lt;Configuration&gt;().AsSingletons().TheDefault.IsThis(cfg); ForRequestedType&lt;ISessionFactory&gt;().AsSingletons() .TheDefault.IsThis(sessionFactory); ForRequestedType&lt;ISession&gt;().CacheBy(InstanceScope.Hybrid) .TheDefault.Is.ConstructedBy(ctx =&gt; ctx.GetInstance&lt;ISessionFactory&gt;().OpenSession()); ForRequestedType&lt;IUnitOfWork&gt;().CacheBy(InstanceScope.Hybrid) .TheDefaultIsConcreteType&lt;UnitOfWork&gt;(); ForRequestedType&lt;IDatabaseBuilder&gt;().TheDefaultIsConcreteType&lt;DatabaseBuilder&gt;(); } } } </code></pre> <p>Here is the code for the Unit of Work:</p> <pre><code>using System; using NHibernate; namespace NHibernateBootstrap.Core.Persistence { public interface IUnitOfWork : IDisposable { ISession CurrentSession { get; } void Commit(); } } using NHibernate; namespace NHibernateBootstrap.Core.Persistence { public class UnitOfWork : IUnitOfWork { private readonly ISessionFactory _sessionFactory; private readonly ITransaction _transaction; public UnitOfWork(ISessionFactory sessionFactory) { _sessionFactory = sessionFactory; CurrentSession = _sessionFactory.OpenSession(); _transaction = CurrentSession.BeginTransaction(); } public ISession CurrentSession { get; private set;} public void Dispose() { CurrentSession.Close(); CurrentSession = null; } public void Commit() { _transaction.Commit(); } } } </code></pre> <p>Here is the NHibernateModule for web applications:</p> <pre><code>using System; using System.Web; using NHibernateBootstrap.Core.Persistence; using StructureMap; namespace NHibernateBootstrap.Web { public class NHibernateModule : IHttpModule { private IUnitOfWork _unitOfWork; public void Init(HttpApplication context) { context.BeginRequest += ContextBeginRequest; context.EndRequest += ContextEndRequest; } private void ContextBeginRequest(object sender, EventArgs e) { _unitOfWork = ObjectFactory.GetInstance&lt;IUnitOfWork&gt;(); } private void ContextEndRequest(object sender, EventArgs e) { Dispose(); } public void Dispose() { _unitOfWork.Dispose(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload