virtual properties and lazy loading

北慕城南 提交于 2019-12-05 04:39:17

The fact that they are declared virtual allows NHibernate to override the property and create a proxy implementation for it - the proxy in turn they can use to implement lazy loading on the first access of the property.

There is no hidden behavior behind virtual members. Except the not so hidden fact that they can be overridden in child classes.

Lazy loading can be achieved by using the Lazy<T> class. In which T is the type that will be loaded. It'll implicitly convert to T.

Or if you want to manually set properties to behave lazy you could use something like this:

private SomeType _someProperty = null;
public override SomeType SomeProperty
{
    get
    {
        if (_someProperty == null)
        {
            // Load _someProperty
        }
        return _someProperty;
    }
}

With ValueTypes you can choose to make them Nullable<T>. Or introduce a bool whether they're loaded or not.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!