Permalink
Browse files

Added in an abstracted configuration

  • Loading branch information...
1 parent 915e24f commit 8f97bc89ff21e4ecaaa71c541523c54ebf4c3fd4 @gravypower gravypower committed Apr 3, 2013
Showing with 318 additions and 47 deletions.
  1. +5 −11 Source/Glass.Mapper.Sc/Caching/SitecoreObjectCache.cs
  2. +22 −0 Source/Glass.Mapper.Sc/Configuration/SitecoreGlassConfiguration.cs
  3. +22 −0 Source/Glass.Mapper.Sc/Configuration/SitecoreGlassConfigurationFactory.cs
  4. +2 −0 Source/Glass.Mapper.Sc/Glass.Mapper.Sc.csproj
  5. +1 −1 Source/Glass.Mapper/Caching/Configuration/AbstractObjectCacheConfiguration.cs
  6. +4 −8 Source/Glass.Mapper/Caching/ObjectCaching/AbstractObjectCache.cs
  7. +4 −7 Source/Glass.Mapper/Caching/ObjectCaching/MemoryObjectCache.cs
  8. +25 −0 Source/Glass.Mapper/Configuration/GlassConfiguration.cs
  9. +19 −0 Source/Glass.Mapper/Configuration/IGlassConfigurationFactory.cs
  10. +22 −6 Source/Glass.Mapper/Context.cs
  11. +4 −0 Source/Glass.Mapper/Glass.Mapper.csproj
  12. +7 −0 Tests/Integration Tests/Sitecore/Glass.Mapper.Sc.Integration/App_Config/Sitecore.config
  13. +42 −0 ...sts/Sitecore/Glass.Mapper.Sc.Integration/Configuation/SitecoreGlassConfigurationFactoryFixture.cs
  14. +4 −0 Tests/Integration Tests/Sitecore/Glass.Mapper.Sc.Integration/Glass.Mapper.Sc.Integration.csproj
  15. +4 −3 Tests/Unit Tests/Glass.Mapper.Sc.Tests/Caching/ObjectCaching/SitecoreObjectCacheFixture.cs
  16. +46 −0 Tests/Unit Tests/Glass.Mapper.Sc.Tests/Configuration/SitecoreGlassConfigurationFactoryFixture.cs
  17. +2 −0 Tests/Unit Tests/Glass.Mapper.Sc.Tests/Glass.Mapper.Sc.Tests.csproj
  18. +47 −0 Tests/Unit Tests/Glass.Mapper.Tests/Caching/Configuration/ContextFixture.cs
  19. +2 −1 Tests/Unit Tests/Glass.Mapper.Tests/Caching/ObjectCaching/AbstractObjectCacheFixture.cs
  20. +6 −4 Tests/Unit Tests/Glass.Mapper.Tests/Caching/ObjectCaching/MemoryObjectCacheFixture.cs
  21. +24 −0 Tests/Unit Tests/Glass.Mapper.Tests/Configuration/GlassConfigurationFactoryFixture.cs
  22. +2 −0 Tests/Unit Tests/Glass.Mapper.Tests/Glass.Mapper.Tests.csproj
  23. +1 −1 ...ests/Pipelines/ObjectConstruction/Tasks/ObjectCachingResolver/ObjectCachingResolverTaskFixture.cs
  24. +1 −5 ...pper.Tests/Pipelines/ObjectConstruction/Tasks/ObjectCachingSaver/ObjectCachingSaverTaskFixture.cs
