Reputation: 5173
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?
Upvotes: 4
Views: 803
Reputation: 30813
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.
Upvotes: 7