NHibernate static proxy

It's been a long time (06/24/2011) I pushed my last commit on my GitHub project NHibernate.StaticProxy, and I notice only now that I even didn't speak about this project on my blog... Shame on me!

So after a short delay (hum...), here it comes 🙂

As the name of the project suggests it, its goal is to provide an alternative proxy to NHibernate. But what it doesn't tell, is that this proxy is built using PostSharp.


Let's copy / paste the awesome readme file, to get the benefits of this static proxy, over the more traditional dynamic proxies, like those built on top of Castle or Linfu:

  1. No need to make all your entities have virtual properties / methods
  2. Only the mapped properties / fields are altered by PostSharp
  3. You know at compile time if:
    1. No mappings were found for an entity type (A warning is emitted)
    2. You didn't configure correctly the proxy
  4. Your mappings can be used directly without any modification


Again, from the readme, here are the steps to use this proxy inside your project:

  1. Reference NHibernate.StaticProxy from the assembly which contains the entities declarations (Let's call this assembly Entity.dll, which is a very original name for such an assembly)
  2. Create a new class in Assembly.dll, inheriting from NHibernate.StaticProxy.StaticProxyConfigurationAttribute. This class must return a list of HbmMapping. There are several examples on how to derive this class in the NHibernate.StaticProxy.Examples project (using mapping by code, resources files, or Fluent NHibernate)
  3. In the AssemblyInfo.cs file of Assembly.dll, add a reference to the class you have just created, as an assembly attribute. For example :
    [assembly: ModelMapperStaticProxyConfigurationAttribute]
  4. To make Postsharp wave your entities classes, there are several possibilities:
    1. Add [StaticProxyAttribute] on top of each of your entity classes
    2. If all of your entities derive from a common class, add [StaticProxyAttribute] on top of that class
    3. Reference [StaticProxyAttribute] as an assembly attribute in the AssemblyInfo.cs file of the project owning the entity classes:
      [assembly: StaticProxy(AttributeTargetTypes = "NHStaticProxy.Tests.Entities.*", AttributeTargetTypeAttributes = MulticastAttributes.Public, AttributeTargetMemberAttributes = MulticastAttributes.Public)]
      More infos here
  5. Configure NHibernate, so it uses the custom StaticProxyFactoryFactory:
    var configuration = new Configuration(); configuration.Proxy(proxy => proxy.ProxyFactoryFactory<NHibernate.StaticProxy.StaticProxyFactoryFactory>());

Pretty straightforward, isn't it? ^^

I'm using this proxy in a project I'm working on for almost a year, and the only caveat I ran into is the lack of support for the mapping of interfaces, as PostSharp is not able to create the derived classes at runtime when NHibernate calls the proxy to load entities from the database.

Otherwise, it keeps running like a charm 🙂

Currently, this project makes use of pretty old releases of PostSharp and NHibernate. If some of you would find that useful, I could create a Nuget Package so it is more easily usable. Just let me now 🙂

Happy Coding!

2 thoughts on “NHibernate static proxy

  1. I know that a lot of time was passed between my comment and this post. But I have a question: this proxy implementation really works? I ask becoz I have tested it and none of the tests in "LoadingInstancesFromDatabase" (from NHibernate.StaticProxy.Tests) that load a entity from DB work. All of these I get a exception "Unable to cast object of type 'NHibernate.StaticProxy.Tests.Entities.Customer' to type 'NHibernate.StaticProxy.INHibernateStaticProxy'."

    All entities that I see (customer, orders) does not have the INHibernateStaticProxy interface. And I see too that none of the methods in "StaticProxyAttribute.cs" are called. This maybe is becoz I are using PostSharp Express (the free version)?

    • Hello,

      indeed it's been ages since the post, and unfortunately I did not use PostSharp or NHibernate for ages.
      But I confirm that at the time this post was written, evertything was working well.
      I don't remember which version of NHibernate I was compiling my projects with at that time, but I'm sure that PostSharp had a major version update since.
      It might indeed be a limitation of the express version.

      I guess the best advice I could give you is to contact Postsharp devs and ask them.

      Good luck!

Comments are closed.