gpt4 book ai didi

asp.net-mvc - ASP.NET MVC 和 StructureMap 的最佳实践 DI - 如何在 ActionResult 中注入(inject)依赖项

转载 作者:行者123 更新时间:2023-12-03 04:47:02 25 4
gpt4 key购买 nike

我编辑了整个问题,所以不要怀疑:)

嗯,我想要一个 ActionResult 来获取域模型数据和一些附加参数,即用于分页列表的页面索引和页面大小。它根据 Web 请求的类型(是否为 ajax 请求)自行决定是返回 PartialViewResult 还是 ViewResult。

应使用 IMappingService 自动映射引用的数据,IMappingService 负责将任何域模型数据转换为 View 模型。为了简单起见,MappingService 使用 AutoMapper。

映射操作结果:

public abstract class MappingActionResult : ActionResult
{
public static IMappingService MappingService;
}

BaseHybridView结果:

public abstract class BaseHybridViewResult : MappingActionResult
{
public const string defaultViewName = "Grid";

public string ViewNameForAjaxRequest { get; set; }
public object ViewModel { get; set; }

public override void ExecuteResult(ControllerContext context)
{
if (context == null) throw new ArgumentNullException("context");
var usePartial = ShouldUsePartial(context);
ActionResult res = GetInnerViewResult(usePartial);

res.ExecuteResult(context);
}

private ActionResult GetInnerViewResult(bool usePartial)
{
ViewDataDictionary viewDataDictionary = new ViewDataDictionary(ViewModel);
if (String.IsNullOrEmpty(ViewNameForAjaxRequest))
{
ViewNameForAjaxRequest = defaultViewName;
}

if (usePartial)
{
return new PartialViewResult { ViewData = viewDataDictionary, ViewName = ViewNameForAjaxRequest };
}

return new ViewResult { ViewData = viewDataDictionary };
}

private static bool ShouldUsePartial(ControllerContext context)
{
return context.HttpContext.Request.IsAjaxRequest();
}
}

AutoMappedHybridView结果:

public class AutoMappedHybridViewResult<TSourceElement, TDestinationElement> : BaseHybridViewResult
{
public AutoMappedHybridViewResult(PagedList<TSourceElement> pagedList)
{
ViewModel = MappingService.MapToViewModelPagedList<TSourceElement, TDestinationElement>(pagedList);
}

public AutoMappedHybridViewResult(PagedList<TSourceElement> pagedList, string viewNameForAjaxRequest)
{
ViewNameForAjaxRequest = viewNameForAjaxRequest;
ViewModel = MappingService.MapToViewModelPagedList<TSourceElement, TDestinationElement>(pagedList);
}

public AutoMappedHybridViewResult(TSourceElement model)
{
ViewModel = MappingService.Map<TSourceElement, TDestinationElement>(model);
}

public AutoMappedHybridViewResult(TSourceElement model, string viewNameForAjaxRequest)
{
ViewNameForAjaxRequest = viewNameForAjaxRequest;
ViewModel = MappingService.Map<TSourceElement, TDestinationElement>(model);
}
}

在 Controller 中的用法:

public ActionResult Index(int page = 1)
{
return new AutoMappedHybridViewResult<TeamEmployee, TeamEmployeeForm>(_teamEmployeeRepository.GetPagedEmployees(page, PageSize));
}

如您所见,IMappingService 已隐藏。当使用 AutoMappedHybridViewResult 时, Controller 不应该知道有关 IMappingService 接口(interface)的任何信息。

MappingActionResultstatic IMappingServer 是否合适,或者我是否违反了 DI 原则?

最佳答案

我认为更好的设计是拥有一个依赖于 IMappingService 的 ViewResultFactory,然后您可以将其注入(inject)到 Controller 中。然后你可以这样调用它:

public class MyController : Controller
{
IViewResultFactory _viewResultFactory;
ITeamEmployeeRepository _teamEmployeeRepository;

public MyController(IViewResultFactory viewResultFactory)
{
_viewResultFactory = viewResultFactory;
}

public ActionResult MyAction(int page, int pageSize)
{
return
_viewResultFactory.GetResult<TeamEmployee, TeamEmployeeForm>(
_teamEmployeeRepository.GetPagedEmployees(page, pageSize));
}
}

实现会像这样(您需要为每个 HybridViewResult 构造函数创建重载):

public HybridViewResult<TSourceElement, TDestinationElement> GetResult<TSourceElement, TDestinationElement>(PagedList<TSourceElement> pagedList)
{
return new HybridViewResult<TSourceElement, TDestinationElement>(_mappingService, pagedList);
}

这样,您就可以对 Controller 隐藏实现,并且不必依赖容器。

关于asp.net-mvc - ASP.NET MVC 和 StructureMap 的最佳实践 DI - 如何在 ActionResult 中注入(inject)依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3833872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com