虚拟代理如何工作?
我在理解虚拟代理方面遇到了一些麻烦。我阅读了大量的文章,并花了几个小时试图找到好的信息,但我还没有找到全面的东西。因此,我将在这里提出一个通用请求,以获取更好的信息(发布在这里或只是一个链接)。我还将在下面添加一些细节,以更好地解释我到底想要什么。
我有几个对象,它们之间有很多引用。为了简洁起见,我将拥有一个具有基本父子关系的对象(节点)。当我从数据库中取出这个对象时,我想实现延迟加载。据我所知,虚拟代理本质上将通过引用接口(INode)并根据需要拉取数据成员来为我处理所有延迟加载。 [注意:我实际上没有 INode 类,但是当我将 virtual 关键字放在我的数据成员上时,似乎确实使用了代理]
当我将类中的数据成员设为虚拟时,它似乎创建了一个代理。这是虚拟代理吗?这些实现了延迟加载吗?
我搜索了有关 virtual 关键字的信息,但我能找到的唯一文档是在方法上使用它,该方法用于继承,以便派生类可以重写该函数,这与我想要的无关(我认为) 。
这是我当前的 Node.cs
[DataContract(IsReference=true)]
public partial class Node
{
[DataMember]
public long ID { get; private set; }
[DataMember]
public virtual Node Parent { get; set; }
[DataMember]
public virtual ICollection<Node> Children { get; set; }
}
基本上在这一点上我非常困惑,只需要一些关于这个主题的指导,甚至是我可以查找的在线资源,因为我发现的所有资源都没有什么帮助。
提前致谢。
I am having some trouble wrapping my head around virtual proxies. I have read tons of articles and spent several hours trying to find good information, but I have yet to find something comprehensive. So I will make a generic request here for better information (either posted here or just a link). I will also add some detail below to better explain what exactly it is I want.
I have several objects and there are many references between them. For the sake of brevity I will have one object (Node) with a basic parent-child relationship. When I pull this object out of the database, I would like to implement lazy-loading. From what I have read, a virtual proxy will essentially handle all the lazy-loading for me by referencing the interface (INode) and pulling data members as needed. [Note: I do not actually have an INode class, but when I put the virtual keyword on my data members, a proxy did seem to be used]
When I make data members in my classes virtual, it seems to make a proxy. Is this a virtual proxy? Do these implement lazy-loading?
I searched for information about the virtual keyword, but the only documentation I could find was to use it on methods, which is used for inheritance so that derived classes can override the function, which has nothing to do with what I want (I think).
This is my current Node.cs
[DataContract(IsReference=true)]
public partial class Node
{
[DataMember]
public long ID { get; private set; }
[DataMember]
public virtual Node Parent { get; set; }
[DataMember]
public virtual ICollection<Node> Children { get; set; }
}
Basically at this point I am very confused and just need some guidance on this topic, or even an online resource that I can look to, since all the ones I have found have been less than helpful.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“虚拟”代理和延迟加载是与 ORM 工具相关的东西。代理实际上不是虚拟的,而是动态的,并且遵循 GoF 定义的真实代理模式。
动态代理是 ORM 工具在运行时创建的类(它没有在任何地方定义为代码文件)。它源自您的实体并覆盖导航属性。因此,它们必须是虚拟的才能使代理工作。代理在私有字段或任何更复杂的结构中保存导航属性的状态,如果第一次访问该属性,它会看到该状态已卸载并触发从数据库加载并将状态更改为已加载。
无论如何,我不确定这如何指代 WCF,因为最佳实践不是使用 WCF 延迟加载。为什么?
"Virtual" proxy and lazy loading is something related to ORM tools. The proxy actually is not virtual it is dynamic and it follows real Proxy pattern defined by GoF.
Dynamic proxy is class created by ORM tool at runtime (it is not defined as a code file anywhere). It derives from your entity and it overrides navigation properties. Because of that they must be virtual to make a proxy work. The proxy holds a state of navigation property in private field or any more complex structure and if the property is accessed first time it sees that the state is unloaded and triggers loading from database and change the state to loaded.
Anyway I'm not sure how this refers to WCF because best practice is not using lazy loading with WCF. Why?
我认为您需要一些私人领域来支持您的虚拟财产。在这些虚拟属性的获取覆盖中,您检查私有字段以查看其当前是否有效(是否已从数据库获取,是否是最新的等) - 如果不是,则获取或重新获取它。我不认为事情会比这更复杂。
基类:
覆盖:
I think you want some private fields backing your virtual properites. In the get overrides of these virtual properties you check the private field to see if its currently valid (has it been got already from db, is it upto date etc) - if not then fetch or refetch it. I don't see it has to been anymore complicated than that.
Base class:
Override: