views:

229

answers:

1

I'm successfully using Ninject in my web applications by deriving my Global from NinjectHttpApplication and using the NinjectHttpModule in my web.config

What I want to do now is us DI in one of my class libraries and I don't know how to go about this. I have the following dummy class:

/// <summary>
    /// Testing Ninject DI in a class library
    /// </summary>
    public class Class1
    {
        [Inject]
        ICustomerRepository CustomerRepository { get; set; }

        public string SomeText { get; set; }

        public Class1(string text)
        {
            MyConfig config = new MyConfig();
            config.Configure();

            SomeText = text;
        }

        public Customer GetCustomer()
        {
            var customer = CustomerRepository.GetCustomer();
            return customer;
        }
    }

    public class MyConfig
    {
        public IKernel Configure()
        {
            IKernel kernel = new StandardKernel(new NinjectRepositoryModule());
            return kernel;
        }
    }

When I instantiate Class1 and call GetCustomer(), the CustomerRepository is null, so I'm obviously doing something wrong?

Also, if I wanted to use constructor injection and have my constructor like

public Class1([Inject] ICustomerRepository repository)

how would I go about instantiating Class1?

Quite new to Ninject, so this could all be very easy stuff.

+1  A: 

Looks like I already know how to do this - whoops :)

http://stackoverflow.com/questions/1294779/ninject-how-and-when-to-inject

Ciaran