简体   繁体   English

什么是NHibernate的Castle代理工厂?

[英]What is Castle proxy factory in NHibernate?

What is Castle proxy factory in NHibernate? 什么是NHibernate的Castle代理工厂? What is its task? 它的任务是什么? What does proxy mean in this case? 代理在这种情况下意味着什么?

Castle can be used (amongst others, you have the choice, you can also use LinFu, Spring.NET, ...) to create dynamic proxies of your entities. 可以使用Castle(其中包括您可以选择,也可以使用LinFu,Spring.NET,...)来创建实体的动态代理。

By default, NHibernate uses dynamic proxies to represent your entities; 默认情况下,NHibernate使用动态代理来表示您的实体; 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. 这个行为是NHibernate希望你默认创建每个属性为虚拟的原因:NHibernate将使用继承自你的实体的Castle(或LinFu,...)代理提供程序创建一个新类,它将覆盖所有属性这样它就可以“注入”从数据库中检索必要数据所需的代码。

You can disable this behaviour by specifying 'lazy=false' in your entity mapping. 您可以通过在实体映射中指定“lazy = false”来禁用此行为。 (Although, I do think that even if you disable this feature, NHibernate will still require that you use one of the proxy factories). (虽然,我确实认为即使您禁用此功能,NHibernate仍然会要求您使用其中一个代理工厂)。

When you are selecting an entity from ISession you are getting not real entity instance - you are getting proxy object. 当你从ISession中选择一个实体时,你得到的不是真正的实体实例 - 你正在获得代理对象。 This proxy object inherits your entity and used by NHibernate to track changes made to the fields. 此代理对象继承您的实体,并由NHibernate用于跟踪对字段所做的更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NHibernate需要Proxy Factory吗? - Is Proxy Factory necessary in NHibernate? NHibernate 2.1代理工厂选项 - 有什么区别和选择? - NHibernate 2.1 Proxy Factory options - what are the differences and which to choose? NHibernate 3和城堡代理:myList.Contains已损坏 - NHibernate 3 and castle proxy: myList.Contains is broken NHibernate无法加载代理工厂 - NHibernate unable to load Proxy Factory 我在哪个程序集中找到命名空间“ NHibernate.Proxy.Poco.Castle”? - In which assembly I find namespace 'NHibernate.Proxy.Poco.Castle'? 没有坚持:Castle.Proxies。 <EntityName> NHibernate中的Proxy和lazy =“true”? - No persister for: Castle.Proxies.<EntityName>Proxy and lazy=“true” in NHibernate? 通过MSMQ通过MassTransit从NHibernate发送城堡代理会导致StackOverflowException - Sending Castle Proxy from NHibernate with MassTransit over MSMQ causes StackOverflowException 在Castle / NHibernate中使用不受支持的SQL函数或子句的首选方式是什么? - What is the preferred way of using unsupported SQL functions or clauses with Castle/NHibernate? Castle,LinFu和Spring与NHibernate之间的关系是什么? - What's the relationship between Castle, LinFu and Spring with NHibernate? 如果建立Nhibernate会话工厂失败怎么办? - What if building Nhibernate session factory fails?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM