You have to make them virtual because otherwise an uninitialized proxy won't know to initialize when the property is touched.
Even worse, we have some non-persisted properties that get assigned through our implementation of IInterceptor.Instantiate(). E.g. we assign a business rule object here that listens to property changes and reacts accordingly, because we don't want our entity classes to know about business rule classes directly (our entities only know the rule interface, which only exposes a method for the entity's ToString() method to call). The problem is that IInterceptor.Instantiate() does not get called when a a proxy is first created, but only when the proxy is initialized. So, when we initially tried to make these non-persisted properties non-virtual (because we didn't want reading the property to trigger initializing the proxy), the property would incorrectly return null because it was never initialized by the interceptor. We were forced to make these properties virtual to get the correct behavior.
|