Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

MappingExtensions.cs

using Nop.Core;
using Nop.Core.Configuration;
using Nop.Core.Infrastructure.Mapper;
using Nop.Services.Plugins;
using Nop.Web.Framework.Models;

namespace Nop.Web.Areas.Admin.Infrastructure.Mapper.Extensions;

/// 
/// Represents the extensions to map entity to model and vise versa
/// 
public static class MappingExtensions
{
    #region Utilities

    /// 
    /// Execute a mapping from the source object to a new destination object. The source type is inferred from the source object
    /// 
    /// Destination object type
    /// Source object to map from
    /// Mapped destination object
    private static TDestination Map(this object source)
    {
        //use AutoMapper for mapping objects
        return AutoMapperConfiguration.Mapper.Map(source);
    }

    /// 
    /// Execute a mapping from the source object to the existing destination object
    /// 
    /// Source object type
    /// Destination object type
    /// Source object to map from
    /// Destination object to map into
    /// Mapped destination object, same instance as the passed destination object
    private static TDestination MapTo(this TSource source, TDestination destination)
    {
        //use AutoMapper for mapping objects
        return AutoMapperConfiguration.Mapper.Map(source, destination);
    }

    #endregion

    #region Methods

    #region Model-Entity mapping

    /// 
    /// Execute a mapping from the entity to a new model
    /// 
    /// Model type
    /// Entity to map from
    /// Mapped model
    public static TModel ToModel(this BaseEntity entity) where TModel : BaseNopEntityModel
    {
        ArgumentNullException.ThrowIfNull(entity);

        return entity.Map();
    }

    /// 
    /// Execute a mapping from the entity to the existing model
    /// 
    /// Entity type
    /// Model type
    /// Entity to map from
    /// Model to map into
    /// Mapped model
    public static TModel ToModel(this TEntity entity, TModel model)
        where TEntity : BaseEntity where TModel : BaseNopEntityModel
    {
        ArgumentNullException.ThrowIfNull(entity);

        ArgumentNullException.ThrowIfNull(model);

        return entity.MapTo(model);
    }

    /// 
    /// Execute a mapping from the model to a new entity
    /// 
    /// Entity type
    /// Model to map from
    /// Mapped entity
    public static TEntity ToEntity(this BaseNopEntityModel model) where TEntity : BaseEntity
    {
        ArgumentNullException.ThrowIfNull(model);

        return model.Map();
    }

    /// 
    /// Execute a mapping from the model to the existing entity
    /// 
    /// Entity type
    /// Model type
    /// Model to map from
    /// Entity to map into
    /// Mapped entity
    public static TEntity ToEntity(this TModel model, TEntity entity)
        where TEntity : BaseEntity where TModel : BaseNopEntityModel
    {
        ArgumentNullException.ThrowIfNull(model);

        ArgumentNullException.ThrowIfNull(entity);

        return model.MapTo(entity);
    }

    #endregion

    #region Model-Settings mapping

    /// 
    /// Execute a mapping from the settings to a new settings model
    /// 
    /// Model type
    /// Settings to map from
    /// Mapped model
    public static TModel ToSettingsModel(this ISettings settings) where TModel : BaseNopModel, ISettingsModel
    {
        ArgumentNullException.ThrowIfNull(settings);

        return settings.Map();
    }

    /// 
    /// Execute a mapping from the model to the existing settings
    /// 
    /// Settings type
    /// Model type
    /// Model to map from
    /// Settings to map into
    /// Mapped settings
    public static TSettings ToSettings(this TModel model, TSettings settings)
        where TSettings : class, ISettings where TModel : BaseNopModel, ISettingsModel
    {
        ArgumentNullException.ThrowIfNull(model);

        ArgumentNullException.ThrowIfNull(settings);

        return model.MapTo(settings);
    }

    #endregion

    #region Model-Config mapping

    /// 
    /// Execute a mapping from the configuration to a new config model
    /// 
    /// Model type
    /// Config to map from
    /// Mapped model
    public static TModel ToConfigModel(this IConfig config) where TModel : BaseNopModel, IConfigModel
    {
        ArgumentNullException.ThrowIfNull(config);

        return config.Map();
    }

    /// 
    /// Execute a mapping from the model to the configuration
    /// 
    /// Config type
    /// Model type
    /// Model to map from
    /// Config to map into
    /// Mapped config
    public static TConfig ToConfig(this TModel model, TConfig config)
        where TConfig : class, IConfig where TModel : BaseNopModel, IConfigModel
    {
        ArgumentNullException.ThrowIfNull(model);

        ArgumentNullException.ThrowIfNull(config);

        return model.MapTo(config);
    }

    #endregion

    #region Model-Plugin mapping

    /// 
    /// Execute a mapping from the plugin to a new plugin model
    /// 
    /// Model type
    /// Plugin to map from
    /// Mapped model
    public static TModel ToPluginModel(this IPlugin plugin) where TModel : BaseNopModel, IPluginModel
    {
        ArgumentNullException.ThrowIfNull(plugin);

        return plugin.Map();
    }

    /// 
    /// Execute a mapping from the plugin descriptor to the plugin model
    /// 
    /// Model type
    /// Plugin descriptor to map from
    /// Model to map into; pass null to map to the new model
    /// Mapped model
    public static TModel ToPluginModel(this PluginDescriptor pluginDescriptor, TModel model = null)
        where TModel : BaseNopModel, IPluginModel
    {
        ArgumentNullException.ThrowIfNull(pluginDescriptor);

        return model == null ? pluginDescriptor.Map() : pluginDescriptor.MapTo(model);
    }

    #endregion

    #endregion
}