Skip to content

Instantly share code, notes, and snippets.

@steviemcg
Created June 20, 2014 18:04
Show Gist options
  • Select an option

  • Save steviemcg/1b0a9e2f9a21c24fdecc to your computer and use it in GitHub Desktop.

Select an option

Save steviemcg/1b0a9e2f9a21c24fdecc to your computer and use it in GitHub Desktop.
using System;
using Sitecore.Caching;
using Sitecore.ContentSearch;
namespace MyProject
{
public class MyContext
{
private static readonly ItemsContext _items;
public static ItemsContext Items
{
get
{
return _items;
}
}
static MyContext()
{
_items = new ItemsContext();
}
/// <summary>
/// An example of executing code just once per request
/// The actual example is quite contrived :)
/// </summary>
public static ISearchIndex SearchIndex
{
get
{
const string key = "ContentSearchIndex";
if (Items[key] == null)
{
if (Sitecore.Context.Database == null)
{
throw new Exception("Sitecore.Context.Database cannot be null");
}
Items[key] = Sitecore.Context.Database.Name == "web"
? ContentSearchManager.GetIndex("sitecore_web_index")
: ContentSearchManager.GetIndex("sitecore_master_index");
}
return (ISearchIndex)Items[key];
}
}
}
}
@steviemcg
Copy link
Author

Especially as it doesn't account for the database potentially being switched during the request ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment