Glass Mapper + Unity

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

I want to be able to 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 some sample code for how to get Glass related to Unity?

+7
asp.net-mvc unity-container sitecore glass-mapper
source share
1 answer

In fact, you do not need an IoC container to configure the basic mapping settings for Glass.

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

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

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

All Articles