Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Castle proxy factory in NHibernate?

Tags:

orm

nhibernate

What is Castle proxy factory in NHibernate? What is its task? What does proxy mean in this case?

like image 902
masoud ramezani Avatar asked Apr 20 '10 07:04

masoud ramezani


3 Answers

Castle can be used (amongst others, you have the choice, you can also use LinFu, Spring.NET, ...) to create dynamic proxies of your entities.

By default, NHibernate uses dynamic proxies to represent your entities; by doing so, it is able to return an object to you when you retrieve some entity from the DB, without all properties being populated. By using a dynamic proxy, it will only populate the entity once you really refer to a property.
(So it is some kind of lazy loading; not to be confused with lazy loading of collections / associations though).

This behaviour is the reason why NHibernate wants you to create every property as virtual by default: NHibernate will create a new class using this Castle (or LinFu, ...) proxy provider which inherits from your entity, and it will override all the properties so that it can 'inject' the code that is necessary to retrieve the necessary data from the DB.

You can disable this behaviour by specifying 'lazy=false' in your entity mapping. (Although, I do think that even if you disable this feature, NHibernate will still require that you use one of the proxy factories).

like image 153
Frederik Gheysels Avatar answered Oct 27 '22 17:10

Frederik Gheysels


When you are selecting an entity from ISession you are getting not real entity instance - you are getting proxy object. This proxy object inherits your entity and used by NHibernate to track changes made to the fields.

like image 39
Sly Avatar answered Oct 27 '22 18:10

Sly


see it: http://en.wikipedia.org/wiki/Proxy_pattern

like image 38
Afshar Mohebi Avatar answered Oct 27 '22 17:10

Afshar Mohebi