Created
September 3, 2012 18:40
-
-
Save Rookian/3612026 to your computer and use it in GitHub Desktop.
Dynamic Interception
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RepositoryInterceptor : IInterceptor | |
{ | |
private readonly MemoryCache _memoryCache; | |
public RepositoryInterceptor() | |
{ | |
_memoryCache = new MemoryCache("repositoryCache"); | |
} | |
public void Intercept(IInvocation invocation) | |
{ | |
var cacheKey = GenerateCacheKey(invocation.Method.Name, invocation.Arguments); | |
if (_memoryCache.Get(cacheKey) != null) | |
{ | |
invocation.ReturnValue = _memoryCache[cacheKey]; | |
return; | |
} | |
invocation.Proceed(); | |
_memoryCache[cacheKey] = invocation.ReturnValue; | |
} | |
private static string GenerateCacheKey(string methodName, ICollection arguments) | |
{ | |
if (arguments == null || arguments.Count == 0) | |
return methodName; | |
var argumentsKey = JsonConvert.SerializeObject(arguments); | |
return methodName + "#" + argumentsKey; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Type is not expected, and no contract can be inferred: CachingWithDynamicProxy.ByIdSpecification
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Caching;
using Castle.DynamicProxy;
using ProtoBuf;
using StructureMap;
namespace CachingWithDynamicProxy
{
class Program
{
static void Main()
{
var container = new Container();
Booting(container);
}