据我所知,Glass Mapper v4现在可以与任何IoC容器一起使用.但我正在努力寻找如何实现这一目标的代码示例.
我希望能够注册玻璃组件并使用Unity将它们注入我的控制器,例如:
public class SearchController : Controller { private readonly ISitecoreContext _context; //Inject via Unity public SearchController(Glass.Mapper.Sc.ISitecoreContext context) { _context = context; } }
解决方法
您实际上不需要IoC容器来配置Glass的基本映射设置.
相反,为ISitecoreContext和自定义MVC控制器工厂配置注册,覆盖必需的ReleaseController和GetControllerInstance方法.
我的Unity有点生疏,但是这样的东西(你可能会发现更简单的东西):
IUnityContainer container = new UnityContainer() container.RegisterType<ISitecoreContext,SitecoreContext>(new HierarchicalLifetimeManager(),new InjectionFactory(x => new SitecoreContext()));
原文地址:https://www.jb51.cc/aspnet/248214.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。