Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

[implementation] Simplify how ModelMetadata gets defaults and is extended; eliminate CachedModelMetadata<TPrototypeCache> #1034

@dougbu

Description

@dougbu

CachedModelMetadata<TPrototypeCache> is unnecessary and leads to problems setting default values for overridden ModelMetadata properties. We have strange things like subclasses calling base.Property from a CalculateProperty() method. This first came up when @yishaigalatzer got a good look at the "pattern" 😺

Current pattern for ModelMetadata properties is:

  • Property is virtual in ModelMetadata and its constructor sets all default values
  • Property is sealed in CachedModelMetadata<TPrototypeCache>
  • CachedModelMetadata<TPrototypeCache> makes a protected virtual {type} Compute{name}() method available to subclasses
  • Where useful, CachedDataAnnotationsModelMetadata overrides Compute{name}() to provide a value based on applied attributes

Should instead remove CachedModelMetadata<TPrototypeCache> and instead follow the following pattern:

  • Property is not virtual in ModelMetadata
  • ModelMetadata makes a protected virtual {type} Compute{name}() method available to subclasses
  • Base implementations of these methods return appropriate default values
  • Where useful, CachedDataAnnotationsModelMetadata overrides Compute{name}() to provide a value based on applied attributes

Details...

Overall this would push property lazy-initialization down into the base ModelMetadata class since the Compute{name}() are called lazily and just once. The current CachedModelMetadata<TPrototypeCache>.PrototypeCache property would move up into CachedDataAnnotationsModelMetadata, the only place that property is read.

Comments carried over from MVC 5.2 indicate CachedModelMetadata<TPrototypeCache> exists to allow "flexible caching strategies". But the comment is likely more about supporting alternate metadata sources, a separation maintained in the new pattern proposed above.

If we need an abstract layer in here somewhere, could create IModelMetadata. But that would impact lots more code than this issue. This issue shouldn't impact much beyond the 3 mentioned classes.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions