I am testing Ninject and trying to understand how to inject repository to singleton class. Below is working repository and singleton class example...
public interface ITestRepository
{
void TestRepositoryMethod();
}
public class TestRepository:ITestRepository
{
public void TestRepositoryMethod()
{
}
}
public class TestSingletonInjectionClass
{
private readonly ITestRepository _repository;
public TestSingletonInjectionClass(
ITestRepository repository)
{
_repository = repository;
}
public void TestMethod()
{
}
}
The test method which pass sucesfully
[TestMethod]
public void SimpleTestSingleton()
{
using (IKernel kernel = new StandardKernel())
{
kernel.Bind<ITestRepository>().To<TestRepository>();
var testSingletonInjectionClass =
kernel.Get<TestSingletonInjectionClass>();\
Assert.IsNotNull(testSingletonInjectionClass);
}
}
I have two questions
1. Is this correct method to get singleton class instance?
kernel.Get<TestSingletonInjectionClass>()
2. How to get singleton instance class from app code. In test method I create Ninject Kernel and access Get method. How to access ninject kernel form code?