Glass Mapper + Unity
Asked Answered
N

1

10

I understand that the Glass Mapper v4 can now be used in conjunction with any IoC container. But I'm struggling to find a code example of how to achieve this.

I want to be able register glass components and inject them into my controllers using Unity, for example:

public class SearchController : Controller
{
    private readonly ISitecoreContext _context;

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

Can someone provide a code example for how to get Glass linked up with Unity?

Ninos answered 14/8, 2015 at 11:58 Comment(0)
D
3

You don't actually need an IoC container to configure the basic mapping setup for Glass anymore.

Instead, configure a registration for ISitecoreContext and a custom MVC controller factory overriding the requisite ReleaseController and GetControllerInstance methods.

My Unity is a little rusty but something like this (you might find something simpler):

IUnityContainer container = new UnityContainer()
container.RegisterType<ISitecoreContext, SitecoreContext>(new HierarchicalLifetimeManager(), new InjectionFactory(x => new SitecoreContext()));
Davedaveda answered 18/8, 2015 at 1:36 Comment(1)
Yep I actually realised this last week - super simple. For some reason I was expecting it to be more difficult to hook up an IoC.Ninos

© 2022 - 2024 — McMap. All rights reserved.