Ninject: How do I inject into a class library ?

Posted by DennyDotNet on Stack Overflow See other posts from Stack Overflow or by DennyDotNet
Published on 2009-10-30T17:18:59Z Indexed on 2010/05/18 3:11 UTC
Read the original article Hit count: 210

To start I'm using Ninject 1.5. I have two projects: Web project and a Class library. My DI configuration is within the Web project. Within my class library I have the following defined:

    public interface ICacheService<T>
    {
            string Identifier { get; }
            T Get();
            void Set( T objectToCache, TimeSpan timeSpan );
            bool Exists();
    }

And then a concrete class called CategoryCacheService.

In my web project I bind the two:

Bind( typeof( ICacheService<List<Category>> ) ).To( typeof(CategoryCacheService)).Using<SingletonBehavior>();

In my class library I have extension methods for the HtmlHelper class, for example:

public static class Category

{ [Inject] public static ICacheService> Categories { get; set; }

public static string RenderCategories(this HtmlHelper htmlHelper) { var c = Categories.Get();

return string.Join(", ", c.Select(s => s.Name).ToArray()); } }

I've been told that you cannot inject into static properties, instead I should use Kernel.Get<>() - However... Since the code above is in a class library I don't have access to the Kernel. How can I get the Kernel from this point or is there a better way of doing this?

Thanks!

© Stack Overflow or respective owner

Related posts about dependency-injection

Related posts about ninject