3

I have a semantic mapping for example where i map 2 schemas to single model like this:

[SemanticEntity(EntityName = "Article", Prefix = "a", Vocab = CoreVocabulary)]
[SemanticEntity(EntityName = "Event", Prefix = "e", Vocab = "http://example.com/web/Schemas")]
public class Teaser : EntityModel

So my question is how i can in code later identify which schema is mapped to a model? I see only id property that i can use from entity model, but is there anything else.

Something like if Teaser is "Article"

1
  • It's possible you could do it by GetType().Name to get the EntityName, GetType().Name.ToLower().Equals("article")
    – Velmurugan
    Mar 28, 2023 at 19:48

2 Answers 2

5

OOTB you can't. Inside the app you will just have the viewmodel as an object with no reference to the Schema. From the websites perspective it (the schema) is totally irrelevant, it's a CM concept.

You could create a custom model builder to also add that into the viewmodel (and ComponentDataModel on the CM side if not already there, can't remember from the top of my head) to a custom property.

Or if you're using Regions you could in theory derive which Schema it is based on the Region, assuming you don't allow both Schemas in the same Region.

3

As Atila explained, the idea is that a DXA Web Application works with View Models (defined in the Web App itself) rather than with CM concepts likes Components and Schemas. The DXA (Semantic) Model mapping maps/binds View Model properties to CM fields.

If you are mapping multiple CM Schemas to the same View Model type, you won't know what the source CM Schema was after the mapping, unless you have a View Model property which is only mapped to one of the CM Schemas, so it can act as a tell-tale property.

Note, however, that you can also use so-called polymorphic mapping, where you map each CM Schema to its own View Model Type, but multiple View Model types can have a common base type. If you use such a mapping, you can infer the CM Schema from the View Model type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.