Life Through A Lens: Automatically registering Glass Configuration Maps using a container

In Glass Mapper V4, we introduced an awesome feature for keeping your POCO objects isolated and allowing easier use of the fluent configuration API called Glass Mapper Configuration Maps. This post describes how you can auto-register all of the maps in a given assembly using your IoC container.

I have used SimpleInjector as the basis for this example, but it would in fact be easier with some other containers as they have assembly scanning functionality built in (I think SimpleInjector does too tbh).

First of all – you need register all of the appropriate classes within your desired assembly with your container. I have done this in the example below using a simple reflection call:

   string assemblyName = "MyCo.Models.Mapping";

   Assembly assembly = AppDomain.CurrentDomain.GetAssemblies().First(x => x.FullName.IndexOf(assemblyName, StringComparison.OrdinalIgnoreCase) >= 0);

   Type glassmapType = typeof (IGlassMap);
   IEnumerable<Type> maps = assembly.GetTypes().Where(x => glassmapType.IsAssignableFrom(x));

   container.RegisterAll<IGlassMap>(maps);

Now, I need a way to get at the container from the Glass configuration setup (commonly done using the GlassScCustom.cs for the main web application). I have created a simple class below for demonstration, we are effectively service locating our maps, but I think this is a reasonable option given how little we would want to change the Glass configuration. Most implementations have some sort of Service Location somewhere down the line, so you could easily plug in your own.

using SimpleInjector;

namespace MyCo.Web.IoC
{
   public class ContainerManager
   {
      private static readonly Container container = new Container();

      public Container Container
      {
         get
         {
            return container;
         }
      }
   }
}

Finally, we simply modify the GlassScCustom.cs to add the following:

   public static void AddMaps(IConfigFactory<IGlassMap> mapsConfigFactory)
   {
      ContainerManager containerManager = new ContainerManager();
      foreach (var map in containerManager.Container.GetAllInstances<IGlassMap>())
      {
         mapsConfigFactory.Add(() => map);
      }
   }

Hope this helps ๐Ÿ˜€

Advertisements

2 thoughts on “Life Through A Lens: Automatically registering Glass Configuration Maps using a container

  1. I really like what you’ve done here and it’s helped me get my model projects further away from their direct dependence on Glass. However, the one dependence I have left are properties that are Glass types – Image and Link mostly. If it wasn’t for these types, I could remove both the Sitecore.Kernel and Glass.Mapper.Sc references from my model projects completely!

    How do you handle these field types?

    • Sadly at this time, I have the same issue. I have been considering how it would be possible to deal with this, but the File / Image and Link data types are quite tightly tied in to the data mappers that correspond to their display..

      You COULD potentially create your own version of the necessary data mappers and change to use your own Image / File or Link, but I am not sure the effort warrants the reward.

      I would definitely suggest you raise a ticket on GitHub so it can be discussed further ๐Ÿ˜€

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s