oreillymedia / ASP_NET-MVC-5-with-Bootstrap-and-Knockout_js

46 stars 42 forks source link

AutoMapper.Mapper.CreateMap is obsolete #3

Open kergee opened 7 years ago

kergee commented 7 years ago

AutoMapper.Mapper.CreateMap(_sourceType, _destinationType); var viewModel = AutoMapper.Mapper.Map(model, srcGenericType, destGenericType);

can't be passed, how correct it?

using BootstrapIntroduction.Models; using BootstrapIntroduction.ViewModels; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Web; using System.Web.Mvc;

namespace BootstrapIntroduction.Filters { [AttributeUsage(AttributeTargets.Method)] public class GenerateResultListFilterAttribute : FilterAttribute, IResultFilter { private readonly Type _sourceType; private readonly Type _destinationType;

    public GenerateResultListFilterAttribute(Type sourceType, Type destinationType)
    {
        _sourceType = sourceType;
        _destinationType = destinationType;
    }

    public void OnResultExecuting(ResultExecutingContext filterContext)
    {
        var model = filterContext.Controller.ViewData.Model;

        var resultListGenericType = typeof(ResultList<>).MakeGenericType(new Type[] { _destinationType });
        var srcGenericType = typeof(List<>).MakeGenericType(new Type[] { _sourceType });
        var destGenericType = typeof(List<>).MakeGenericType(new Type[] { _destinationType });

        AutoMapper.Mapper.CreateMap(_sourceType, _destinationType);
        var viewModel = AutoMapper.Mapper.Map(model, srcGenericType, destGenericType);

        var queryOptions = filterContext.Controller.ViewData.ContainsKey("QueryOptions") ? 
            filterContext.Controller.ViewData["QueryOptions"] : 
            new QueryOptions();

        var resultList = Activator.CreateInstance(resultListGenericType, viewModel, queryOptions);

        filterContext.Controller.ViewData.Model = resultList;
    }

    public void OnResultExecuted(ResultExecutedContext filterContext)
    {
    }
}

}

saint-yellow commented 7 years ago

I modified like this:

''' using BookstoreWebApp.ViewModels; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; using System; using System.Collections.Generic;

namespace BookstoreWebApp.Filters { [AttributeUsage(AttributeTargets.Method)] public class GenerateResultListFilterAttribute : ResultFilterAttribute, IResultFilter { private readonly Type _sourceType;

    private readonly Type _desinationType;

    public GenerateResultListFilterAttribute(Type sourceType, Type destionationType)
    {
        _sourceType = sourceType;

        _desinationType = destionationType;
    }

    public override void OnResultExecuting(ResultExecutingContext context)
    {
        Controller controller = (Controller)context.Controller;

        object model = controller.ViewData.Model;

        Type resultListGenericType = typeof(ResultListViewModel<>).MakeGenericType(new Type[] { _desinationType });

        Type sourceGenericType = typeof(List<>).MakeGenericType(new Type[] { _sourceType });

        Type destinationGenericType = typeof(List<>).MakeGenericType(new Type[] { _desinationType });

        // It equals to the code AutoMapper.Mapper.CreateMap(_sourceType, _destinationType);
        AutoMapper.Mapper.Initialize(config => config.CreateMap(_sourceType, _desinationType));

        object viewModel = AutoMapper.Mapper.Map(model, sourceGenericType, destinationGenericType);

        // from QueryOptions
        var sortField = controller.ViewData.ContainsKey("SortField") ? controller.ViewData["SortField"] : "default";

        // from QueryOptions
        var sortOrder = controller.ViewData.ContainsKey("SortOrder") ? controller.ViewData["SortOrder"] : "desc";

        // from QueryOptions
        var currentPage = controller.ViewData.ContainsKey("CurrentPage") ? controller.ViewData["CurrentPage"] : 1;

        var resultList = Activator.CreateInstance(resultListGenericType, viewModel, sortField, sortOrder, currentPage);

        controller.ViewData.Model = resultList;
    }

    public override void OnResultExecuted(ResultExecutedContext context)
    {
        base.OnResultExecuted(context);
    }
}

}

'''