Glass Mapper 4.2: The most flexible Sitecore ORM just got more flexible

Over the last couple of months, within the Glass Mapper team we have been busy considering a few things regarding the framework and decided this upcoming release of Glass Mapper should aim to improve on the flexibility of certain areas of the product as well as the ability for developers to get involved with our source more easily. Here is a run down of the key features of this new release and what it means.

Continue reading

Advertisements

Life Through a Lens – Extending and Modifying Glass Rendering Output

Have been asked a couple of times lately about ways to extend Glass Mapper given that in many cases it does not call the renderField pipeline. Glass Mapper is a mature and extensible API, so it in itself is not so tricky to customise.

Below are a couple of example snippets on how you can modify / extend rendering of html (note – this code compiles, I have not run it in a production solution).

Continue reading

Life Through a Lens – Using the Sitecore ContentDatabase in Glass Mapper

This post should hopefully be a short one. Just to note that in the since v4.0.3 of Glass Mapper we have added a SitecoreContentContext. This class inherits from ISitecoreContext just as the regular SitecoreContext does.

This differs from the standard SitecoreContext in that it uses Sitecore.Context.ContentDatabase instead of Sitecore.Context.Database.


ISitecoreContext sitecoreContext = new SitecoreContentContext();

Happy Mapping!!

Life Through a Lens – Lesser known Glass Mapper features

Mapping types

[SitecoreNode] / Node() – This mapping type allows you to map to another item in Sitecore directly by the string representation of id as a guid or by its path. You would typically map this to another Glass mapped type.
[SitecoreLinked] / Linked() – This mapping type allows you to map to an IEnumerable for items that are linked to the mapped item (think navigate > links in the standard Sitecore ribbon). This (using the configuration options) can be referrers / referred to items.
[Ignore] / Ignore() – this can definitely be handy for example when injecting content from external sources into your models from controller code.

Supported types
Whilst not mapping types, Glass can indeed map to types that are not quite so commonly used.
RuleList – if you map to a rulelist (technically a [SitecoreField] mapping) then you can actually get out a usable set of conditions for use in the Sitecore rules api.
Nullable Mapping – Glass Mapper has long supported mapping to nullable .net types (e.g. int? or Nullable), this includes the majority of regular mappings with HasValue generally referring to whether the Sitecore field was empty or not.

Typed Edit Frames
“Because I am lazy” is the best and most useful introduction to a feature I have seen in a while – brought to to from this V4 Demo at around 26min 15secs. The whole video is well worth a watch, but it shows them in use. Typed edit frames though are a feature to allow the creation of edit frames based upon Glass models.

Mapping to / from external data

One of the great advantages of Glass models being detached from Sitecore (unlike most wrapping based solutions) is the ability to map data from external third party solutions. In the Glass Mapper tutorials, there is a great example of mapping Glass Mapper objects using external data.

Data transfer between environments

Another great advantage of Glass models being detached from the Sitecore item is that, using a Glass model, you can breach the master / web boundary or even environment boundary. So, for example, it would be perfectly easy to write a web service (maybe for transferring data from web to master for the purpose of saving user submitted FAQ’s for example), that would construct a Glass model on the web node, post it to the master node and save it using regular Glass code.

Model Caching
Model caching was primarily designed to allow the caching of models that are used in locations that cannot use regular Sitecore caching. This could be for example, a rendering that contains a form and would thus need to respond to postbacks etc.

Configuration Maps
Configuratioin maps are a wrapper for regular Glass fluent configuration that allows better separation from the models, it was covered in further detail in V4 – Configuration Maps

VersionCountDisabler
The version count disabler is a simple structure that allows Glass to behave a little more like the standard Sitecore API, in use it is really this simple

using(new VersionCountDisabler)
{
   var whatever = sitecoreContext.GetItem<IWhatever>(); // this code will get the item even if the version count is 0
}

More information on version count disabling can be found at Glass.lu – VersionCountDisabler

Typed Glass Controllers
A relatively recent addition to Glass Mapper has been the addition of a typed GlassController, this was described further in Unit Testing with GlassController

Further Reading / Watching

Glass Mapper V4 release post
Glass Mapper V4 release video

Life Through a Lens – Unit Testing with Glass Controllers

Never let it be said that I like unit testing 😛

With the release of Glass Mapper 4.0.3 I am pleased to shout out that we have made some improvements to the unit testability of both GlassController & GlassUserControl. In addition we have also made some additional helper derivatives to make your awesome development lives even more awesome. You can find out more about the other improvements in the Glass Release Notes

In this post, I will cover some of these changes and show how you can now use GlassController in your unit tests effectively.

Continue reading

Life Through A Lens – Using your own IoC container with Glass Mapper

This should be a nice quick post, but when I look back, it’s something I have done on every project I have used Glass Mapper on.

Glass’ out of the box Castle Windsor implementation tends to create it’s own IWindsorContainer during Sitecore’s initialise pipeline (formerly done using WebActivator). This is great if you want to get up and running quickly, but in my case – especially with the new Delegate mapping function available, I often find that I would rather have a more global container than this, since – for example, some of the delegate mapping does use dependencies from my codebase.

In this post I will quickly describe how to use an external Castle Windsor container with Glass Mapper. If you want to find out more on using other IoC containers including Autofac, StructureMap or Unity as the container for Glass, please take a look at Glass Mapper On Agnostic IoC

Continue reading