问题
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