- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
public abstract class ConventionController : Controller
{
public const int PageSize = 5;
public IMappingService MappingService { get; set;}
}
如何设置StructureMap获取IMappingService实例?
编辑:
在 Joshua Flanagan 的帮助下,我现在有了以下代码:
雇员 Controller
public class EmployeeController : ConventionController
{
private readonly ITeamEmployeeRepository _teamEmployeeRepository;
public EmployeeController(ITeamEmployeeRepository teamEmployeeRepository)
{
_teamEmployeeRepository = teamEmployeeRepository;
}
public ActionResult Index(int page = 1)
{
// The IMappingService dependency is hidden in the AutoMappedHybridView method that is a part of the ConventionController, easy use in the controller
return AutoMappedHybridView<TeamEmployee, TeamEmployeeForm>(_teamEmployeeRepository.GetPagedEmployees(page, PageSize));
// With constructor injection I had to write this ...
// return new HybridViewResult<TSourceElement, TDestinationElement>(_mappingService, _teamEmployeeRepository.GetPagedEmployees(page, PageSize));
}
}
session Controller
public abstract class ConventionController : Controller
{
public const int PageSize = 5;
// This property is inject via StructureMap
public IMappingService MappingService { get; private set; }
public HybridViewResult<TSourceElement, TDestinationElement> AutoMappedHybridView<TSourceElement,TDestinationElement>(PagedList<TSourceElement> pagedList, string viewNameForAjaxRequest)
{
return new HybridViewResult<TSourceElement, TDestinationElement>(MappingService, pagedList, viewNameForAjaxRequest);
}
public HybridViewResult<TSourceElement, TDestinationElement> AutoMappedHybridView<TSourceElement,TDestinationElement>(PagedList<TSourceElement> pagedList)
{
return new HybridViewResult<TSourceElement, TDestinationElement>(MappingService, pagedList);
}
public HybridViewResult<TSourceElement, TDestinationElement> AutoMappedHybridView<TSourceElement, TDestinationElement>(TSourceElement sourceElement)
{
return new HybridViewResult<TSourceElement, TDestinationElement>(MappingService, sourceElement);
}
public HybridViewResult<TSourceElement, TDestinationElement> AutoMappedHybridView<TSourceElement, TDestinationElement>(TSourceElement sourceElement, string viewNameForAjaxRequest)
{
return new HybridViewResult<TSourceElement, TDestinationElement>(MappingService, sourceElement, viewNameForAjaxRequest);
}
}
混合 View 结果
public class HybridViewResult<TSourceElement, TDestinationElement> : BaseHybridViewResult
{
public HybridViewResult(IMappingService mappingService, PagedList<TSourceElement> pagedList)
{
ViewModel = mappingService.MapToViewModelPagedList<TSourceElement, TDestinationElement>(pagedList);
}
public HybridViewResult(IMappingService mappingService, PagedList<TSourceElement> pagedList, string viewNameForAjaxRequest)
{
ViewNameForAjaxRequest = viewNameForAjaxRequest;
ViewModel = mappingService.MapToViewModelPagedList<TSourceElement, TDestinationElement>(pagedList);
}
public HybridViewResult(IMappingService mappingService, TSourceElement model)
{
ViewModel = mappingService.Map<TSourceElement, TDestinationElement>(model);
}
public HybridViewResult(IMappingService mappingService, TSourceElement model, string viewNameForAjaxRequest)
{
ViewNameForAjaxRequest = viewNameForAjaxRequest;
ViewModel = mappingService.Map<TSourceElement, TDestinationElement>(model);
}
}
如您所见,HybridViewResult 需要 IMappingService 依赖项。
如果我在 ConventionController 中使用构造函数,我会“污染”我的 EmployeeController(恕我直言)。
如果 EmployeeController 直接需要 IMapping 依赖项,我会使用构造函数进行注入(inject)。但这不是必需的,因为已经存在 ConventionController 的 IMapping 属性。所以正如 Darin Dimitrov 所说,这将违反 DI 原则。
如何重构我的代码?我真的必须使用构造函数注入(inject)吗?
编辑2
我如何命令 StructureMap 来创建 HybridViewResult 的实例?如果这是可能的,那么 Controller 就不需要知道 IMapping 依赖项。是否有可能从 StructureMap 获取通用对象(未装箱)?
最佳答案
实际上,不,SetAllProperties() 不对抽象类起作用。这是 Structuremap 实现中的一个弱点。我为你感到抱歉,就像我为我自己一样,属性注入(inject)不适用于基本抽象类。对于基础抽象类,您将需要使用构造函数注入(inject)(与这里的所有宣传相反,在满足依赖性时,这并不总是最好的方法)。
关于c# - 最佳实践 : StructureMap and ASP. NET MVC 2 - 抽象基 Controller 中的 Setter 注入(inject)/构造注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3811402/
http://docs.structuremap.net/似乎有使用已弃用成员的非常古老的示例。 有什么地方可以找到最新的 StructureMap doco 吗? 最佳答案 我似乎在以下位置找到了一
我似乎无法弄清楚如何将对象定义为单例并为构造函数定义两个参数。 我可以做/或 .. 只是不能同时做。 例如。 (这不起作用)... ForRequestedType() .TheDefault
有没有办法确定在 StructureMap 中是否配置了特定类型? 如果在 StructureMap 中没有专门配置,我想返回一个泛型类型。 最佳答案 在 v2.6 你想要: IContainer.M
我想配置结构图以使用工厂类创建服务。工厂本身有一个需要填充的依赖项。目前我的注册表类中有以下内容: For().Singleton().Use(() => new DoStuffWebServ
我正在尝试使用 StructureMap 2.6.1 使用基于约定的注册一次注册我的所有存储库。见下面的代码: x.Scan(s => { s.TheCallingAssembly();
在 StructureMap 中你可以声明一个 Forward声明,这将允许注册单个具体实例,以由来自 StructureMap documentation 的多个接口(interface)解析: v
在 StructureMap 2 中,我有这样的事情: For().HybridHttpOrThreadLocalScoped().Use(); 使用 Structure Map 3 时,我应该使用以
如何将 StructureMap 与 OpenRasta 一起使用?我可以使用它来代替内部依赖解析器,还是只能将它与内置 DI 结合使用(即用于我自己的应用程序的依赖项)? 谢谢 最佳答案 Struc
我刚刚开始使用 StructureMap,之前使用过 Spring.Net。我喜欢 DefaultConventionScanner 以及扫描程序集和使用约定优于配置来查找类的能力。但是似乎有一个限制
我有一个通用接口(interface) public interface IDomainDataRepository { T[] GetAll(); } 有一个通用的实现 public cla
我正在尝试从 Structuremap 2.6.4.1 迁移到 3.1.4.143,但无法弄清楚如何处理 HybridHttpOrThreadLocalScoped。我能找到的所有 SO Q/A 似乎
所以我有一种情况,我想注册 n 个特定查找类型的映射。即: x.For().Add(); x.For().Add(); 我想让 SM 将它们的可枚举(或数组)注入(inject)到类的构造函数中: p
以下哪种语法被认为是最佳实践? For().LifecycleIs(new HybridLifecycle()).Use(); For().LifecycleIs(Lifecycles.GetLife
StructureMap 有没有办法用一行或约定来做这种重复映射? For>().Use(); For>().Use(); For>().Use(); For>().U
似乎 NLog 不能对 GetCurrentClassLogger() 使用反射,即使我的 MVC 3 应用程序部署在 中完全信任 IIS7 环境。我使用的是 StructureMap 2.6.1,问
StructureMap 定义了一个接口(interface) IBootStrapper,我看到很多人在他们的 Bootstrap 类中实现了这个接口(interface)。 但是我找不到任何可以说
我们将 Caliburn.Micro 用于我们的 MVVM 框架,将 StructureMap 用于我们的 IoC 容器,并将 MediatR 用于我们的调解器实现。这一切都工作正常,除了注册 Med
使用方法DecorateAllWith用 DynamicProxy 装饰所有实例都实现了一个接口(interface)? 例如: public class ApplicationServiceInte
在 autoFac 中,我可以注册一个接口(interface)的多个实现。当 autofac 实例化我的对象时,所有实例都传递给构造函数。 来自 autofac 的文档:here For examp
我有一个 Repository 的基类。在一个特定的项目中,我有几个这个基类的实现。例如 PersonRepository : Repository EmployerRepository : Repo
我是一名优秀的程序员,十分优秀!