Skip to content

Instantly share code, notes, and snippets.

@cammerman
Created February 11, 2011 05:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cammerman/821979 to your computer and use it in GitHub Desktop.
Save cammerman/821979 to your computer and use it in GitHub Desktop.
Cache registrations 2
builder
.RegisterType<Cache>()
.As<ICache>()
.WithMetadata("CacheType", ECacheType.Thumb)
.InstancePerLifetimeScope();
builder
.RegisterType<Cache>()
.As<ICache>()
.WithMetadata("CacheType", ECacheType.Image)
.InstancePerLifetimeScope();
using System;
using System.Linq;
using System.Collections.Generic;
using Autofac.Features.Metadata;
namespace HowILearnedToLoveKeyedDependencies
{
public class ImageBrowser
{
public virtual ICache ThumbCache
{
get;
private set;
}
public virtual ICache ImageCache
{
get;
private set;
}
protected virtual Func<Meta<ICache>, Boolean> CacheTypeChecker(ECacheType cacheType)
{
return
cache =>
cache.Metadata["CacheType"]
.Equals(cacheType);
}
public ImageBrowser(IEnumerable<Meta<ICache>> caches)
{
ThumbCache =
caches
.First(CacheTypeChecker(ECacheType.Thumb))
.Value;
ImageCache =
caches
.First(CacheTypeChecker(ECacheType.Image))
.Value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment