Info
Version: | 10.3.0.23496 |
Author(s): | Richard J. Cabral |
Last Update: | Friday, June 16, 2023 |
.NET Fiddle: | Create the first Fiddle |
Project Url: | https://constellation4sitecore.com/foundation/modelmapping/ |
NuGet Url: | https://www.nuget.org/packages/Constellation.Foundation.ModelMapping |
Install
Install-Package Constellation.Foundation.ModelMapping
dotnet add package Constellation.Foundation.ModelMapping
paket add Constellation.Foundation.ModelMapping
Constellation.Foundation.ModelMapping Download (Unzip the "nupkg" after downloading)
Dependencies
- Constellation.Foundation.Data(>= 10.0.0.22111)
Tags
Requires .NET 4.8
Constellation is a collection of utilities for .NET CMS implementers.
The Foundation.ModelMapping library is inspired by AutoMapper and is designed to provide a quick, non-ORM method of mapping Sitecore Items to ViewModels.
String properties on your ViewModel will be mapped to FieldRendered values from fields with the same names. Integer and DateTime property types can also be handled automatically, although you will not get page editor support for these types. There are a number of Attributes that you can assign to your ViewModel's properties to change the behavior of the Mapper.
Usage:
var modelMapper = (IModelMapper)ServiceLocator.ServiceProvider.GetService(typeof(IModelMapper)); // Dependency Injection of an IModelMapper is supported
TModel viewModel = modelMapper.MapItemToNew<TModel>(sourceItem);
Or: TModel viewmodel = item.MapToNew<TModel>();
Or: modelMapper.MapTo(item, model);
When using Item or ItemList extensions for mapping, the context IModelMapper is requested from the ServiceLocator, so any moc objects you use for testing are supported with these extension methods.
Constraints:
- Your ViewModel must have a public, parameterless constructor.
- Only Writable, Public Instance properties will be mapped.
- Within Sitecore, your field names can be human-legible. ModelMapper will attempt to convert the field name to a valid Property name.
Simpler names work best.
You can use the Field's Display Name for better human legibility and keep the Field's actual name closer to a legit C# entity name.
- ViewModel properties can be:
- - Strings
- - HtmlStrings (recommended for RichText and FieldRenderer)
- - Integers
- - Decimals
- - DateTimes
- - Other ViewModels (recommended for DropLink, DropTree)
- - ICollection Generics of other ViewModels (recommended for Multilist, Treelist)
- - ModelMapping.Model.ImageModel (for Image Fields, gives access to all field properties and permits generating different kinds of image URLs on the View.)
- - ModelMapping.Model.GeneralLinkModel (for General Link fields, gives access to all field properties through a single object.)
- - Random Plain Old Classes as long as they are Where T: class, new()
The following Model properties are mapped to Item properties rather than Item fields:
- ID - Maps to the Item.ID - Make sure you use Sitecore.Data.ID as the property type.
- Name - Maps to the Item.Name
- DisplayName - Maps to Item.DisplayName
- Url - Should be a string, not a Uri. Causes the current context LinkManager to generate a Url for the Item.
- Parent - Maps to the ViewModel you provide based on the Item.Parent.
Support for XML Field Attributes
- ImageField.Alt - use Model.FieldNameAlt
- ImageField.Height - use Model.FieldNameHeight
- GeneralLink.Target - use Model.FieldNameTarget
- etc.
Support for LinkField.TargetItem - use Model.FieldNameTargetItem and supply a class with public properties and a parameterless constructor.
Support for MultilistField.GetItems - use Model.FieldName and supply a generic ICollection<T> where "T" is new() and has properties you can map.
Extensible! See the Constellation.Foundation.ModelMapping.config file to change the way Fields are processed.