Especially for integration scenarios there is often the need to provide a simple configuration for mappings. This may include property mappings but also routing configurations (“if this value, then that service”). This snippet provides a simple base class to define such mappings with easy editable JSON configurations, typically stored in a Blob Storage. It supports complex requirements such as resolution based on multiple criterias, wildcard support and fallback / default mechanism.
The snippet demonstrates a usage in an Azure Function, but it can be easily used in any C# code without any dependencies other than Newtonsoft JSON.
Create mapping configurations like this (demo-content-type-mapping
):
{
"Image:psd": "PSD",
"Image:*": "PHOTO",
"Movie:*": "VIDEO",
"*:pdf": "PDF",
"MS PowerPoint:*": "PRESENTATION",
"MS Excel:*": "EXCEL",
"MS Word:doc": "DOC",
"MS Word:docx": "DOCX",
"*": "OTHER"
}
Features:
"*:pdf": 42
. All values have to be of same typeYou can then parse and use this file in any place like:
Mapping<string> contentTypeMapping = Mapping<string>.CreateFromFileContent(mappingFileContent);
string contentType = contentTypeMapping.GetMatchOrDefault("Movie", "mp4"); // Returns VIDEO
The main class is Mapping