@@ -4,6 +4,8 @@
using System.Runtime.InteropServices;
using System.Text;
using Glass.Mapper.Caching.ObjectCaching;
+using Glass.Mapper.Configuration;
+using Glass.Mapper.Sc.Configuration;
using Sitecore.Caching;
using Sitecore.Data;
@@ -14,22 +16,14 @@ public class SitecoreObjectCache : AbstractObjectCache<ID>
private readonly Cache _objectCache;
private readonly TimeSpan _cacheItemSlidingExpiration;
- public SitecoreObjectCache(AbstractCacheKeyResolver<ID> cacheKeyResolver)
- : base(cacheKeyResolver)
+ public SitecoreObjectCache(AbstractCacheKeyResolver<ID> cacheKeyResolver, SitecoreGlassConfiguration glassConfiguration)
+ : base(cacheKeyResolver, glassConfiguration)
{
- _objectCache = Cache.GetNamedInstance("Glass.Mapper.Sc.Caching", GetCacheSize());
+ _objectCache = Cache.GetNamedInstance(GlassConfiguration.CacheName, glassConfiguration.CacheSize);
_cacheItemSlidingExpiration = System.Web.Caching.Cache.NoSlidingExpiration;
}
- private static long GetCacheSize()
- {
- return Sitecore.StringUtil.ParseSizeString(
- Sitecore.Configuration.Settings.GetSetting(
- "Glass.Mapper.Sc.Caching.CacheSize", "100MB"
- )
- );
- }
protected override bool InternalContansObject(string objectKey)
{
@@ -0,0 +1,22 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Glass.Mapper.Configuration;
+
+namespace Glass.Mapper.Sc.Configuration
+{
+ public class SitecoreGlassConfiguration : GlassConfiguration
+ {
+ private long _cacheSize = 100;
+
+ /// <summary>
+ /// The name of the Cache
+ /// </summary>
+ public long CacheSize
+ {
+ get { return _cacheSize; }
+ set { _cacheSize = value; }
+ }
+ }
+}
@@ -0,0 +1,22 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Xml;
+using Glass.Mapper.Configuration;
+
+namespace Glass.Mapper.Sc.Configuration
+{
+ /// <summary>
+ /// GlassConfigurationFactory for Sitecore
+ /// </summary>
+ public class SitecoreGlassConfigurationFactory : IGlassConfigurationFactory<SitecoreGlassConfiguration>
+ {
+ private const string GlassConfigurationSectionNode = "GlassConfigurationSection/ObjectCaching";
+ public SitecoreGlassConfiguration CreateGlassConfiguration()
+ {
+ XmlNode config = Sitecore.Configuration.Factory.GetConfigNode(GlassConfigurationSectionNode);
+ return Sitecore.Configuration.Factory.CreateObject<SitecoreGlassConfiguration>(config);
+ }
+ }
+}
@@ -91,6 +91,8 @@
<Compile Include="Configuration\SitecoreFieldConfiguration.cs" />
<Compile Include="Configuration\SitecoreFieldSettings.cs" />
<Compile Include="Configuration\SitecoreFieldType.cs" />
+ <Compile Include="Configuration\SitecoreGlassConfiguration.cs" />
+ <Compile Include="Configuration\SitecoreGlassConfigurationFactory.cs" />
<Compile Include="Configuration\SitecoreIdConfiguration.cs" />
<Compile Include="Configuration\SitecoreInfoConfiguration.cs" />
<Compile Include="Configuration\SitecoreInfoType.cs" />
@@ -12,7 +12,7 @@ namespace Glass.Mapper.Caching.Configuration
public abstract class AbstractObjectCacheConfiguration
{
/// <summary>
- /// The
+ /// The Object Cache for Glass
/// </summary>
public IAbstractObjectCache ObjectCache { get; set; }
@@ -3,15 +3,14 @@
using System.Linq;
using System.Text;
using Glass.Mapper.Caching.Exceptions;
+using Glass.Mapper.Configuration;
namespace Glass.Mapper.Caching.ObjectCaching
{
- /// <summary>
- ///
- /// </summary>
public abstract class AbstractObjectCache<TIdType> : IAbstractObjectCache
{
protected AbstractCacheKeyResolver<TIdType> CacheKeyResolver;
+ protected GlassConfiguration GlassConfiguration;
protected abstract bool InternalContansObject(string objectKey);
protected abstract void InternalAddObject(string objectKey, object objectForCaching);
@@ -39,13 +38,10 @@ public void AddObject(Pipelines.ObjectConstruction.ObjectCachingArgs args)
InternalAddObject(objectKey, args.Result);
}
- /// <summary>
- ///
- /// </summary>
- /// <param name="cacheKeyResolver"></param>
- protected AbstractObjectCache(AbstractCacheKeyResolver<TIdType> cacheKeyResolver)
+ protected AbstractObjectCache(AbstractCacheKeyResolver<TIdType> cacheKeyResolver, GlassConfiguration glassConfiguration)
{
CacheKeyResolver = cacheKeyResolver;
+ GlassConfiguration = glassConfiguration;
}
}
@@ -3,21 +3,18 @@
using System.Linq;
using System.Runtime.Caching;
using System.Text;
+using Glass.Mapper.Configuration;
namespace Glass.Mapper.Caching.ObjectCaching
{
public class MemoryObjectCache<TIdType> : AbstractObjectCache<TIdType>
{
private volatile MemoryCache _objectCache;
- /// <summary>
- ///
- /// </summary>
- /// <param name="cacheKeyResolver"></param>
- public MemoryObjectCache(AbstractCacheKeyResolver<TIdType> cacheKeyResolver)
- : base(cacheKeyResolver)
+ public MemoryObjectCache(AbstractCacheKeyResolver<TIdType> cacheKeyResolver, GlassConfiguration glassConfiguration)
+ : base(cacheKeyResolver, glassConfiguration)
{
- _objectCache = new MemoryCache("Glass.Mapper.Caching.ObjectCaching");
+ _objectCache = new MemoryCache(GlassConfiguration.CacheName);
}
protected override bool InternalContansObject(string objectKey)
@@ -0,0 +1,25 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Glass.Mapper.Configuration
+{
+ /// <summary>
+ /// Holds configuration for Glass
+ /// </summary>
+ public class GlassConfiguration
+ {
+
+ private string _cacheName = "Glass.Mapper.Caching.ObjectCaching";
+
+ /// <summary>
+ /// The name of the Cache
+ /// </summary>
+ public string CacheName
+ {
+ get { return _cacheName; }
+ set { _cacheName = value; }
+ }
+ }
+}
@@ -0,0 +1,19 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Glass.Mapper.Configuration
+{
+ /// <summary>
+ /// Interface to give the ability for
+ /// </summary>
+ public interface IGlassConfigurationFactory<T> where T: GlassConfiguration
+ {
+ /// <summary>
+ /// Builds a GlassConfiguration object
+ /// </summary>
+ /// <returns>GlassConfiguration object</returns>
+ T CreateGlassConfiguration();
+ }
+}
@@ -115,9 +115,28 @@ public static void Clear()
public IDependencyResolver DependencyResolver { get; set; }
/// <summary>
+ /// Configuration for the instance of thie glass context
+ /// </summary>
+ public GlassConfiguration Configuration { get; set; }
+
+
+ private AbstractObjectCacheConfiguration _objectCacheConfiguration;
+
+ /// <summary>
/// The configuration for the Glass Cache
/// </summary>
- public AbstractObjectCacheConfiguration ObjectCacheConfiguration { get; set; }
+ public AbstractObjectCacheConfiguration ObjectCacheConfiguration
+ {
+ get
+ {
+ if (_objectCacheConfiguration == null)
+ {
+ ConfigureCache();
+ }
+
+ return _objectCacheConfiguration;
+ }
+ }
@@ -156,9 +175,6 @@ public void Load(params IConfigurationLoader[] loaders)
var typeConfigurations = loaders
.Select(loader => loader.Load()).Aggregate((x, y) => x.Union(y));
-
-
-
//first we have to add each type config to the collection
foreach (var typeConfig in typeConfigurations)
{
@@ -237,7 +253,7 @@ public AbstractTypeConfiguration GetTypeConfiguration(object obj)
/// </summary>
public void ConfigureCache()
{
- ObjectCacheConfiguration =
+ _objectCacheConfiguration =
DependencyResolver.Resolve<AbstractObjectCacheConfiguration>();
}
@@ -247,7 +263,7 @@ public void ConfigureCache()
/// <param name="objectCacheConfiguration"></param>
public void ConfigureCache(AbstractObjectCacheConfiguration objectCacheConfiguration)
{
- ObjectCacheConfiguration = objectCacheConfiguration;
+ _objectCacheConfiguration = objectCacheConfiguration;
}
}
}
@@ -54,6 +54,7 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
+ <Reference Include="System.Runtime.Caching" />
<Reference Include="System.Web" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
@@ -74,6 +75,7 @@
<Compile Include="Caching\ObjectCaching\AbstractCacheKeyResolver.cs" />
<Compile Include="Caching\ObjectCaching\AbstractObjectCache.cs" />
<Compile Include="Caching\ObjectCaching\IAbstractObjectCache.cs" />
+ <Compile Include="Caching\ObjectCaching\MemoryObjectCache.cs" />
<Compile Include="Configuration\AbstractTypeConfiguration.cs" />
<Compile Include="Configuration\AbstractPropertyConfiguration.cs" />
<Compile Include="Configuration\Attributes\AttributeConfigurationLoader.cs" />
@@ -90,8 +92,10 @@
<Compile Include="Configuration\ChildrenConfiguration.cs" />
<Compile Include="Configuration\ConfigurationException.cs" />
<Compile Include="Configuration\FieldConfiguration.cs" />
+ <Compile Include="Configuration\GlassConfiguration.cs" />
<Compile Include="Configuration\IConfigurationLoader.cs" />
<Compile Include="Configuration\IdConfiguration.cs" />
+ <Compile Include="Configuration\IGlassConfigurationFactory.cs" />
<Compile Include="Configuration\InfoConfiguration.cs" />
<Compile Include="Configuration\LinkedConfiguration.cs" />
<Compile Include="Configuration\NodeConfiguration.cs" />
@@ -3229,4 +3229,11 @@
-->
<setting name="EmailValidation" value="^[a-zA-Z0-9_-]+(?:\.[a-zA-Z0-9_-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$" />
</settings>
+
+ <GlassConfigurationSection>
+ <ObjectCaching>
+ <CacheName>Glass.Mapper.Caching.ObjectCaching.ConfigFile</CacheName>
+ <CacheSize>1000</CacheSize>
+ </ObjectCaching>
+ </GlassConfigurationSection>
</sitecore>
@@ -0,0 +1,42 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Xml;
+using Glass.Mapper.Configuration;
+using Glass.Mapper.Sc.Configuration;
+using NUnit.Framework;
+
+namespace Glass.Mapper.Sc.Integration.Configuation
+{
+ [TestFixture]
+ public class SitecoreGlassConfigurationFactoryFixture
+ {
+ [Test]
+ public void CanBuildGlassConfiguration()
+ {
+ var galssConfigFactory = new SitecoreGlassConfigurationFactory();
+ var glassConfig = galssConfigFactory.CreateGlassConfiguration();
+
+ Assert.IsNotNull(glassConfig);
+ }
+
+ [Test]
+ public void HasCacheName()
+ {
+ var galssConfigFactory = new SitecoreGlassConfigurationFactory();
+ var glassConfig = galssConfigFactory.CreateGlassConfiguration();
+
+ Assert.AreEqual("Glass.Mapper.Caching.ObjectCaching.ConfigFile", glassConfig.CacheName);
+ }
+
+ [Test]
+ public void HasCacheSize()
+ {
+ var galssConfigFactory = new SitecoreGlassConfigurationFactory();
+ var glassConfig = galssConfigFactory.CreateGlassConfiguration();
+
+ Assert.AreEqual(1000, glassConfig.CacheSize);
+ }
+ }
+}
@@ -80,6 +80,7 @@
<ItemGroup>
<Compile Include="CodeFirst\GlassDataProviderFixture.cs" />
<Compile Include="Configuation\Fluent\FluentGeneralFixture.cs" />
+ <Compile Include="Configuation\SitecoreGlassConfigurationFactoryFixture.cs" />
<Compile Include="ConnectionFixture.cs" />
<Compile Include="DataMappers\AbstractMapperFixture.cs" />
<Compile Include="DataMappers\AbstractSitecoreFieldMapperFixture.cs" />
@@ -252,6 +253,9 @@
<Name>Glass.Mapper</Name>
</ProjectReference>
</ItemGroup>
+ <ItemGroup>
+ <Folder Include="Caching\" />
+ </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="BeforeBuild">
</Target>
@@ -6,6 +6,7 @@
using Glass.Mapper.Caching.ObjectCaching;
using Glass.Mapper.Pipelines.ObjectConstruction;
using Glass.Mapper.Sc.Caching;
+using Glass.Mapper.Sc.Configuration;
using NSubstitute;
using NUnit.Framework;
using Sitecore.Data;
@@ -41,7 +42,7 @@ public void AbstractObjectCacheContainObject()
//Assign
- var abstractObjectCache = new SitecoreObjectCache(_cacheKeyResolver);
+ var abstractObjectCache = new SitecoreObjectCache(_cacheKeyResolver, new SitecoreGlassConfiguration());
//Act
abstractObjectCache.AddObject(args);
@@ -66,7 +67,7 @@ public void AbstractObjectCacheAddObject()
_cacheKeyResolver.GetKey(args).Returns(key);
- var abstractObjectCache = new SitecoreObjectCache(_cacheKeyResolver);
+ var abstractObjectCache = new SitecoreObjectCache(_cacheKeyResolver, new SitecoreGlassConfiguration());
abstractObjectCache.AddObject(args);
_cacheKeyResolver.GetKey(args).Returns(key);
args.CacheKey = key;
@@ -83,7 +84,7 @@ public void AbstractObjectCacheAddObject()
public void ExpectWhenTryToAddObjectAgain()
{
//Assign
- var abstractObjectCache = new SitecoreObjectCache(_cacheKeyResolver);
+ var abstractObjectCache = new SitecoreObjectCache(_cacheKeyResolver, new SitecoreGlassConfiguration());
var id = new ID(new Guid("d1f6f2b8-93dd-477d-8089-c3639e713fab"));
var revisionId = new ID(new Guid("d1f6f2b8-93dd-477d-8089-c3639e713fab"));
Oops, something went wrong.

0 comments on commit 8f97bc8

Please sign in to comment.