我正在测试 Ninject 并试图了解如何将存储库注入单例类。下面是工作存储库和单例类示例......
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()
{
}
}
成功通过的测试方法
[TestMethod]
public void SimpleTestSingleton()
{
using (IKernel kernel = new StandardKernel())
{
kernel.Bind<ITestRepository>().To<TestRepository>();
var testSingletonInjectionClass =
kernel.Get<TestSingletonInjectionClass>();\
Assert.IsNotNull(testSingletonInjectionClass);
}
}
我有两个问题
1.这是获取单例类实例的正确方法吗?
kernel.Get<TestSingletonInjectionClass>()
2.如何从应用代码中获取单例实例类。在测试方法中,我创建 Ninject Kernel 并访问 Get 方法。如何访问ninject内核表单代码?