Why should all Glass.Mapper fields be virtual?

你离开我真会死。 提交于 2019-12-10 16:20:31

问题


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:


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.



来源:https://stackoverflow.com/questions/33105212/why-should-all-glass-mapper-fields-be-virtual

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