Re: Speed improvement using compile time generated proxies

"Johan Kirsten" <[email protected]> Thu, 4 Sep 2008 09:25:22 +0200
Newsgroups gmane.comp.windows.dotnet.nhibernate.devel
Message-ID <[email protected]>
Greetings

Unfortunately I am not able to supply source code, as I am under contract. I 
will however be able to tell you what I did and how it works. It will not be 
difficult for you to do it. I think it took me an afternoon. My code is 
based on WC Pierce's code available from 
http://code.google.com/p/nhibernateproxygenerator/. It has al the necessary 
code and lib to get you started.

The content of the source code zip file you will be interested in is:
1) lib - contains all the libraries (just update NHibernate library with the 
latest version)
2) source/NHibernate.Proxy.Generator project - the source code of a console 
application that compiles the proxies. The console application obtains the 
name of an input library that contains *.hbm.xml mapping files. It compiles 
the runtime proxies and saves these runtime proxies into the specified 
output library. It also adds a StaticProxyGenerator class to the output 
library that will be called by NHibernate to instantiate the compile time 
proxies.

The downloaded NHibernate.Proxy.Generator has the following files:
1) ActiveRecordConfigurationSource.cs - class that handles configuration of 
nhibernate
2) ApplicationOptions.cs - defines the commandline arguments.
3) CommandLineArguments.cs - used by the console application to parse 
commandline arguments into ApplicationOptions class
4) GeneratorProxyFactory.cs - performs the compilation of a proxy factory
5) GeneratorProxyFactoryFactory.cs - calls the GeneratorProxyFactory
6) Program.cs - main console application
7) StaticProxyFactory.boo - boo source that is compiled into the 
StaticProxyFactory that will be placed in output library

My changes
1) I dropped the ActiveRecordConfigurationSource as I use my nhibernate 
configuration files as input
2) I altered ApplicationOptions to store
    a) the input the path to an nhibernate configuration file and
    b) the path to my source directory containing the input libraries
3) I left GeneratorProxyFactory and GeneratorProxyFactoryFactory as they are
4) I altered the StaticProxyFactory.boo so that its static _proxies 
dictionary will compile to a generic dictionary that maps System.Int64 to 
System.Type

I altered the Program file to have it work with normal nhibernate 
configuration files, but is similar to the existing progrma file. Also, I do 
not think one should assume ActiveRecord is used. To create a compile time 
proxy library the main program must
1) load command line arguments
2) load the nhibernate configuration file
3) for each session factory defined in the configuration file it must:
   a) Obtain the name to use for the output library file from the property 
"proxyfactory.factory_class" in the nhibernate configuration instance
   b) Replace the property "proxyfactory.factory_class" in the nhibernate 
configuration instance with the type name of GeneratorProxyFactoryFactory
   c) Call BuildSessionFactory on configuration instance. (This will use 
GeneratorProxyFactoryFactory to call GeneratorProxyFactory to compile 
proxies using ProxyGenerator created by Program class and place the Type 
objects into the Proxies library defined in Program class)
   d) Call Program.ProxyGenerator.ProxyBuilder.ModuleScope.SaveAssembly() to 
save compile time proxies to CastleDynProxy2.dll
   e) Use Boo compiler to compile StaticProxyFactory.dll (look at original 
Program class to see how to do this - remember to add all assemblies used by 
NHibernate configuration)
   f) Use ILMerge to merge compile time proxies in CastleDynProxy2.dll with 
StaticProxyFactory class in StaticProxyFactory.dll (remember to name output 
library to the library specified in original loaded nhibernate 
configuration)

Note there is a spelling mistake in original Program class. Replace the 
occurance of StatixProxyFactory with StaticProxyFactory. (The x should be a 
c)

In summary I made three important changes:
1) I dropped the ActiveRecord support as I do not use it and do not think it 
should be assumed
2) Only used nhibernate configuration instance (I created configuration 
instance from my normal config file, but others might use a different 
method)
3) I altered the StaticProxyFactory.boo so that its static _proxies 
dictionary will compile to a generic dictionary that maps System.Int64 to 
System.Type. this is important as it is a bug

I hope this helps. If you have any questions, please feel free to email and 
ask. I hope you can understand that I am not allowed to supply code, but I 
do want to help.

Johan Kirsten

> ----------------------------------------------------------------------
>
> Message: 1
> Date: Wed, 3 Sep 2008 19:33:11 -0300
> From: "Fabio Maulo" <[email protected]>
> Subject: Re: [NHibernate-development] Speed improvement using compile
> time generated proxies
> To: "the NHibernate development list"
> <[email protected]>
> Message-ID:
> <[email protected]>
> Content-Type: text/plain; charset="iso-8859-1"
>
> Do you can send me the solution with all your sources and required 
> external
> lib ?Thanks.
>



-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/