NHibernate Forge
The official new home for the NHibernate for .NET community

Pre-Generate Lazy Loading Proxies


Wiki Page Hierarchy


Page Details

First published by:
Bill Pierce
on 09-22-2008
Last revision by:
John Davidson
on 09-07-2011
3 people found this article useful.
Comments (6)
History (13)
100% of people found this useful

Pre-Generate Lazy Loading Proxies

Filed under: [Edit Tags]


  1. Download the latest binaries for NHibernate ProxyGenerators
  2. Extract the binaries to your "lib" directory relative to your source files
  3. Open a command prompt and navigate the to "bin" directory in your web project.
  4. Enter the following command:
    ..\..\..\lib\bin\net-2.0\NHPG.exe /o:Example.Domain.Proxies.dll Example.Domain.dll

    NHPG.exe:  The console application that generates the proxies.  Included in the binary distribution from step 1
    /o: The path to the assembly that will be generated by NHPG
    Example.Domain.dll: The assembly that contains your NHibernate Mappings (hbm.xml files)
  5. Add 'proxyfactory.factory_class' to your NHibernate configuration file:
    <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="connection.connection_string">Server=(local);Database=Development;Trusted_Connection=True;</property>
    <mapping assembly="Example.Domain" />

    <property name="proxyfactory.factory_class">CastleStaticProxyFactoryFactory, Example.Domain.Proxies</property>
    Replace Example.Domain.Proxies with the name of the assembly used for argument '/o' in Step 4.
  6. You're done!  Pre-generated lazy loading proxies can be used with NHibernate to Run in Medium Trust 

Sample Commands

  • Generate proxies for ActiveRecord
    NHPG.exe /g:activerecord /o:Example.ActiceRecordDomain.Proxies.dll Example.ActiveRecordDomain.dll
  • Generate proxies with multiple class mapping assemblies
    NHPG.exe /o:Example.Domain.Proxies.dll Example.Domain1.dll Example.Domain2.dll
  • Generate proxies with a custom Dialect
    NHPG.exe /d:NHibernate.Dialect.Oracle9Dialect /o:Example.Domain.Proxies.dll Example.Domain.dll

Recent Comments

By: Phil Japikse Posted on 10-13-2009 0:59

Is there any consideration to update this to work with NH 2.1?


By: Tomasz Modelski Posted on 04-28-2009 9:38

I wrote blog post about proxies generation in Visual Studio post build event, as much transparent to whole solution as possible.


By: mgrillob Posted on 03-23-2009 14:35

I wanted to thank you for your great job on this tool. I was about to change my code to use a different ORM mapper, but then I got to this tool and tested it and I was able to deploy my website to a shared hosting with medium trust. It's working fine at the moments and I feel safe using NHibernate which is a great ORM that save us a lot of work.

Do you have plans to move to RC or GA?


By: Hielke Hoeve Posted on 02-10-2009 15:36

That would be indeed very much desired, although our application generates hbm.config files they are generated @ compile time and can be different quite easily having 100 hbm.config files in our dll is not really desired.

By: Bill Pierce Posted on 10-09-2008 11:51

<p>JIRA added - <a rel="nofollow" target="_new" href="jira.nhibernate.org/.../p>

View All
Powered by Community Server (Commercial Edition), by Telligent Systems