为什么所有Glass.Mapper字段都应该是虚拟的?

问题描述:

Glass.Mapper文档指出:

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; }
    }
}

您可以看到该类不包含任何特殊标记 使用Glass.Mapper编写类时要记住的一件事 是为了确保您所有的字段都标记为虚拟.玻璃制图仪 可以通过使用 随需应变映射和自动映射的组合.

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.

有人知道将这些字段标记为virtual的重要性吗?没有这个,它似乎工作得很好.我看不到继承这些字段并overrides它们吗?

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?

如果您使用的是延迟加载,Glass映射器需要能够动态地动态生成用于包装您的类的代理类.为了能够继承和覆盖您的属性,需要将其标记为virtual.

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.