Glass Mapper + Unity

10

我知道Glass Mapper v4现在可以与任何IoC容器一起使用。但是我很难找到如何实现这一点的代码示例。

我想能够注册glass组件并使用Unity将它们注入到我的控制器中,例如:

public class SearchController : Controller
{
    private readonly ISitecoreContext _context;

    //Inject via Unity
    public SearchController(Glass.Mapper.Sc.ISitecoreContext context)
    {
        _context = context;
    }
}

有人能提供一个代码示例,展示如何将Glass与Unity联系起来吗?

1个回答

3

现在,您不需要IoC容器来配置Glass的基本映射设置。

相反,为ISitecoreContext配置一个注册表和自定义MVC控制器工厂,覆盖必需的ReleaseControllerGetControllerInstance方法。

我的Unity有点生疏,但是可能会像这样(您可能会找到更简单的方法):

IUnityContainer container = new UnityContainer()
container.RegisterType<ISitecoreContext, SitecoreContext>(new HierarchicalLifetimeManager(), new InjectionFactory(x => new SitecoreContext()));

是的,我上个星期才意识到这一点——非常简单。由于某种原因,我以为连接IoC会更加困难。 - David Masters

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接