Navigation and service panel


Content

Separating Glass Mapper contextes

By Kevin Brechbühl on 24. October 2014, No comments

Glass Mapper itself works with a static context. Everytime on the application start, the Glass context is configured and registered to the dependency injection container. For example, if you have a separate module which is completely decoupled from your Sitecore solution but should also use Glass Mapper, you can't use the same static context. This blog post describes how you can create different contextes for different concerns.

If you install the default Glass Mapper package with Castle Windsor then you got a new class GlassMapperSc within your App_Start folder. In this class you'll find the following line of code, which creates a new Glass context:

var context = Glass.Mapper.Context.Create(resolver);

The created context will be stored as default context within a static dictionary of the class Glass.Mapper.Context. When you instantiate a new SitecoreContext, then by default also the default Glass context is used:

var sitecoreContext = new SitecoreContext();

But the Create() method of Glass.Mapper.Context has an overload which accepts a context name. In your module, you could then create a named context, e.g.:

var context = Glass.Mapper.Context.Create(resolver, "MyModule");

The same context name you also need while instantiating a new SitecoreContext:

var sitecoreContext = new SitecoreContext("MyModule");

So whenever you create a module (or some other decoupled part of your solution) you should always consider to using a separate Glass context. The different contextes within the same solution are running very smoothly side by side.

Did anyone of you already use this feature? I didn't even know about this, until I ran into problems with a module these days.

Categories  Configuration  Modules Tags  Glass Mapper  Context

No comments

Add your comment

Your email address will not be published. Required fields are marked *

*