Question

I trying to build nice architecture for my project and I decided to use Ninject as DI and Castle project dinamic proxy to add the caching for my repositories. Unfortunatly I get a exception. Here is my code:

public class NinjectImplementation : NinjectModule
{
    public override void Load()
    {
        // Binding repositories
        var assembly = Assembly.GetAssembly(typeof(UserRepository));
        var types = assembly.GetTypes()
             .Where(t => t.Name.EndsWith("Repository") && !t.Name.StartsWith("I"));
        ProxyGenerator generator = new ProxyGenerator();
        //CacheInterceptor cacheInterceptor = 
        foreach (var type in types)
        {
            var interfaceType = type.GetInterfaces().Single();
            var typeWithCaching = generator.CreateClassProxy(type, new MyTestShop.Infrastructure.Caching.CacheInterceptor());

            Bind(interfaceType).To(typeWithCaching.GetType()).InThreadScope();
        }
        ...//Service layer injection
    }
}

So I inject not the implementation of my repositories but the proxy class of the repositories(with caching).

Here is my IInterceptor implementation of the Castle dinamic proxy:

[Serializable]
public class CacheInterceptor : IInterceptor
{

    public void Intercept(IInvocation invocation)
    {
        int argumentCount = invocation.Arguments.Length;
        if (argumentCount > 1)
        {
            invocation.Proceed();
            return;
        }
        String methodNameInLower = invocation.Method.Name.ToLower();
        if (methodNameInLower.StartsWith("get"))
        {
            String cachePath = invocation.TargetType.FullName + "_" + invocation.Method.Name + "_" + invocation.Arguments[0].ToString();
            CacheHelper.Get(cachePath);
            //DO SOMETHING
            return;
        }

    }
}

The exception I get in _kernel.Get<T>() method of Ninject DI container:

  • Error activating IInterceptor using conditional implicit self-binding of IInterceptor Provider returned null.*

Activation path: 3) Injection of dependency IInterceptor into parameter of constructor of type UserRepositoryProxy 2) Injection of dependency IUserRepository into parameter userRepository of constructor of type UserService 1) Request for IUserService

Suggestions: 1) Ensure that the provider handles creation requests properly.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: Ninject.ActivationException: Error activating IInterceptor using conditional implicit self-binding of IInterceptor Provider returned null. Activation path: 3) Injection of dependency IInterceptor into parameter of constructor of type UserRepositoryProxy 2) Injection of dependency IUserRepository into parameter userRepository of constructor of type UserService 1) Request for IUserService

Suggestions: 1) Ensure that the provider handles creation requests properly.

Was it helpful?

Solution

I finally found the answer on my question. The problem was that my proxy is not a type but the instance of type so I fixed it like this:

var interfaceType = type.GetInterfaces().Single();

var proxy = generator.CreateClassProxy(type,
    new Type[] { interfaceType },
    new IInterceptor[]
    {
        new CacheInterceptor(), 
        new LoggingInterceptor()
    });

// I'm using directive ToConstant(..), and not To(..)
Bind(interfaceType).ToConstant(proxy).InThreadScope();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top