Resolving Glass Configuration Maps in Glass Mapper v4 using Castle Windsor
In one of my recent projects, we used the new version of Glass Mapper. One of the new features Glass has introduced is that of using Fluent Configuration API [Glass Configuration Maps] which helps in keeping the POCO’s isolated and re-usable. Below is an example of how I am using it with Castle Windsor.
Here is a simple model that we will use for reference.
public class TestPOCO
{
public virtual string Title { get;set; }
public virtual IEnumerable<Item> ListItems { get;set; }
public virtual bool IsTestSite { get;set;}
}
Now, we will map it to Sitecore using the Glass Map feature.
V4 Glass Map POCO
public class TestPOCOConfig : SitecoreGlassMap<TestPOCO> //inherits <IGlassMap>
{
public override void Configure()
{
Map(
x => x.AutoMap(),
x => x.Field(y => y.Title).FieldName("Headline"),
x =>
x.Field(y => y.ListItems).FieldName("List Items").Setting(SitecoreFieldSettings.InferType),
x => x.Delegate(y => y.IsTestSite).GetValue(y => IsTestSite())
//Delegate lets you call a method and use it's value...how cool is that!
);
}
}
The above configuration is similar to what was present in V3 but is much easier to read and manage IMO.
V3 POCO
[SitecoreType(AutoMap = true)]
public class TestPOCO
{
[SitecoreField("Headline")]
public virtual string Title { get; set; }
[SitecoreField("List Items", Setting = SitecoreFieldSettings.InferType)]
public virtual IEnumerable<Item> ListItems { get; set; }
public virtual bool IsTestSite { get; set; }
}
Glass also added the new Delegate feature which is simply awesome as seen above! You can read more about it here Glass Mapper Delegate.
We have the original model and the Glass Map configuration for the model set up correctly for use. Now we need to register it so Glass can pick it up and Windsor can resolve it.
Below is how we would do it via the basic Glass Configuration in the GlassMapperScCustom.cs file.
public static void AddMaps(IConfigFactory<IGlassMap> mapsConfigFactory)
{
// Add maps here
mapsConfigFactory.Add(() => new TestPOCOConfig());
}
Because we will be adding a bunch of models during development, we decided to resolve all of them via an IoC container such as Castle Windsor. You can use any of the IoC containers to do the same thing.
Check this for best IoC performer.
I created a new class called GlassMapConfigFactory.cs which inherits from IConfigFactory<IGlassMap>. The below code scans through the assembly and registers all the classes which inherit IGlassMap.
public class GlassMapConfigFactory : IConfigFactory<IGlassMap>
{
public void Insert(int index, Func<IGlassMap> add)
{
throw new NotImplementedException();
}
public void Add(Func<IGlassMap> add)
{
throw new NotImplementedException();
}
public IEnumnerable<IGlassMap> GetItems()
{
IWindsorContainer container = new WindsorContainer();
container.Register(Classes.FromThisAssembly().BasedOn<IGlassMap>().WithServiceBase());
return container.ResolveAll<IGlassMap>();
}
}
The last thing remaining to do is to tell Glass to pick this up in the GlassMapperScCustom.cs file.
public static IDependencyResolver CreateResolver()
{
var config = new Glass.Mapper.Sc.Config();
var dependencyResolver = new DependencyResolver(config)
{
ConfigurationMapFactory = new GlassMapConfigFactory();
};
return dependencyResolver;
}
Comment this out
public static void AddMaps(IConfigFactory<IGlassMap> mapsConfigFactory)
{
// Add maps here
//Resolved via Windsor
//mapsConfigFactory.Add(() => new TestPOCOConfig());
}
That’s it! Now, if you rebuild your solution and re-load the page which uses the above model, it will still work as before without any issues. You will not have to touch the GlassMapperScCustom.cs again for configuring new models.
Have a look at the below posts as well from the author/creator of this feature:
Registering Glass Maps using a container
Resolving Sitecore controllers using IoC
Let me know if you run into any issues or have any feedback. Cheers! :)