开发者

How to pass values to Auto Mapper at runtime

I just started using Auto Mapper and it is awesome :)

I have however run into some trouble with the following scenario. I need to pass a value from my controller (using asp.net mvc) to either a custom resolver or formatter. My controller takes in a string label "Day","Week", "Month", etc... that is used to get a filtered range of tasks.

How can I pass this value to my resolver?

Thanks!

/// <summary>
    /// Ajax Method for filtering tasks by Date Range
    /// </summary>
    /// <param name="dateFilter">The date filter label ["Day", "Week", "Month", "All", "Last 30 Days"]</param>
    /// <returns>List of task objects</returns>
    [HttpGet]
    public Jso开发者_StackOverflow社区nResult GetTasksByDateRange(DateFilters dateFilter)
    {
        List<Task> tasks = taskService.GetFilteredTasksByDate(dateFilter, "chobojunk@gmail.com");

        List<TaskTableViewModel> viewModelList = Mapper.Map<List<Task>, List<TaskTableViewModel>>(tasks);

        return Json(viewModelList, JsonRequestBehavior.AllowGet);
    }

// In RegisterMaps file

        Mapper.CreateMap<Task, TaskTableViewModel>().ForMember(dest => dest.DueDate, opt => opt.AddFormatter<DueDateFormatter>());


Only way I can think of to do this is through a DI/IoC container. Create a context object with your resolver parameter, inject it into the resolver, and update it from the controller before executing the mapper.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