Why should all Glass.Mapper fields be virtual?

2019-06-24 09:56发布

The Glass.Mapper documentation states:

using Glass.Mapper.Sc.Configuration.Attributes;

namespace Glass.Mapper.Sites.Sc.Models.Landing
{
    public class HomePage
    {
        public virtual string Title { get; set; }
        public virtual string MainBody { get; set; }
    }
}

You can see that this class doesn't include any special markup however one important thing to remember when writing class with Glass.Mapper is to ensure that all your fields are marked as virtual. Glass.Mapper can automatically infer what data to load for your model by using a combination of On Demand mapping and Auto-Mapping.

It eludes to use "On-demand" and "Auto-Mapping" but doesn't explain how or why.

Does anyone know why it's important to flag these fields as virtual? It seems to work perfectly fine without this. Nothing I can see inherits the fields and overrides them?

1条回答
一纸荒年 Trace。
2楼-- · 2019-06-24 10:21

If you are using lazy loading, Glass mapper needs to be able to dynamically generate a proxy class on the fly that wraps your class. To be able to inherit and override your properties, they need to be marked virtual.

查看更多
登录 后发表回答