Glass Mapper + Unity

Dav*_*ers 9 asp.net-mvc sitecore unity-container glass-mapper

据我所知,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;
    }
}
Run Code Online (Sandbox Code Playgroud)

有人可以提供一个代码示例,说明如何将Glass与Unity连接起来?

Jim*_*sch 3

实际上,您不再需要 IoC 容器来配置 Glass 的基本映射设置。

相反,配置注册ISitecoreContext并覆盖自定义 MVC 控制器工厂的必要条件ReleaseControllerGetControllerInstance方法。

我的 Unity 有点生疏,但是是这样的(你可能会发现更简单的东西):

IUnityContainer container = new UnityContainer()
container.RegisterType<ISitecoreContext, SitecoreContext>(new HierarchicalLifetimeManager(), new InjectionFactory(x => new SitecoreContext()));
Run Code Online (Sandbox Code Playgroud)