Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Glass Context Provider vs Direct Context
using System;
using System.Diagnostics;
using xxx.App_Start;
using Glass.Mapper.Sc;
using Xunit;
namespace xxx.UnitTests
{
public class GlassContextTests
{
public GlassContextTests()
{
GlassMapperSc.Start(); // comes from the actual Glass Mapper startup for the solution
// Warm up
ISitecoreContext context = new SitecoreContext();
}
[Fact]
public void GetClassWithDirectContextDependency_Lots()
{
Console.WriteLine("Generating class with direct dependency on ISitecoreContext");
var pass1 = GetClassWithDirectContextDependency();
Console.WriteLine("Pass 1: {0}ms", pass1);
var pass2 = GetClassWithDirectContextDependency();
Console.WriteLine("Pass 2: {0}ms", pass2);
var pass3 = GetClassWithDirectContextDependency();
Console.WriteLine("Pass 3: {0}ms", pass3);
Console.WriteLine("Average: {0}ms", (pass1 + pass2 + pass3) / 3);
}
[Fact]
public void GetClassWithContextProviderDependency_Lots()
{
Console.WriteLine("Generating class with direct dependency on ISitecoreContextProvider");
var pass1 = GetClassWithContextProvider();
Console.WriteLine("Pass 1: {0}ms", pass1);
var pass2 = GetClassWithContextProvider();
Console.WriteLine("Pass 2: {0}ms", pass2);
var pass3 = GetClassWithContextProvider();
Console.WriteLine("Pass 3: {0}ms", pass3);
Console.WriteLine("Average: {0}ms", (pass1 + pass2 + pass3) / 3);
}
private static long GetClassWithDirectContextDependency()
{
Stopwatch sw = new Stopwatch();
sw.Start();
for (var i = 0; i < 1000; i++)
{
SitecoreContext context = new SitecoreContext();
GlassDependentClassA classA = new GlassDependentClassA(context);
// ensures the if statement does not skew results
if (i%2 == 1)
{
Assert.Equal(context, classA.SitecoreContext);
}
}
sw.Stop();
return sw.ElapsedTicks;
}
private static long GetClassWithContextProvider()
{
Stopwatch sw = new Stopwatch();
sw.Start();
for (var i = 0; i < 1000; i++)
{
SitecoreContextProvider sitecoreContextProvider = new SitecoreContextProvider();
GlassDependentClassB classB = new GlassDependentClassB(sitecoreContextProvider);
if (i%2 == 1)
{
Assert.Equal(sitecoreContextProvider.SitecoreContext, classB.SitecoreContext);
}
}
sw.Stop();
return sw.ElapsedTicks;
}
public class GlassDependentClassA : ISitecoreContextDependence
{
public GlassDependentClassA(ISitecoreContext context)
{
SitecoreContext = context;
}
public ISitecoreContext SitecoreContext { get; private set; }
}
public class GlassDependentClassB : ISitecoreContextDependence
{
public GlassDependentClassB(ISitecoreContextProvider sitecoreContextProvider)
{
ContextProvider = sitecoreContextProvider;
}
protected ISitecoreContextProvider ContextProvider { get; private set; }
public ISitecoreContext SitecoreContext { get { return ContextProvider.SitecoreContext; } }
}
public interface ISitecoreContextDependence
{
ISitecoreContext SitecoreContext { get; }
}
public class SitecoreContextProvider : ISitecoreContextProvider
{
private ISitecoreContext sitecoreContext;
public ISitecoreContext SitecoreContext
{
get { return GetSitecoreContext(); }
}
protected virtual ISitecoreContext GetSitecoreContext()
{
// you could service locate this, but instantiation is still encapsulating the functionality nicely
return sitecoreContext ?? (sitecoreContext = new SitecoreContext());
}
}
public interface ISitecoreContextProvider
{
ISitecoreContext SitecoreContext { get; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.