Skip to content

Instantly share code, notes, and snippets.

@rsleggett
Created January 28, 2013 00:03
Show Gist options
  • Save rsleggett/4651524 to your computer and use it in GitHub Desktop.
Save rsleggett/4651524 to your computer and use it in GitHub Desktop.
This gist shows code for an IResourceProvider backed by DD4T. Depends on log4net. Assumes you have an implementation of the ICacheWrapper interface inserted into the IoC container being used as the DependencyResolver
using System.Web.Mvc;
using DD4T.ContentModel.Contracts.Providers;
using DD4T.ContentModel.Factories;
using DD4T.Providers.SDLTridion2011sp1;
using log4net;
namespace BuildingBlocks.DD4T.Core
{
public static class DD4TResourceHelper
{
public static ILog _log = LogManager.GetLogger(typeof (DD4TResourceHelper));
public static IDictionary GetResources(IComponentProvider provider)
{
var query = new ExtendedQueryParameters();
query.PublicationId = int.Parse(ConfigurationManager.AppSettings["DD4T.PublicationId"]);
string labelsSchemaId = string.Format(ConfigurationManager.AppSettings["LabelsSchemaId"],
query.PublicationId);
query.QuerySchemas = new[] { labelsSchemaId };
var result = provider.FindComponents(query);
if (result.Count == 0)
{
return new ListDictionary();
}
string labelsUri = result[0];
var factory = DependencyResolver.Current.GetService<IComponentFactory>();
factory.ComponentProvider = provider;
var component = factory.GetComponent(labelsUri);
var dictionary = new ListDictionary();
var labels = component.Fields["label"].EmbeddedValues;
foreach (var label in labels)
{
string key = label["key"].Value;
string value = label["value"].Value;
if(!dictionary.Contains(key))
{
dictionary.Add(key, value);
}
else
{
_log.WarnFormat("Resource with key {0} has already been added to the dictionary", key);
}
}
return dictionary;
}
}
}
using System.Collections;
using System.Configuration;
using System.Globalization;
using System.Resources;
using System.Web.Compilation;
using System.Web.Mvc;
using BuildingBlocks.DD4T.Core.Caching;
using BuildingBlocks.DD4T.Core.Extensions;
using DD4T.ContentModel.Contracts.Providers;
namespace BuildingBlocks.DD4T.Core
{
public class DD4TResourceProvider : IResourceProvider
{
private IComponentProvider _provider;
private ICacheWrapper _cacheWrapper;
private int _cacheMinutes;
private object lockObject = new object();
public DD4TResourceProvider()
{
_provider = DependencyResolver.Current.GetService<IComponentProvider>();
_cacheWrapper = DependencyResolver.Current.GetService<ICacheWrapper>();
int.TryParse(ConfigurationManager.AppSettings["LabelsCacheMinutes"] ?? "1", out _cacheMinutes);
}
private IDictionary GetResourceCache(string location)
{
string key = "Resources_{0}".FormatWith(location);
IDictionary cache;
if (!_cacheWrapper.TryGet(key, out cache))
{
lock(lockObject)
{
if(!_cacheWrapper.TryGet(key,out cache))
{
cache = DD4TResourceHelper.GetResources(_provider);
_cacheWrapper.Insert(key, cache, _cacheMinutes);
}
}
}
return cache;
}
public object GetObject(string resourceKey, CultureInfo culture)
{
string cultureName = (culture == null ? "Default":culture.Name);
var dictionary = GetResourceCache(cultureName);
if(!dictionary.Contains(resourceKey))
return "Missing Resource with key: " + resourceKey;
return dictionary[resourceKey];
}
public IResourceReader ResourceReader
{
get { return new CustomResourceReader(GetResourceCache(CultureInfo.CurrentUICulture.Name)); }
}
}
public class DD4TResourceProviderFactory : ResourceProviderFactory
{
public override IResourceProvider CreateGlobalResourceProvider(string classKey)
{
return new DD4TResourceProvider();
}
public override IResourceProvider CreateLocalResourceProvider(string virtualPath)
{
return new DD4TResourceProvider();
}
}
internal sealed class CustomResourceReader : IResourceReader
{
private readonly IDictionary _dictionary;
public CustomResourceReader(IDictionary resources)
{
_dictionary = resources;
}
public void Close() { }
public IDictionaryEnumerator GetEnumerator()
{
return _dictionary.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return _dictionary.GetEnumerator();
}
public void Dispose() { }
}
}
using System;
namespace BuildingBlocks.DD4T.Core.Caching
{
public interface ICacheWrapper
{
void Insert<T>(string key, T obj, DateTime expiresOn);
void Insert<T>(string key, T obj, int minsToKeep);
bool TryGet<T>(string key, out T obj) where T : class;
}
}
using System.Globalization;
using System.Web;
using System.Web.Mvc;
namespace BuildingBlocks.DD4T.Core
{
public static class ResourceHelperExtensionMethods
{
public static string Resource(this HtmlHelper htmlHelper, string resourceName)
{
return (string)Resource(htmlHelper.ViewContext.HttpContext, resourceName);
}
public static string Resource<T>(this HtmlHelper<T> htmlHelper, string resourceName)
{
return (string) Resource(htmlHelper.ViewContext.HttpContext, resourceName);
}
public static object Resource(this HttpContextBase httpContext, string resourceName)
{
return httpContext.GetGlobalResourceObject(CultureInfo.CurrentUICulture.ToString(), resourceName);
}
}
}
using System;
using System.Web.Caching;
namespace BuildingBlocks.DD4T.Core.Caching
{
public sealed class WebCacheWrapper : ICacheWrapper
{
private Cache _cache;
public WebCacheWrapper(Cache cache)
{
_cache = cache;
}
public bool TryGet<T>(string key, out T obj)
where T : class
{
obj = (T)_cache[key];
return obj != null;
}
public void Insert<T>(string key, T obj, int minsToKeep)
{
Insert(key, obj, DateTime.Now.AddMinutes(minsToKeep));
}
public void Insert<T>(string key, T obj, DateTime expiresOn)
{
if (obj != null)
{
_cache.Insert(key, obj, null, expiresOn, System.Web.Caching.Cache.NoSlidingExpiration);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment