为什么NHibernate的3.3不允许在ICollection的私人二传手?不允许、私人、二传、NHibernate

2023-09-03 17:44:13 作者:まま(离开)

我升级形式NHibernate的3.2〜3.3 NHibernate的,我有很多虚拟的成员在我的领域类定义是这样的:

  public虚拟的ICollection<电影>电影{获得;私定; }
 

该停止了升级后的工作,我得到这个异​​常:

 以下类型可能不被用作代理服务器:
产品类别:方法set_Movies应该是公共/受保护的虚拟或受保护的内部虚拟
 

现在我设法通过改变setter方法​​来保护,以解决这个问题,但我很惊讶,也是不断变化的集合属性为只读与支持字段的伎俩。

所以,我有两个问题:

为什么NHibernate的决定禁止使用私人setter方法​​呢?它看起来并不像从.NET技术上的限制,因为它是之前的支持,我得到它不应该从设计的角度做的,我们应该用集合中删除(),添加()等方法来操作,但那么为什么允许protected和public setter方法​​?

为什么(以及如何)使用的是只读属性与支持字段的工作呢?

解决方案

NHibernate的集成代理生成到它的装配和(纠正我,如果我错了)的实施中所使用不能处理私人setter方法​​。

然而,如果没有设置器它试图搜索一个背场(因为否则就不能得到)和只读是编译时特征并且使用反射时被忽略。

为什么次次下载的qq堂3.3只可以玩一次,下一次想登陆它就会提示说不可以玩了

I upgraded form nhibernate 3.2 to nhibernate 3.3, and I had a lot of virtual members defined in my domain classes like this:

public virtual ICollection<Movie> Movies { get; private set; }

This stopped working after the upgrade and I get this exception:

Following types may not be used as proxies:
ClassName: method set_Movies should be 'public/protected virtual' or 'protected internal virtual'

Now I managed to solve the problem by changing the setter to protected but I was surprised also that changing the Collection property to be readonly with a backing field does the trick.

So I have two questions:

Why does NHibernate decided to disallow the use of private setters? It doesn't look like a technical limitation from .net as it was supported before, I get it shouldn't be done from design point of view as we should use the Collection Remove(), Add() etc.. methods to manipulate it but then why allow protected and public setters?

Why (and how) does using a readonly property with a backing field work?

解决方案

NHibernate integrated the Proxy generator into it's assembly and (correct me if I'm wrong) the implementation used can't handle private setters.

However if there is no setter it tries to search for a backing field (since otherwise it can't set it) and readonly is compiletime feature and ignored when using reflection.

 
精彩推荐